You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@synapse.apache.org by ru...@apache.org on 2009/04/07 22:10:31 UTC

svn commit: r762932 - in /synapse/trunk/java/modules: core/src/main/java/org/apache/synapse/ tasks/src/main/java/org/apache/synapse/task/

Author: ruwan
Date: Tue Apr  7 20:10:31 2009
New Revision: 762932

URL: http://svn.apache.org/viewvc?rev=762932&view=rev
Log:
Fixing the startup order of synapse

Modified:
    synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/Axis2SynapseController.java
    synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/ServerManager.java
    synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/SynapseConstants.java
    synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/SynapseController.java
    synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/SynapseServer.java
    synapse/trunk/java/modules/tasks/src/main/java/org/apache/synapse/task/TaskScheduler.java

Modified: synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/Axis2SynapseController.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/Axis2SynapseController.java?rev=762932&r1=762931&r2=762932&view=diff
==============================================================================
--- synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/Axis2SynapseController.java (original)
+++ synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/Axis2SynapseController.java Tue Apr  7 20:10:31 2009
@@ -113,10 +113,54 @@
     public void destroy() {
 
         try {
-            cleanupDefault();
-            // stop all services
             if (information.isCreateNewInstance()) {  // only if we have created the server
 
+                // destroy listener manager
+                if (listenerManager != null) {
+                    listenerManager.destroy();
+                }
+
+                // we need to call this method to clean the temp files we created.
+                if (configurationContext != null) {
+                    configurationContext.terminate();
+                }
+            }
+            initialized = false;
+        } catch (Exception e) {
+            log.error("Error stopping the Axis2 Based Server Environment", e);
+        }
+    }
+
+    public boolean isInitialized() {
+        return initialized;
+    }
+
+    /**
+     * Starts the listener manager if the axis2 instancec is created by the Synapse
+     */
+    public void start() {
+
+        if (information.isCreateNewInstance()) {
+            if (listenerManager != null) {
+                listenerManager.start();
+            } else {
+                handleFatal("Couldn't start Synapse, ListenerManager not found");
+            }
+        }
+    }
+
+    /**
+     * Cleanup the axis2 environment and stop the synapse environment.
+     */
+    public void stop() {
+        try {
+            cleanupDefault();
+            if (information.isCreateNewInstance()) {
+
+                if (listenerManager != null) {
+                    listenerManager.stop();
+                }
+
                 if (configurationContext != null &&
                         configurationContext.getAxisConfiguration() != null) {
 
@@ -135,26 +179,11 @@
                         }
                     }
                 }
-
-                // stop all transports
-                if (listenerManager != null) {
-                    listenerManager.stop();
-                    listenerManager.destroy();
-                }
-
-                // we need to call this method to clean the temp files we created.
-                if (configurationContext != null) {
-                    configurationContext.terminate();
-                }
             }
-            initialized = false;
-        } catch (Exception e) {
-            log.error("Error stopping the Axis2 Based Server Environment", e);
-        }
-    }
 
-    public boolean isInitialized() {
-        return initialized;
+        } catch (AxisFault e) {
+            log.error("Error stopping the Axis2 Environemnt");
+        }
     }
 
     /**
@@ -186,8 +215,8 @@
                     "' to the Axis2 configuration : " + e.getMessage(), e);
 
         }
-        synapseEnvironment.setInitialized(true);
         synapseConfiguration.init(synapseEnvironment);
+        synapseEnvironment.setInitialized(true);
         return synapseEnvironment;
     }
 
@@ -257,21 +286,16 @@
 
             listenerManager = configurationContext.getListenerManager();
             if (listenerManager == null) {
+                // create and initialize the listener manager but do not start
                 listenerManager = new ListenerManager();
                 listenerManager.init(configurationContext);
-            }
-
-            for (String trp : configurationContext.getAxisConfiguration()
-                    .getTransportsIn().keySet()) {
-
-                TransportInDescription trsIn =
-                        configurationContext.getAxisConfiguration().getTransportsIn().get(trp);
-                log.info("Starting transport " + trsIn.getName());
-                listenerManager.addListener(trsIn, false);
+                // do not use the listener manager shutdown hook, because it clashes with the
+                // SynapseServer shutdown hook.
+                listenerManager.setShutdownHookRequired(false);
             }
             
         } catch (Throwable t) {
-            handleFatal("Synapse startup failed...", t);
+            handleFatal("Failed to create a new Axis2 instance...", t);
         }
     }
 

Modified: synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/ServerManager.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/ServerManager.java?rev=762932&r1=762931&r2=762932&view=diff
==============================================================================
--- synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/ServerManager.java (original)
+++ synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/ServerManager.java Tue Apr  7 20:10:31 2009
@@ -36,33 +36,49 @@
 
     private static final Log log = LogFactory.getLog(ServerManager.class);
 
+    /* Keeps the ServerManager instance */
     private final static ServerManager instance = new ServerManager();
 
-    /* The controller for synapse create and Destroy synapse artifacts in a particular environment
-       Only for internal usage - DON"T PUT GETTER ,SETTER */
+    /**
+     * The controller for synapse create and Destroy synapse artifacts in a particular environment
+     * Only for internal usage - DON"T PUT GETTER ,SETTER
+     */
     private SynapseController synapseController;
+
     /* Server Configuration  */
     private ServerConfigurationInformation configurationInformation;
+
     /* Server context */
     private ServerContextInformation contextInformation;
-    /*Only represents whether server manager has been initialized by given required
-     configuration information - not server state or internal usage - DON"T PUT SETTER */
+
+    /**
+     * Only represents whether server manager has been initialized by given required
+     * configuration information - not server state or internal usage - DON"T PUT SETTER
+     */
     private boolean initialized = false;
 
+    /**
+     * Gives the access to the singleton instance of the ServerManager
+     *
+     * @return the ServerManager instance
+     */
     public static ServerManager getInstance() {
         return instance;
     }
 
     /**
-     * Initializes the server
+     * Initializes the server, if we need to create a new axis2 instance, calling this will create
+     * the new axis2 environment, but this won't start the transport lsiteners
      *
      * @param configurationInformation ServerConfigurationInformation instance
      * @param contextInformation       ServerContextInformation instance
-     * @return ServerState - The state of the server after call this operation
+     * @return ServerState - State of the server which is
+     *          {@link org.apache.synapse.ServerState#INITIALIZED}, if successful
      */
-    public ServerState init(ServerConfigurationInformation configurationInformation,
+    public synchronized ServerState init(ServerConfigurationInformation configurationInformation,
                             ServerContextInformation contextInformation) {
 
+        // sets the initializations parameters
         this.configurationInformation = configurationInformation;
         if (contextInformation == null) {
             this.contextInformation = new ServerContextInformation();
@@ -72,39 +88,97 @@
         this.synapseController = SynapseControllerFactory
                 .createSynapseController(configurationInformation);
 
-        this.initialized = true;
-        registerMBean();
+        // does the initialization of the controller
         doInit();
+        this.initialized = true;
         return this.contextInformation.getServerState();
     }
 
     /**
-     * Starting up the server
+     * Destroyes the Server instance. If the Server is stopped this will destroy the
+     * ServerManager, and if it is running (i.e. in the STARTED state) this will first stop the
+     * ServerManager and destroy it in turn
+     * 
+     * @return the state after the destruction, {@link org.apache.synapse.ServerState#UNDETERMINED}
+     */
+    public synchronized ServerState destroy() {
+
+        ServerState serverState = ServerStateDetectionStrategy.currentState(contextInformation,
+                configurationInformation);
+
+        switch (serverState) {
+            // if the current state is INITIALIZED, then just destroy
+            case INITIALIZED: {
+                doDestroy();
+                break;
+            }
+            // if the current state is STOPPED, then again just destroy
+            case STOPPED: {
+                doDestroy();
+                break;
+            }
+            // if the current state is STARTED, then stop and destroy
+            case STARTED: {
+                stop();
+                doDestroy();
+                break;
+            }
+        }
+
+        // clear the instance parameters
+        this.synapseController = null;
+        this.contextInformation = null;
+        this.configurationInformation = null;
+        
+        this.initialized = false;
+        return ServerState.UNDETERMINED;
+    }
+
+    /**
+     * Starts the system, if the system is initialized, and if not a Runtime exception of type
+     * {@link org.apache.synapse.SynapseException} will be thrown
      *
-     * @return ServerState - The state of the server after call this operation
+     * @return the state of the server after starting, for a successful start
+     *          {@link org.apache.synapse.ServerState#STARTED}
      */
-    public ServerState start() {
+    public synchronized ServerState start() {
+
+        // if the system is not initialized we are not happy
         assertInitialized();
-        doInit();
+        
+        // starts the system
         doStart();
         return this.contextInformation.getServerState();
     }
 
     /**
-     * Stopping the server
+     * Stops the system, if it is started and if not a Runtime exception of type
+     * {@link org.apache.synapse.SynapseException} will be thrown
      *
-     * @return ServerState - The state of the server after call this operation
+     * @return the state of the system after stopping, which is
+     *          {@link org.apache.synapse.ServerState#STOPPED} for a successful stopping
      */
-    public ServerState stop() {
-        assertInitialized();
-        doStop();
+    public synchronized ServerState stop() {
+
+        ServerState serverState = ServerStateDetectionStrategy.currentState(contextInformation,
+                configurationInformation);
+
+        // if the system is started then stop if not we are not happy
+        if (serverState == ServerState.STARTED) {
+            doStop();
+        } else {
+            String message = "Couldn't stop the ServerManager, it has not been started yet";
+            handleException(message);
+        }
+        
         return this.contextInformation.getServerState();
     }
 
     /**
-     * Returns the ServerConfigurationInformation
+     * Returns the ServerConfigurationInformation, if the system is initialized and if not a
+     * Runtime exception of type {@link org.apache.synapse.SynapseException} will be thrown
      *
-     * @return ServerConfigurationInformation insatnce
+     * @return the configuration information of the initialized system
      */
     public ServerConfigurationInformation getConfigurationInformation() {
         assertInitialized();
@@ -112,9 +186,10 @@
     }
 
     /**
-     * Retunrs the ServerContextInformation
+     * Retunrs the ServerContextInformation, if the system is initialized and if not a Runtime
+     * Exception of type {@link org.apache.synapse.SynapseException} will be thrown
      *
-     * @return ServerContextInformation instance
+     * @return the context information of the initialized system
      */
     public ServerContextInformation getContextInformation() {
         assertInitialized();
@@ -122,44 +197,92 @@
     }
 
     /**
-     * Helper method to do the initialization
+     * Has server manager been initialized ?
+     *
+     * @return true if the server manager has been initialized by given required
+     *         configuration information
+     */
+    public boolean isInitialized() {
+        return initialized;
+    }
+
+    /**
+     * Helper method for initializing the ServerManager
      */
     private void doInit() {
 
         ServerState serverState = ServerStateDetectionStrategy.currentState(contextInformation,
                 configurationInformation);
 
+        // if the server is ready for the initialization, this will make sure that we are not
+        // calling the initialization on an already initialized/started system
         if (serverState == ServerState.INITIALIZABLE) {
 
+            // register the ServerManager MBean
+            registerMBean();
+
+            // initializes the SynapseController
             this.synapseController.init(configurationInformation, contextInformation);
 
+            // sets the server context and the controller context
             if (this.contextInformation == null) {
                 this.contextInformation = new ServerContextInformation(
                         this.synapseController.getContext());
             } else if (this.contextInformation.getServerContext() == null) {
                 this.contextInformation.setServerContext(this.synapseController.getContext());
             }
+
+            // mark as initialized
             chanageState(ServerState.INITIALIZED);
         } else {
+            // if the server cannot be initialized just set the current state as the server state
+            chanageState(serverState);
+        }
+    }
+
+    /**
+     * Helper method for destroying the ServerManager
+     */
+    private void doDestroy() {
+        ServerState serverState = ServerStateDetectionStrategy.currentState(contextInformation,
+                configurationInformation);
+
+        if (serverState == ServerState.INITIALIZED || serverState == ServerState.STOPPED) {
+            // destroy the SynapseController
+            synapseController.destroy();
+
+            // unregister the ServerManager MBean
+            unRegisterMBean();
+
+            // mark as destroyed
+            chanageState(ServerState.UNDETERMINED);
+        } else {
+            // if the server cannot be destroyed just set the current state as the server state
             chanageState(serverState);
         }
     }
 
     /**
-     * Helper method that to do start
+     * Helper method to start the ServerManager
      */
     private void doStart() {
 
         ServerState serverState = ServerStateDetectionStrategy.currentState(contextInformation,
                 configurationInformation);
 
-        if (serverState == ServerState.INITIALIZED) {
+        if (serverState == ServerState.INITIALIZED || serverState == ServerState.STOPPED) {
+
+            // creates the Synapse Configuration using the SynapseController
+            synapseController.createSynapseConfiguration();
+            // creates the Synapse Environment using the SynapseController
+            synapseController.createSynapseEnvironment();
+            // starts the SynapseController
+            synapseController.start();
 
-            this.synapseController.createSynapseConfiguration();
-            this.synapseController.createSynapseEnvironment();
             chanageState(ServerState.STARTED);
-            log.info("Ready for processing");
+            log.info("Server ready for processing...");
         } else {
+            // if the server cannot be started just set the current state as the server state
             chanageState(serverState);
         }
     }
@@ -172,20 +295,20 @@
         ServerState serverState = ServerStateDetectionStrategy.currentState(contextInformation,
                 configurationInformation);
 
-        switch (serverState) {
-            case INITIALIZED: {
-                this.synapseController.destroy();
-                break;
-            }
-            case STARTED: {
-                this.synapseController.destroySynapseConfiguration();
-                this.synapseController.destroySynapseEnvironment();
-                this.synapseController.destroy();
-                break;
-            }
+        if (serverState == ServerState.STARTED) {
+
+            // stop the SynapseController
+            synapseController.stop();
+            // destroy the created Synapse Environment
+            synapseController.destroySynapseEnvironment();
+            // destroy the created Synapse Configuration
+            synapseController.destroySynapseConfiguration();
+            
+            chanageState(ServerState.STOPPED);
+        } else {
+            // if the server cannot be stopped just set the current state as the server state
+            chanageState(serverState);
         }
-        chanageState(ServerState.STOPPED);
-        this.initialized = false;
     }
 
     private void chanageState(ServerState serverState) {
@@ -196,24 +319,23 @@
         if (!initialized) {
             String msg = "Server manager has not been initialized, it requires to be " +
                     "initialized, with the required configurations before starting";
-            log.error(msg);
-            throw new SynapseException(msg);
+            handleException(msg);
         }
     }
 
+    private void handleException(String msg) {
+        log.error(msg);
+        throw new SynapseException(msg);
+    }
+
     private void registerMBean() {
         MBeanRegistrar.getInstance().registerMBean(new ServerManagerView(),
-                "ServerManager", "ServerManager");
+                SynapseConstants.SERVER_MANAGER_MBEAN, SynapseConstants.SERVER_MANAGER_MBEAN);
     }
 
-    /**
-     * Has server manager  been initialized ?
-     *
-     * @return true if the server manager has been initialized by given required
-     *         configuration information
-     */
-    public boolean isInitialized() {
-        return initialized;
+    private void unRegisterMBean() {
+        MBeanRegistrar.getInstance().unRegisterMBean(
+                SynapseConstants.SERVER_MANAGER_MBEAN, SynapseConstants.SERVER_MANAGER_MBEAN);
     }
 }
 

Modified: synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/SynapseConstants.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/SynapseConstants.java?rev=762932&r1=762931&r2=762932&view=diff
==============================================================================
--- synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/SynapseConstants.java (original)
+++ synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/SynapseConstants.java Tue Apr  7 20:10:31 2009
@@ -98,6 +98,8 @@
     public static final String RAMPART_OUT_POLICY = "rampartOutPolicy";
     /** The Axis2 client options property name for the Sandesha policy */
 	public static final String SANDESHA_POLICY = "sandeshaPolicy";
+    /** ServerManager MBean category and id */
+    public static final String SERVER_MANAGER_MBEAN = "ServerManager";
 
     /** Parameter names in the axis2.xml that can be used to configure the synapse */
     public static final class Axis2Param {

Modified: synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/SynapseController.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/SynapseController.java?rev=762932&r1=762931&r2=762932&view=diff
==============================================================================
--- synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/SynapseController.java (original)
+++ synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/SynapseController.java Tue Apr  7 20:10:31 2009
@@ -23,7 +23,7 @@
 
 /**
  * The controller for synapse
- * Create and Destroy synapse artifacts in a particular environment
+ * Create, Start, Stop and Destroy synapse artifacts in a particular environment
  */
 public interface SynapseController {
 
@@ -49,6 +49,16 @@
     public boolean isInitialized();
 
     /**
+     * Starts the synapse controller in turn the synapse server.
+     */
+    public void start();
+
+    /**
+     * Stops the synapse controller in turn the synapse server.
+     */
+    public void stop();
+
+    /**
      * Create the SynapseEnvironment instance
      *
      * @return SynapseEnvironment instance if success

Modified: synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/SynapseServer.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/SynapseServer.java?rev=762932&r1=762931&r2=762932&view=diff
==============================================================================
--- synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/SynapseServer.java (original)
+++ synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/SynapseServer.java Tue Apr  7 20:10:31 2009
@@ -53,29 +53,45 @@
             printUsage();
         }
 
+        log.info("Starting Apache Synapse...");
+
+        // create the server configuration using the commandline arguments
         ServerConfigurationInformation configurationInformation =
                 ServerConfigurationInformationFactory.createServerConfigurationInformation(args);
+
         ServerManager serverManager = ServerManager.getInstance();
         serverManager.init(configurationInformation, null);
-        serverManager.start();
-        addShutdownHook();
-        
-        // Put the main thread into wait state. This makes sure that the Synapse server
-        // doesn't stop immediately if ServerManager#start doesn't create any non daemon
-        // threads (see also SYNAPSE-425).
-        new CountDownLatch(1).await();
+
+        try {
+            serverManager.start();
+            addShutdownHook();
+            log.info("Apache Synapse started successfully");
+
+            // Put the main thread into wait state. This makes sure that the Synapse server
+            // doesn't stop immediately if ServerManager#start doesn't create any non daemon
+            // threads (see also SYNAPSE-425).
+            new CountDownLatch(1).await();
+
+        } catch (SynapseException e) {
+            log.error("Error starting Apache Synapse, trying a clean shutdown...", e);
+            serverManager.stop();
+            serverManager.destroy();
+        }
     }
 
     private static void addShutdownHook() {
         Thread shutdownHook = new Thread() {
             public void run() {
-                log.info("Shutting down Apache Synapse ...");
+                log.info("Shutting down Apache Synapse...");
                 try {
-                    ServerManager.getInstance().stop();
-                    log.info("Shutdown complete");
+                    ServerManager serverManager = ServerManager.getInstance();
+                    serverManager.stop();
+                    serverManager.destroy();
+                    log.info("Apache Synapse shutdown complete");
                     log.info("Halting JVM");
                 } catch (Exception e) {
-                    log.warn("Error occurred while shutting down Apache Synapse : " + e);
+                    log.error("Error occurred while shutting down Apache Synapse, " +
+                            "it may not be a clean shutdown", e);
                 }
             }
         };

Modified: synapse/trunk/java/modules/tasks/src/main/java/org/apache/synapse/task/TaskScheduler.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/tasks/src/main/java/org/apache/synapse/task/TaskScheduler.java?rev=762932&r1=762931&r2=762932&view=diff
==============================================================================
--- synapse/trunk/java/modules/tasks/src/main/java/org/apache/synapse/task/TaskScheduler.java (original)
+++ synapse/trunk/java/modules/tasks/src/main/java/org/apache/synapse/task/TaskScheduler.java Tue Apr  7 20:10:31 2009
@@ -112,7 +112,7 @@
      */
     public void start() {
 
-        validateInit();
+        assertInitialized();
         try {
             if (!scheduler.isStarted()) {
 
@@ -136,8 +136,8 @@
     public void scheduleTask(TaskDescription taskDescription, Map<String,
             Object> resources, Class jobClass) {
 
-        validateInit();
-        validateStart();
+        assertInitialized();
+        assertStarted();
 
         if (taskDescription == null) {
             throw new SynapseTaskException("Task Description can not be found", log);
@@ -191,17 +191,19 @@
      */
     public void shutDown() {
 
-        validateInit();
-        validateStart();
+        if (isInitialized()) {
 
-        try {
-            if (log.isDebugEnabled()) {
-                log.debug("ShutingDown Scheduler : " + scheduler.getMetaData());
+            try {
+                if (scheduler != null && scheduler.isStarted()) {
+                    if (log.isDebugEnabled()) {
+                        log.debug("ShutingDown Task Scheduler : " + scheduler.getMetaData());
+                    }
+                    scheduler.shutdown();
+                }
+                initialized = false;
+            } catch (SchedulerException e) {
+                throw new SynapseTaskException("Error ShutingDown task scheduler ", e, log);
             }
-            scheduler.shutdown();
-            initialized = false;
-        } catch (SchedulerException e) {
-            throw new SynapseTaskException("Error shutingDown scheduler ", e, log);
         }
     }
 
@@ -221,8 +223,8 @@
      */
     public void deleteTask(String name, String group) {
 
-        validateInit();
-        validateStart();
+        assertInitialized();
+        assertStarted();
 
         if (name == null || "".equals(name)) {
             throw new SynapseTaskException("Task Name can not be null", log);
@@ -277,14 +279,14 @@
                 append(name).append("]").append(schedulerMetaData).append(" ]").toString();
     }
 
-    private void validateInit() {
+    private void assertInitialized() {
 
         if (!initialized) {
             throw new SynapseTaskException("Scheduler has not been initialled yet", log);
         }
     }
 
-    private void validateStart() {
+    private void assertStarted() {
 
         try {
             if (!scheduler.isStarted()) {