camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r808400 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/ main/java/org/apache/camel/impl/ main/java/org/apache/camel/management/ test/java/org/apache/camel/management/
Date Thu, 27 Aug 2009 12:32:46 GMT
Author: davsclaus
Date: Thu Aug 27 12:32:45 2009
New Revision: 808400

URL: http://svn.apache.org/viewvc?rev=808400&view=rev
Log:
CAMEL-1933: Overhaul of JMX. Improved restarting of routes to properly manage mbeans.

Added:
    camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedRouteStopAndStartCleanupTest.java
      - copied, changed from r808328, camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedRouteStopAndStartTest.java
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/Route.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultRoute.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/impl/RouteService.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/management/DefaultInstrumentationAgent.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/management/DefaultManagedLifecycleStrategy.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedRouteStopAndStartTest.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/Route.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/Route.java?rev=808400&r1=808399&r2=808400&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/Route.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/Route.java Thu Aug 27 12:32:45 2009
@@ -38,13 +38,6 @@
     Endpoint getEndpoint();
 
     /**
-     * Sets the inbound endpoint
-     *
-     * @param endpoint the endpoint
-     */
-    void setEndpoint(Endpoint endpoint);
-
-    /**
      * This property map is used to associate information about the route.
      *
      * @return properties
@@ -77,13 +70,6 @@
     List<Service> getServices();
 
     /**
-     * Sets the sources for this route
-     *
-     * @param services the services
-     */
-    void setServices(List<Service> services);
-
-    /**
      * Adds a service to this route
      *
      * @param service the service

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java?rev=808400&r1=808399&r2=808400&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java Thu
Aug 27 12:32:45 2009
@@ -1001,10 +1001,17 @@
      */
     protected synchronized void startRouteService(RouteService routeService) throws Exception
{
         String key = routeService.getId();
-        stopRoute(key);
-        routeServices.put(key, routeService);
-        if (shouldStartRoutes()) {
-            routeService.start();
+        ServiceStatus status = getRouteStatus(key);
+
+        if (status != null && status.isStarted()) {
+            // already started, then stop it
+            LOG.debug("Route " + key + " is already started");
+            return;
+        } else {
+            routeServices.put(key, routeService);
+            if (shouldStartRoutes()) {
+                routeService.start();
+            }
         }
     }
 

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultRoute.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultRoute.java?rev=808400&r1=808399&r2=808400&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultRoute.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultRoute.java Thu Aug 27
12:32:45 2009
@@ -21,7 +21,6 @@
 import java.util.List;
 import java.util.Map;
 
-import org.apache.camel.Channel;
 import org.apache.camel.Endpoint;
 import org.apache.camel.Route;
 import org.apache.camel.Service;
@@ -33,12 +32,11 @@
  *
  * @version $Revision$
  */
-public abstract class DefaultRoute implements Route {
+public abstract class DefaultRoute extends ServiceSupport implements Route {
 
+    private final Endpoint endpoint;
     private final Map<String, Object> properties = new HashMap<String, Object>();
-    private Endpoint endpoint;
-    private List<Service> services = new ArrayList<Service>();
-    private List<Channel> channels = new ArrayList<Channel>();
+    private final List<Service> services = new ArrayList<Service>();
 
     public DefaultRoute(Endpoint endpoint) {
         this.endpoint = endpoint;
@@ -53,7 +51,7 @@
 
     @Override
     public String toString() {
-        return "Route";
+        return "Route " + getId();
     }
 
     public String getId() {
@@ -64,18 +62,6 @@
         return endpoint;
     }
 
-    public void setEndpoint(Endpoint endpoint) {
-        this.endpoint = endpoint;
-    }
-
-    public List<Channel> getChannels() {
-        return channels;
-    }
-
-    public void setChannels(List<Channel> channels) {
-        this.channels = channels;
-    }
-
     public Map<String, Object> getProperties() {
         return properties;
     }
@@ -94,10 +80,6 @@
         return services;
     }
 
-    public void setServices(List<Service> services) {
-        this.services = services;
-    }
-
     public void addService(Service service) {
         getServices().add(service);
     }
@@ -108,4 +90,12 @@
     protected void addServices(List<Service> services) throws Exception {
     }
 
+    protected void doStart() throws Exception {
+    }
+
+    protected void doStop() throws Exception {
+        // clear services when stopping
+        services.clear();
+    }
+
 }

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/RouteService.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/RouteService.java?rev=808400&r1=808399&r2=808400&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/RouteService.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/RouteService.java Thu Aug 27
12:32:45 2009
@@ -100,6 +100,9 @@
             }
             startChildService(list);
 
+            // start the route itself
+            ServiceHelper.startService(route);
+
             // fire event
             EventHelper.notifyRouteStarted(camelContext, route);
         }
@@ -124,6 +127,9 @@
             }
             stopChildService(list);
 
+            // stop the route itself
+            ServiceHelper.stopService(route);
+
             // fire event
             EventHelper.notifyRouteStopped(camelContext, route);
         }

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/management/DefaultInstrumentationAgent.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/management/DefaultInstrumentationAgent.java?rev=808400&r1=808399&r2=808400&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/management/DefaultInstrumentationAgent.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/management/DefaultInstrumentationAgent.java
Thu Aug 27 12:32:45 2009
@@ -214,6 +214,9 @@
     public void unregister(ObjectName name) throws JMException {
         server.unregisterMBean(name);
         mbeansRegistered.remove(name);
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("Unregistered MBean with objectname: " + name);
+        }
     }
 
     public boolean isRegistered(ObjectName name) {

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/management/DefaultManagedLifecycleStrategy.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/management/DefaultManagedLifecycleStrategy.java?rev=808400&r1=808399&r2=808400&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/management/DefaultManagedLifecycleStrategy.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/management/DefaultManagedLifecycleStrategy.java
Thu Aug 27 12:32:45 2009
@@ -246,6 +246,15 @@
             // regular for services
             managedObject = getManagedObjectForService(context, service);
         }
+
+        // skip already managed services, for example if a route has been restarted
+        if (getStrategy().isManaged(managedObject, null)) {
+            if (LOG.isTraceEnabled()) {
+                LOG.trace("The service is already managed: " + service);
+            }
+            return;
+        }
+
         try {
             getStrategy().manageObject(managedObject);
         } catch (Exception e) {
@@ -335,6 +344,14 @@
         for (Route route : routes) {
             ManagedRoute mr = new ManagedRoute(getStrategy(), context, route);
 
+            // skip already managed routes, for example if the route has been restarted
+            if (getStrategy().isManaged(mr, null)) {
+                if (LOG.isTraceEnabled()) {
+                    LOG.trace("The route is already managed: " + route);
+                }
+                continue;
+            }
+
             // get the wrapped instrumentation processor from this route
             // and set me as the counter
             if (route instanceof EventDrivenConsumerRoute) {
@@ -357,12 +374,7 @@
     }
 
     public void onRoutesRemove(Collection<Route> routes) {
-        // the agent hasn't been started
-        if (!initialized) {
-            return;
-        }
-
-        // keep the route in the mbean so its still there, it will still be unregistered
+        // noop - keep the route in the mbean so its still there, it will still be unregistered
         // when camel itself is shutting down
     }
 

Copied: camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedRouteStopAndStartCleanupTest.java
(from r808328, camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedRouteStopAndStartTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedRouteStopAndStartCleanupTest.java?p2=camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedRouteStopAndStartCleanupTest.java&p1=camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedRouteStopAndStartTest.java&r1=808328&r2=808400&rev=808400&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedRouteStopAndStartTest.java
(original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedRouteStopAndStartCleanupTest.java
Thu Aug 27 12:32:45 2009
@@ -20,23 +20,18 @@
 import javax.management.MBeanServer;
 import javax.management.ObjectName;
 
-import org.apache.camel.ContextTestSupport;
 import org.apache.camel.Exchange;
 import org.apache.camel.ServiceStatus;
-import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.mock.MockEndpoint;
 
 /**
+ * Extended test to see if mbeans is removed and stats are correct
+ *
  * @version $Revision$
  */
-public class ManagedRouteStopAndStartTest extends ContextTestSupport {
-
-    @Override
-    protected void setUp() throws Exception {
-        deleteDirectory("target/managed");
-        super.setUp();
-    }
+public class ManagedRouteStopAndStartCleanupTest extends ManagedRouteStopAndStartTest {
 
+    @SuppressWarnings("unchecked")
     public void testStopAndStartRoute() throws Exception {
         MBeanServer mbeanServer = context.getManagementStrategy().getManagementAgent().getMBeanServer();
         ObjectName on = getRouteObjectName(mbeanServer);
@@ -52,12 +47,33 @@
         String state = (String) mbeanServer.getAttribute(on, "State");
         assertEquals("Should be started", ServiceStatus.Started.name(), state);
 
+        // need a bit time to let JMX update
+        Thread.sleep(1000);
+
+        // should have 1 completed exchange
+        Long completed = (Long) mbeanServer.getAttribute(on, "ExchangesCompleted");
+        assertEquals(1, completed.longValue());
+
+        // should be 1 consumer and 1 processor
+        Set<ObjectName> set = mbeanServer.queryNames(new ObjectName("*:type=consumers,*"),
null);
+        assertEquals("Should be 1 consumer", 1, set.size());
+
+        set = mbeanServer.queryNames(new ObjectName("*:type=processors,*"), null);
+        assertEquals("Should be 1 processor", 1, set.size());
+
         // stop
         mbeanServer.invoke(on, "stop", null, null);
 
         state = (String) mbeanServer.getAttribute(on, "State");
         assertEquals("Should be stopped", ServiceStatus.Stopped.name(), state);
 
+        // should be 0 consumer and 0 processor
+        set = mbeanServer.queryNames(new ObjectName("*:type=consumers,*"), null);
+        assertEquals("Should be 0 consumer", 0, set.size());
+
+        set = mbeanServer.queryNames(new ObjectName("*:type=processors,*"), null);
+        assertEquals("Should be 0 processor", 0, set.size());
+
         mock.reset();
         mock.expectedBodiesReceived("Bye World");
         // wait 3 seconds while route is stopped to verify that file was not consumed
@@ -78,25 +94,22 @@
         state = (String) mbeanServer.getAttribute(on, "State");
         assertEquals("Should be started", ServiceStatus.Started.name(), state);
 
+        // should be 1 consumer and 1 processor
+        set = mbeanServer.queryNames(new ObjectName("*:type=consumers,*"), null);
+        assertEquals("Should be 1 consumer", 1, set.size());
+
+        set = mbeanServer.queryNames(new ObjectName("*:type=processors,*"), null);
+        assertEquals("Should be 1 processor", 1, set.size());
+
         // this time the file is consumed
         mock.assertIsSatisfied();
-    }
-
-    private static ObjectName getRouteObjectName(MBeanServer mbeanServer) throws Exception
{
-        Set<ObjectName> set = mbeanServer.queryNames(new ObjectName("*:type=routes,*"),
null);
-        assertEquals(1, set.size());
 
-        return set.iterator().next();
-    }
+        // need a bit time to let JMX update
+        Thread.sleep(1000);
 
-    @Override
-    protected RouteBuilder createRouteBuilder() throws Exception {
-        return new RouteBuilder() {
-            @Override
-            public void configure() throws Exception {
-                from("file://target/managed").to("mock:result");
-            }
-        };
+        // should have 2 completed exchange
+        completed = (Long) mbeanServer.getAttribute(on, "ExchangesCompleted");
+        assertEquals(2, completed.longValue());
     }
 
-}
+}
\ No newline at end of file

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedRouteStopAndStartTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedRouteStopAndStartTest.java?rev=808400&r1=808399&r2=808400&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedRouteStopAndStartTest.java
(original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedRouteStopAndStartTest.java
Thu Aug 27 12:32:45 2009
@@ -82,7 +82,7 @@
         mock.assertIsSatisfied();
     }
 
-    private static ObjectName getRouteObjectName(MBeanServer mbeanServer) throws Exception
{
+    static ObjectName getRouteObjectName(MBeanServer mbeanServer) throws Exception {
         Set<ObjectName> set = mbeanServer.queryNames(new ObjectName("*:type=routes,*"),
null);
         assertEquals(1, set.size());
 



Mime
View raw message