You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by js...@apache.org on 2007/04/25 12:04:33 UTC

svn commit: r532289 - in /incubator/tuscany/java/sca/modules: core/src/main/java/org/apache/tuscany/core/runtime/ http-jetty/src/main/java/org/apache/tuscany/http/jetty/ http-jetty/src/main/java/org/apache/tuscany/http/jetty/module/ http-jetty/src/test...

Author: jsdelfino
Date: Wed Apr 25 03:04:31 2007
New Revision: 532289

URL: http://svn.apache.org/viewvc?view=rev&rev=532289
Log:
Shutdown our WorkManager when the runtime gets stopped, to avoid leaking threads and the resources they hold, including open TCP/IP sockets. Changed the Jetty and Tomcat embedded servers to use our WorkScheduler. Changed the start order of the Tomcat engine, connector and registration of servlet mapping to fix connection refused and page not found exceptions.

Modified:
    incubator/tuscany/java/sca/modules/core/src/main/java/org/apache/tuscany/core/runtime/AbstractRuntime.java
    incubator/tuscany/java/sca/modules/http-jetty/src/main/java/org/apache/tuscany/http/jetty/JettyServer.java
    incubator/tuscany/java/sca/modules/http-jetty/src/main/java/org/apache/tuscany/http/jetty/module/JettyRuntimeModuleActivator.java
    incubator/tuscany/java/sca/modules/http-jetty/src/test/java/org/apache/tuscany/http/jetty/JettyServerTestCase.java
    incubator/tuscany/java/sca/modules/http-tomcat/src/main/java/org/apache/tuscany/http/tomcat/TomcatServer.java
    incubator/tuscany/java/sca/modules/http-tomcat/src/main/java/org/apache/tuscany/http/tomcat/module/TomcatRuntimeModuleActivator.java
    incubator/tuscany/java/sca/modules/http-tomcat/src/test/java/org/apache/tuscany/http/tomcat/TomcatServerTestCase.java

Modified: incubator/tuscany/java/sca/modules/core/src/main/java/org/apache/tuscany/core/runtime/AbstractRuntime.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/core/src/main/java/org/apache/tuscany/core/runtime/AbstractRuntime.java?view=diff&rev=532289&r1=532288&r2=532289
==============================================================================
--- incubator/tuscany/java/sca/modules/core/src/main/java/org/apache/tuscany/core/runtime/AbstractRuntime.java (original)
+++ incubator/tuscany/java/sca/modules/core/src/main/java/org/apache/tuscany/core/runtime/AbstractRuntime.java Wed Apr 25 03:04:31 2007
@@ -58,8 +58,6 @@
 import org.apache.tuscany.spi.services.work.WorkScheduler;
 import org.osoa.sca.ComponentContext;
 
-import commonj.work.WorkManager;
-
 /**
  * @version $Rev$ $Date$
  */
@@ -104,6 +102,8 @@
 
     protected ScopeRegistry scopeRegistry;
     protected Collection<ModuleActivator> activators;
+    
+    protected ThreadPoolWorkManager workManager;
 
     protected AbstractRuntime(Class<I> runtimeInfoType) {
         this(runtimeInfoType, new NullMonitorFactory());
@@ -178,7 +178,7 @@
         extensionRegistry.addExtensionPoint(ContributionService.class, contributionService);
 
         extensionRegistry.addExtensionPoint(Deployer.class, deployer);
-        WorkManager workManager = new ThreadPoolWorkManager(10);
+        workManager = new ThreadPoolWorkManager(10);
         extensionRegistry.addExtensionPoint(WorkScheduler.class, new Jsr237WorkScheduler(workManager)); //lresende
 
         this.scopeRegistry = bootstrapper.getScopeRegistry();
@@ -212,6 +212,7 @@
             systemComponent.stop();
             systemComponent = null;
         }
+        workManager.destroy();
     }
 
     public ComponentContext getComponentContext(URI componentName) {

Modified: incubator/tuscany/java/sca/modules/http-jetty/src/main/java/org/apache/tuscany/http/jetty/JettyServer.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/http-jetty/src/main/java/org/apache/tuscany/http/jetty/JettyServer.java?view=diff&rev=532289&r1=532288&r2=532289
==============================================================================
--- incubator/tuscany/java/sca/modules/http-jetty/src/main/java/org/apache/tuscany/http/jetty/JettyServer.java (original)
+++ incubator/tuscany/java/sca/modules/http-jetty/src/main/java/org/apache/tuscany/http/jetty/JettyServer.java Wed Apr 25 03:04:31 2007
@@ -27,6 +27,7 @@
 
 import org.apache.tuscany.http.ServletHostExtension;
 import org.apache.tuscany.http.ServletMappingException;
+import org.apache.tuscany.spi.services.work.WorkScheduler;
 import org.mortbay.jetty.Connector;
 import org.mortbay.jetty.Server;
 import org.mortbay.jetty.handler.ContextHandler;
@@ -38,7 +39,7 @@
 import org.mortbay.jetty.servlet.SessionHandler;
 import org.mortbay.log.Log;
 import org.mortbay.log.Logger;
-import org.mortbay.thread.BoundedThreadPool;
+import org.mortbay.thread.ThreadPool;
 
 /**
  * Implements an HTTP transport service using Jetty.
@@ -68,6 +69,7 @@
     private Server server;
     private Connector connector;
     private ServletHandler servletHandler;
+    private WorkScheduler workScheduler;
 
     static {
         // hack to replace the static Jetty logger
@@ -75,7 +77,8 @@
 
     }
 
-    public JettyServer() {
+    public JettyServer(WorkScheduler workScheduler) {
+        this.workScheduler = workScheduler;
 
         // Configure the Jetty logger
         Logger logger = Log.getLogger(null);
@@ -135,9 +138,7 @@
 
             try {
                 server = new Server();
-                BoundedThreadPool threadPool = new BoundedThreadPool();
-                threadPool.setMaxThreads(100);
-                server.setThreadPool(threadPool);
+                server.setThreadPool(new WorkSchedulerThreadPool());
                 if (connector == null) {
                     if (https) {
                         Connector httpConnector = new SelectChannelConnector();
@@ -206,6 +207,62 @@
             servletHandler.setServletMappings((ServletMapping[])mappings.toArray(new ServletMapping[mappings.size()]));
         }
         return removedServlet;
+    }
+
+    /**
+     * An integration wrapper to enable use of a {@link WorkScheduler} with Jetty
+     */
+    private class WorkSchedulerThreadPool implements ThreadPool {
+
+        public boolean dispatch(Runnable work) {
+            workScheduler.scheduleWork(work);
+            return true;
+        }
+
+        public void join() throws InterruptedException {
+            synchronized (joinLock) {
+                joinLock.wait();
+            }
+        }
+
+        public int getThreads() {
+            throw new UnsupportedOperationException();
+        }
+
+        public int getIdleThreads() {
+            throw new UnsupportedOperationException();
+        }
+
+        public boolean isLowOnThreads() {
+            // TODO FIXME
+            return false;
+        }
+
+        public void start() throws Exception {
+        }
+
+        public void stop() throws Exception {
+        }
+
+        public boolean isRunning() {
+            return state == STARTING || state == STARTED;
+        }
+
+        public boolean isStarted() {
+            return state == STARTED;
+        }
+
+        public boolean isStarting() {
+            return state == STARTING;
+        }
+
+        public boolean isStopping() {
+            return state == STOPPING;
+        }
+
+        public boolean isFailed() {
+            return state == ERROR;
+        }
     }
 
 }

Modified: incubator/tuscany/java/sca/modules/http-jetty/src/main/java/org/apache/tuscany/http/jetty/module/JettyRuntimeModuleActivator.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/http-jetty/src/main/java/org/apache/tuscany/http/jetty/module/JettyRuntimeModuleActivator.java?view=diff&rev=532289&r1=532288&r2=532289
==============================================================================
--- incubator/tuscany/java/sca/modules/http-jetty/src/main/java/org/apache/tuscany/http/jetty/module/JettyRuntimeModuleActivator.java (original)
+++ incubator/tuscany/java/sca/modules/http-jetty/src/main/java/org/apache/tuscany/http/jetty/module/JettyRuntimeModuleActivator.java Wed Apr 25 03:04:31 2007
@@ -25,6 +25,7 @@
 import org.apache.tuscany.core.ModuleActivator;
 import org.apache.tuscany.http.ServletHostExtensionPoint;
 import org.apache.tuscany.http.jetty.JettyServer;
+import org.apache.tuscany.spi.services.work.WorkScheduler;
 
 /**
  * @version $Rev$ $Date$
@@ -42,7 +43,8 @@
         // Register a Jetty servlet host
         ServletHostExtensionPoint servletHosts =
             extensionPointRegistry.getExtensionPoint(ServletHostExtensionPoint.class);
-        server = new JettyServer();
+        WorkScheduler workScheduler = extensionPointRegistry.getExtensionPoint(WorkScheduler.class);
+        server = new JettyServer(workScheduler);
         servletHosts.addExtension(server);
         server.init();
     }

Modified: incubator/tuscany/java/sca/modules/http-jetty/src/test/java/org/apache/tuscany/http/jetty/JettyServerTestCase.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/http-jetty/src/test/java/org/apache/tuscany/http/jetty/JettyServerTestCase.java?view=diff&rev=532289&r1=532288&r2=532289
==============================================================================
--- incubator/tuscany/java/sca/modules/http-jetty/src/test/java/org/apache/tuscany/http/jetty/JettyServerTestCase.java (original)
+++ incubator/tuscany/java/sca/modules/http-jetty/src/test/java/org/apache/tuscany/http/jetty/JettyServerTestCase.java Wed Apr 25 03:04:31 2007
@@ -31,6 +31,8 @@
 import javax.servlet.http.HttpServletResponse;
 
 import org.apache.tuscany.http.jetty.JettyServer;
+import org.apache.tuscany.spi.services.work.NotificationListener;
+import org.apache.tuscany.spi.services.work.WorkScheduler;
 
 import junit.framework.TestCase;
 
@@ -50,11 +52,23 @@
 
     private static final int HTTP_PORT = 8585;
 
+    private WorkScheduler workScheduler = new WorkScheduler() {
+        
+        public <T extends Runnable> void scheduleWork(T work) {
+            Thread thread = new Thread(work);
+            thread.start();
+        }
+        
+        public <T extends Runnable> void scheduleWork(T work, NotificationListener<T> listener) {
+            scheduleWork(work);
+        }
+    };
+    
     /**
      * Verifies requests are properly routed according to the servlet mapping
      */
     public void testRegisterServletMapping() throws Exception {
-        JettyServer service = new JettyServer();
+        JettyServer service = new JettyServer(workScheduler);
         service.init();
         TestServlet servlet = new TestServlet();
         service.addServletMapping(HTTP_PORT, "/", servlet);
@@ -68,7 +82,7 @@
     }
 
     public void testUnregisterMapping() throws Exception {
-        JettyServer service = new JettyServer();
+        JettyServer service = new JettyServer(workScheduler);
         service.init();
         TestServlet servlet = new TestServlet();
         service.addServletMapping(HTTP_PORT, "/foo", servlet);
@@ -83,7 +97,7 @@
     }
 
     public void testRequestSession() throws Exception {
-        JettyServer service = new JettyServer();
+        JettyServer service = new JettyServer(workScheduler);
         service.setDebug(true);
         service.init();
         TestServlet servlet = new TestServlet();
@@ -99,7 +113,7 @@
     }
 
     public void testRestart() throws Exception {
-        JettyServer service = new JettyServer();
+        JettyServer service = new JettyServer(workScheduler);
         service.init();
         service.destroy();
         service.init();
@@ -107,7 +121,7 @@
     }
 
     public void testNoMappings() throws Exception {
-        JettyServer service = new JettyServer();
+        JettyServer service = new JettyServer(workScheduler);
         service.init();
         Exception ex = null;
         try {

Modified: incubator/tuscany/java/sca/modules/http-tomcat/src/main/java/org/apache/tuscany/http/tomcat/TomcatServer.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/http-tomcat/src/main/java/org/apache/tuscany/http/tomcat/TomcatServer.java?view=diff&rev=532289&r1=532288&r2=532289
==============================================================================
--- incubator/tuscany/java/sca/modules/http-tomcat/src/main/java/org/apache/tuscany/http/tomcat/TomcatServer.java (original)
+++ incubator/tuscany/java/sca/modules/http-tomcat/src/main/java/org/apache/tuscany/http/tomcat/TomcatServer.java Wed Apr 25 03:04:31 2007
@@ -18,19 +18,24 @@
  */
 package org.apache.tuscany.http.tomcat;
 
+import java.util.concurrent.Executor;
+
 import javax.servlet.Servlet;
 
 import org.apache.catalina.Context;
-import org.apache.catalina.Engine;
-import org.apache.catalina.Host;
-import org.apache.catalina.LifecycleException;
+import org.apache.catalina.Lifecycle;
 import org.apache.catalina.Wrapper;
 import org.apache.catalina.connector.Connector;
-import org.apache.catalina.startup.Embedded;
+import org.apache.catalina.core.StandardContext;
+import org.apache.catalina.core.StandardEngine;
+import org.apache.catalina.core.StandardHost;
+import org.apache.catalina.startup.ContextConfig;
+import org.apache.coyote.http11.Http11NioProtocol;
 import org.apache.tomcat.util.buf.MessageBytes;
 import org.apache.tomcat.util.http.mapper.MappingData;
 import org.apache.tuscany.http.ServletHostExtension;
 import org.apache.tuscany.http.ServletMappingException;
+import org.apache.tuscany.spi.services.work.WorkScheduler;
 
 /**
  * A Tomcat based implementation of ServletHost.
@@ -39,58 +44,91 @@
  */
 public class TomcatServer implements ServletHostExtension {
 
-    private Embedded tomcat;
-    private Host host;
-    Connector connector;
-    private boolean started;
+    private StandardEngine engine;
+    private StandardHost host;
+    private Connector connector;
+    private WorkScheduler workScheduler;
+    
+    /**
+     * A custom connector that uses our WorkScheduler to schedule
+     * worker threads.
+     */
+    private class CustomConnector extends Connector {
+
+        private class CustomHttpProtocolHandler extends Http11NioProtocol {
+        
+            private class WorkSchedulerExecutor implements Executor {
+                public void execute(Runnable command) {
+                    workScheduler.scheduleWork(command);
+                }
+            }
+            
+            CustomHttpProtocolHandler() {
+                ep.setExecutor(new WorkSchedulerExecutor());
+            }
+        }
+        
+        CustomConnector() throws Exception {
+            this.protocolHandler = new CustomHttpProtocolHandler();
+        }
+    }
 
+    public TomcatServer(WorkScheduler workScheduler) {
+        this.workScheduler = workScheduler;
+    }
+    
     public void init() throws ServletMappingException {
-        tomcat = new Embedded();
 
         // Create an engine
-        Engine engine = tomcat.createEngine();
+        engine = new StandardEngine();
+        engine.setBaseDir("");
         engine.setDefaultHost("localhost");
 
         // Create a host
-        host = tomcat.createHost("localhost", "");
+        host = new StandardHost();
+        host.setAppBase("");
+        host.setName("localhost");
         engine.addChild(host);
 
-        // Create the ROOT context
-        Context context = tomcat.createContext("", "");
+        // Create the root context
+        StandardContext context = new StandardContext();
+        context.setDocBase("");
+        context.setPath("");
+        ContextConfig config = new ContextConfig();
+        ((Lifecycle)context).addLifecycleListener(config);
         host.addChild(context);
-
-        // Install the engine
-        tomcat.addEngine(engine);
-
     }
 
     public void destroy() throws ServletMappingException {
 
         // Stop the server
-        if (started) {
-            try {
-                tomcat.stop();
-            } catch (LifecycleException e) {
-                throw new ServletMappingException(e);
+        try {
+            if (connector !=null) {
+                connector.stop();
+                engine.stop();
             }
-            started = false;
+        } catch (Exception e) {
+            throw new ServletMappingException(e);
         }
     }
 
     public void addServletMapping(int port, String mapping, Servlet servlet) {
-
+        
         // Install a default HTTP connector
         if (connector == null) {
             //TODO support multiple connectors on different ports
             try {
-                connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
+                engine.start();
+                connector = new CustomConnector();
                 connector.setPort(port);
+                connector.setContainer(engine);
+                connector.initialize();
+                connector.start();
             } catch (Exception e) {
                 throw new RuntimeException(e);
             }
-            tomcat.addConnector(connector);
         }
-        
+
         // Register the servlet mapping
         Context context = host.map(mapping);
         Wrapper wrapper = new ServletWrapper(servlet);
@@ -98,18 +136,7 @@
         wrapper.addMapping(mapping);
         context.addChild(wrapper);
         context.addServletMapping(mapping, mapping);
-
-        // Start Tomcat
-        try {
-            if (!started) {
-                tomcat.start();
-                started = true;
-            }
-
-        } catch (LifecycleException e) {
-            // TODO use a better runtime exception
-            throw new RuntimeException(e);
-        }
+        connector.getMapper().addWrapper("localhost", "", mapping, wrapper);
     }
 
     public Servlet removeServletMapping(int port, String mapping) {

Modified: incubator/tuscany/java/sca/modules/http-tomcat/src/main/java/org/apache/tuscany/http/tomcat/module/TomcatRuntimeModuleActivator.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/http-tomcat/src/main/java/org/apache/tuscany/http/tomcat/module/TomcatRuntimeModuleActivator.java?view=diff&rev=532289&r1=532288&r2=532289
==============================================================================
--- incubator/tuscany/java/sca/modules/http-tomcat/src/main/java/org/apache/tuscany/http/tomcat/module/TomcatRuntimeModuleActivator.java (original)
+++ incubator/tuscany/java/sca/modules/http-tomcat/src/main/java/org/apache/tuscany/http/tomcat/module/TomcatRuntimeModuleActivator.java Wed Apr 25 03:04:31 2007
@@ -25,6 +25,7 @@
 import org.apache.tuscany.core.ModuleActivator;
 import org.apache.tuscany.http.ServletHostExtensionPoint;
 import org.apache.tuscany.http.tomcat.TomcatServer;
+import org.apache.tuscany.spi.services.work.WorkScheduler;
 
 /**
  * @version $Rev$ $Date$
@@ -42,7 +43,8 @@
         // Register a Tomcat servlet host
         ServletHostExtensionPoint servletHosts =
             extensionPointRegistry.getExtensionPoint(ServletHostExtensionPoint.class);
-        server = new TomcatServer();
+        WorkScheduler workScheduler = extensionPointRegistry.getExtensionPoint(WorkScheduler.class);
+        server = new TomcatServer(workScheduler);
         server.init();
         servletHosts.addExtension(server);
     }

Modified: incubator/tuscany/java/sca/modules/http-tomcat/src/test/java/org/apache/tuscany/http/tomcat/TomcatServerTestCase.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/http-tomcat/src/test/java/org/apache/tuscany/http/tomcat/TomcatServerTestCase.java?view=diff&rev=532289&r1=532288&r2=532289
==============================================================================
--- incubator/tuscany/java/sca/modules/http-tomcat/src/test/java/org/apache/tuscany/http/tomcat/TomcatServerTestCase.java (original)
+++ incubator/tuscany/java/sca/modules/http-tomcat/src/test/java/org/apache/tuscany/http/tomcat/TomcatServerTestCase.java Wed Apr 25 03:04:31 2007
@@ -29,6 +29,9 @@
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
+import org.apache.tuscany.spi.services.work.NotificationListener;
+import org.apache.tuscany.spi.services.work.WorkScheduler;
+
 import junit.framework.TestCase;
 
 /**
@@ -46,12 +49,24 @@
         REQUEST1_HEADER + REQUEST1_CONTENT.getBytes().length + "\n\n" + REQUEST1_CONTENT;
 
     private static final int HTTP_PORT = 8586;
-
+    
+    private WorkScheduler workScheduler = new WorkScheduler() {
+        
+        public <T extends Runnable> void scheduleWork(T work) {
+            Thread thread = new Thread(work);
+            thread.start();
+        }
+        
+        public <T extends Runnable> void scheduleWork(T work, NotificationListener<T> listener) {
+            scheduleWork(work);
+        }
+    };
+    
     /**
      * Verifies requests are properly routed according to the servlet mapping
      */
     public void testRegisterServletMapping() throws Exception {
-        TomcatServer service = new TomcatServer();
+        TomcatServer service = new TomcatServer(workScheduler);
         service.init();
         TestServlet servlet = new TestServlet();
         service.addServletMapping(HTTP_PORT, "/foo", servlet);
@@ -65,7 +80,7 @@
     }
 
     public void testUnregisterMapping() throws Exception {
-        TomcatServer service = new TomcatServer();
+        TomcatServer service = new TomcatServer(workScheduler);
         service.init();
         TestServlet servlet = new TestServlet();
         service.addServletMapping(HTTP_PORT, "/foo", servlet);
@@ -80,7 +95,7 @@
     }
 
     public void testRequestSession() throws Exception {
-        TomcatServer service = new TomcatServer();
+        TomcatServer service = new TomcatServer(workScheduler);
         service.init();
         TestServlet servlet = new TestServlet();
         service.addServletMapping(HTTP_PORT, "/foo", servlet);
@@ -95,7 +110,7 @@
     }
 
     public void testRestart() throws Exception {
-        TomcatServer service = new TomcatServer();
+        TomcatServer service = new TomcatServer(workScheduler);
         service.init();
         service.destroy();
         service.init();
@@ -103,7 +118,7 @@
     }
 
     public void testNoMappings() throws Exception {
-        TomcatServer service = new TomcatServer();
+        TomcatServer service = new TomcatServer(workScheduler);
         service.init();
         Exception ex = null;
         try {



---------------------------------------------------------------------
To unsubscribe, e-mail: tuscany-commits-unsubscribe@ws.apache.org
For additional commands, e-mail: tuscany-commits-help@ws.apache.org