You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@synapse.apache.org by ve...@apache.org on 2008/06/24 13:13:49 UTC

svn commit: r671129 - in /synapse/trunk/java/modules: core/src/test/java/org/apache/synapse/n2n/ transports/src/main/java/org/apache/synapse/transport/nhttp/ transports/src/test/java/org/apache/synapse/transport/

Author: veithen
Date: Tue Jun 24 04:13:49 2008
New Revision: 671129

URL: http://svn.apache.org/viewvc?rev=671129&view=rev
Log:
Modified HttpCoreNIOListener such that when the start() method returns, it is guaranteed that the listener is ready to accept requests. This was not the case previously because the whole startup procedure was executed in a separate thread. Now the initialization is done in the thread executing the start method and only the I/O reactor's execute method is executed in a separate thread. After starting this thread, start() waits for the endpoint to become ready (using ListenerEndpoint#waitFor) so that the postcondition of the start method is that the listener accepts requests. This change allows unit tests to have a more predictable behavior. It also allows the listener to report problems during startup back to the listener manager.

Modified:
    synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/n2n/SynapseCommodityServiceTest.java
    synapse/trunk/java/modules/transports/src/main/java/org/apache/synapse/transport/nhttp/HttpCoreNIOListener.java
    synapse/trunk/java/modules/transports/src/test/java/org/apache/synapse/transport/TransportListenerTestTemplate.java

Modified: synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/n2n/SynapseCommodityServiceTest.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/n2n/SynapseCommodityServiceTest.java?rev=671129&r1=671128&r2=671129&view=diff
==============================================================================
--- synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/n2n/SynapseCommodityServiceTest.java (original)
+++ synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/n2n/SynapseCommodityServiceTest.java Tue Jun 24 04:13:49 2008
@@ -142,11 +142,6 @@
     }
 
     public void testN2N() throws Exception {
-        try {
-            Thread.sleep(4000);
-            // ensure that the servers are up before we actually use them
-        } catch (InterruptedException ignore) {}
-        
         // Creating the Simple Commodity Client
         System.getProperties().remove(org.apache.axis2.Constants.AXIS2_CONF);
         ServiceClient businessClient = new ServiceClient(null, null);

Modified: synapse/trunk/java/modules/transports/src/main/java/org/apache/synapse/transport/nhttp/HttpCoreNIOListener.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/transports/src/main/java/org/apache/synapse/transport/nhttp/HttpCoreNIOListener.java?rev=671129&r1=671128&r2=671129&view=diff
==============================================================================
--- synapse/trunk/java/modules/transports/src/main/java/org/apache/synapse/transport/nhttp/HttpCoreNIOListener.java (original)
+++ synapse/trunk/java/modules/transports/src/main/java/org/apache/synapse/transport/nhttp/HttpCoreNIOListener.java Tue Jun 24 04:13:49 2008
@@ -41,6 +41,7 @@
 import org.apache.http.nio.NHttpServiceHandler;
 import org.apache.http.nio.reactor.IOEventDispatch;
 import org.apache.http.nio.reactor.IOReactorExceptionHandler;
+import org.apache.http.nio.reactor.ListenerEndpoint;
 import org.apache.http.params.BasicHttpParams;
 import org.apache.http.params.HttpConnectionParams;
 import org.apache.http.params.HttpParams;
@@ -83,53 +84,6 @@
     /** The ServerHandler */
     private ServerHandler handler = null;
 
-    /**
-     * configure and start the IO reactor on the specified port
-     * @param port port to start the listener on
-     */
-    private void startServerEngine(int port) {
-        HttpParams params = getServerParameters();
-        try {
-            ioReactor = new DefaultListeningIOReactor(
-                NHttpConfiguration.getInstance().getServerIOWorkers(), params);
-
-            ioReactor.setExceptionHandler(new IOReactorExceptionHandler() {
-                public boolean handle(IOException ioException) {
-                    log.warn("System may be unstable: IOReactor encountered a checked exception : " +
-                        ioException.getMessage(), ioException);
-                    return true;
-                }
-
-                public boolean handle(RuntimeException runtimeException) {
-                    log.warn("System may be unstable: IOReactor encountered a runtime exception : " +
-                        runtimeException.getMessage(), runtimeException);
-                    return true;
-                }
-            });
-        } catch (IOException e) {
-            log.error("Error starting the IOReactor", e);
-        }
-
-        handler = new ServerHandler(cfgCtx, params, sslContext != null, metrics);
-        IOEventDispatch ioEventDispatch = getEventDispatch(
-            handler, sslContext, sslIOSessionHandler, params);
-        state = BaseConstants.STARTED;
-        try {
-            if (bindAddress == null) {
-                ioReactor.listen(new InetSocketAddress(port));
-            } else {
-                ioReactor.listen(new InetSocketAddress(
-                    InetAddress.getByName(bindAddress), port));
-            }
-            ioReactor.execute(ioEventDispatch);
-        } catch (InterruptedIOException ex) {
-            log.fatal("Reactor Interrupted");
-        } catch (IOException e) {
-            log.fatal("Encountered an I/O error: " + e.getMessage(), e);
-        }
-        log.info((sslContext == null ? "HTTP" : "HTTPS") + " Listener Shutdown");
-    }
-
     protected IOEventDispatch getEventDispatch(
         NHttpServiceHandler handler, SSLContext sslContext, 
         SSLIOSessionHandler sslioSessionHandler, HttpParams params) {
@@ -246,26 +200,83 @@
     }
 
     /**
-     * Start the transport listener on a new thread
+     * Start the transport listener. This method returns when the listener is ready to
+     * accept connections.
      * @throws AxisFault
      */
     public void start() throws AxisFault {
         if (log.isDebugEnabled()) {
             log.debug("Starting Listener...");
         }
-        // start the Listener in a new seperate thread
+        
+        // configure the IO reactor on the specified port
+        HttpParams params = getServerParameters();
+        try {
+            ioReactor = new DefaultListeningIOReactor(
+                NHttpConfiguration.getInstance().getServerIOWorkers(), params);
+
+            ioReactor.setExceptionHandler(new IOReactorExceptionHandler() {
+                public boolean handle(IOException ioException) {
+                    log.warn("System may be unstable: IOReactor encountered a checked exception : " +
+                        ioException.getMessage(), ioException);
+                    return true;
+                }
+
+                public boolean handle(RuntimeException runtimeException) {
+                    log.warn("System may be unstable: IOReactor encountered a runtime exception : " +
+                        runtimeException.getMessage(), runtimeException);
+                    return true;
+                }
+            });
+        } catch (IOException e) {
+            handleException("Error starting the IOReactor", e);
+        }
+
+        handler = new ServerHandler(cfgCtx, params, sslContext != null, metrics);
+        final IOEventDispatch ioEventDispatch = getEventDispatch(
+            handler, sslContext, sslIOSessionHandler, params);
+        state = BaseConstants.STARTED;
+        
+        ListenerEndpoint endpoint;
+        try {
+            if (bindAddress == null) {
+                endpoint = ioReactor.listen(new InetSocketAddress(port));
+            } else {
+                endpoint = ioReactor.listen(new InetSocketAddress(
+                    InetAddress.getByName(bindAddress), port));
+            }
+        } catch (IOException e) {
+            handleException("Encountered an I/O error: " + e.getMessage(), e);
+            return;
+        }
+        
+        // start the IO reactor in a new separate thread
         Thread t = new Thread(new Runnable() {
             public void run() {
                 try {
-                    startServerEngine(port);
+                    ioReactor.execute(ioEventDispatch);
+                } catch (InterruptedIOException ex) {
+                    log.fatal("Reactor Interrupted");
+                } catch (IOException e) {
+                    log.fatal("Encountered an I/O error: " + e.getMessage(), e);
                 } catch (Exception e) {
-                    e.printStackTrace();
+                    log.fatal("Unexpected exception in I/O reactor", e);
                 }
+                log.info((sslContext == null ? "HTTP" : "HTTPS") + " Listener Shutdown");
             }
         }, "HttpCoreNIOListener");
 
         t.start();
-        log.info((sslContext == null ? "HTTP" : "HTTPS") + " Listener starting on" +
+        
+        // Wait for the endpoint to become ready, i.e. for the listener to start accepting
+        // requests.
+        try {
+            endpoint.waitFor();
+        } catch (InterruptedException e) {
+            log.warn("HttpCoreNIOListener#start() was interrupted");
+        }
+        
+        log.info((sslContext == null ? "HTTP" : "HTTPS") + " Listener started on" +
             (bindAddress != null ? " address : " + bindAddress : "") + " port : " + port);
     }
 

Modified: synapse/trunk/java/modules/transports/src/test/java/org/apache/synapse/transport/TransportListenerTestTemplate.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/transports/src/test/java/org/apache/synapse/transport/TransportListenerTestTemplate.java?rev=671129&r1=671128&r2=671129&view=diff
==============================================================================
--- synapse/trunk/java/modules/transports/src/test/java/org/apache/synapse/transport/TransportListenerTestTemplate.java (original)
+++ synapse/trunk/java/modules/transports/src/test/java/org/apache/synapse/transport/TransportListenerTestTemplate.java Tue Jun 24 04:13:49 2008
@@ -113,7 +113,6 @@
         // Run the test.
         beforeStartup();
         server.start();
-        Thread.sleep(100); // TODO: this is required for the NIO transport; check whether this is a bug
         try {
             EndpointReference[] endpointReferences
                 = trpInDesc.getReceiver().getEPRsForService(service.getName(), "localhost");