Author: davsclaus Date: Sat Dec 31 13:39:08 2011 New Revision: 1226085 URL: http://svn.apache.org/viewvc?rev=1226085&view=rev Log: CAMEL-4842: Removing route should remove producer cache from JMX, as well from services to close list on CamelContext, to not eat up memory.
Added: camel/branches/camel-2.8.x/camel-core/src/test/java/org/apache/camel/impl/StartStopAndShutdownRouteTest.java - copied unchanged from r1226081, camel/trunk/camel-core/src/test/java/org/apache/camel/impl/StartStopAndShutdownRouteTest.java camel/branches/camel-2.8.x/camel-core/src/test/java/org/apache/camel/management/ManagedRouteAddRemoveTest.java - copied unchanged from r1226081, camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedRouteAddRemoveTest.java camel/branches/camel-2.8.x/camel-core/src/test/java/org/apache/camel/util/MyDummyObject.java - copied unchanged from r1226081, camel/trunk/camel-core/src/test/java/org/apache/camel/util/MyDummyObject.java Modified: camel/branches/camel-2.8.x/ (props changed) camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/CamelContext.java camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/Route.java camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/impl/DefaultRoute.java camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/impl/RouteService.java camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/processor/RecipientList.java camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/processor/RecipientListProcessor.java camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/processor/RoutingSlip.java camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/processor/SendProcessor.java camel/branches/camel-2.8.x/camel-core/src/test/java/org/apache/camel/util/ObjectHelperTest.java Propchange: camel/branches/camel-2.8.x/ ------------------------------------------------------------------------------ --- svn:mergeinfo (original) +++ svn:mergeinfo Sat Dec 31 13:39:08 2011 @@ -1 +1 @@ -/camel/trunk:1212504,1215477,1221565,1224674,1225077-1225078,1225766 +/camel/trunk:1212504,1215477,1221565,1224674,1225077-1225078,1225766,1226081 Propchange: camel/branches/camel-2.8.x/ ------------------------------------------------------------------------------ Binary property 'svnmerge-integrated' - no diff available. Modified: camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/CamelContext.java URL: http://svn.apache.org/viewvc/camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/CamelContext.java?rev=1226085&r1=1226084&r2=1226085&view=diff ============================================================================== --- camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/CamelContext.java (original) +++ camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/CamelContext.java Sat Dec 31 13:39:08 2011 @@ -153,6 +153,18 @@ public interface CamelContext extends Su void addService(Object object) throws Exception; /** + * Removes a service from this context. + * <p/> + * The service is assumed to have been previously added using {@link #addService(Object)} method. + * This method will <b>not</b> change the service lifecycle. + * + * @param object the service + * @throws Exception can be thrown if error removing the service + * @return <tt>true</tt> if the service was removed, <tt>false</tt> if no service existed + */ + boolean removeService(Object object) throws Exception; + + /** * Has the given service already been added to this context? * * @param object the service Modified: camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/Route.java URL: http://svn.apache.org/viewvc/camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/Route.java?rev=1226085&r1=1226084&r2=1226085&view=diff ============================================================================== --- camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/Route.java (original) +++ camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/Route.java Sat Dec 31 13:39:08 2011 @@ -95,4 +95,9 @@ public interface Route { */ Navigate<Processor> navigate(); + /** + * Callback preparing the route to be started, by warming up the route. + */ + void warmUp(); + } Modified: camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java URL: http://svn.apache.org/viewvc/camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java?rev=1226085&r1=1226084&r2=1226085&view=diff ============================================================================== --- camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java (original) +++ camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java Sat Dec 31 13:39:08 2011 @@ -902,6 +902,23 @@ public class DefaultCamelContext extends startServices(object); } + public boolean removeService(Object object) throws Exception { + if (object instanceof Service) { + Service service = (Service) object; + + for (LifecycleStrategy strategy : lifecycleStrategies) { + if (service instanceof Endpoint) { + // use specialized endpoint remove + strategy.onEndpointRemove((Endpoint) service); + } else { + strategy.onServiceRemove(this, service, null); + } + } + return servicesToClose.remove(service); + } + return false; + } + public boolean hasService(Object object) { if (object instanceof Service) { Service service = (Service) object; Modified: camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/impl/DefaultRoute.java URL: http://svn.apache.org/viewvc/camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/impl/DefaultRoute.java?rev=1226085&r1=1226084&r2=1226085&view=diff ============================================================================== --- camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/impl/DefaultRoute.java (original) +++ camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/impl/DefaultRoute.java Sat Dec 31 13:39:08 2011 @@ -80,7 +80,13 @@ public abstract class DefaultRoute exten } public void addService(Service service) { - getServices().add(service); + if (!services.contains(service)) { + services.add(service); + } + } + + public void warmUp() { + getServices().clear(); } /** @@ -94,7 +100,12 @@ public abstract class DefaultRoute exten } protected void doStop() throws Exception { - // clear services when stopping + // noop + } + + @Override + protected void doShutdown() throws Exception { + // clear services when shutting down services.clear(); } Modified: camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/impl/RouteService.java URL: http://svn.apache.org/viewvc/camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/impl/RouteService.java?rev=1226085&r1=1226084&r2=1226085&view=diff ============================================================================== --- camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/impl/RouteService.java (original) +++ camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/impl/RouteService.java Sat Dec 31 13:39:08 2011 @@ -116,8 +116,10 @@ public class RouteService extends Servic if (warmUpDone.compareAndSet(false, true)) { for (Route route : routes) { - LOG.debug("Starting services on route: {}", route.getId()); + // warm up the route first + route.warmUp(); + LOG.debug("Starting services on route: {}", route.getId()); List<Service> services = route.getServices(); // callback that we are staring these services @@ -209,6 +211,20 @@ public class RouteService extends Servic @Override protected void doShutdown() throws Exception { for (Route route : routes) { + LOG.debug("Shutting down services on route: {}", route.getId()); + List<Service> services = route.getServices(); + + // gather list of services to stop as we need to start child services as well + Set<Service> list = new LinkedHashSet<Service>(); + for (Service service : services) { + doGetChildServices(list, service); + } + // shutdown services + stopChildService(route, list, true); + + // shutdown the route itself + ServiceHelper.stopAndShutdownServices(route); + // endpoints should only be stopped when Camel is shutting down // see more details in the warmUp method ServiceHelper.stopAndShutdownServices(route.getEndpoint()); @@ -218,7 +234,8 @@ public class RouteService extends Servic for (LifecycleStrategy strategy : camelContext.getLifecycleStrategies()) { strategy.onRoutesRemove(routes); } - + + // remove the routes from the collections camelContext.removeRouteCollection(routes); // clear inputs on shutdown @@ -252,7 +269,7 @@ public class RouteService extends Servic protected void stopChildService(Route route, Set<Service> services, boolean shutdown) throws Exception { for (Service service : services) { - LOG.debug("Stopping child service on route: {} -> {}", route.getId(), service); + LOG.debug("{} child service on route: {} -> {}", new Object[]{shutdown ? "Shutting down" : "Stopping", route.getId(), service}); for (LifecycleStrategy strategy : camelContext.getLifecycleStrategies()) { strategy.onServiceRemove(camelContext, service, route); } Modified: camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/processor/RecipientList.java URL: http://svn.apache.org/viewvc/camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/processor/RecipientList.java?rev=1226085&r1=1226084&r2=1226085&view=diff ============================================================================== --- camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/processor/RecipientList.java (original) +++ camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/processor/RecipientList.java Sat Dec 31 13:39:08 2011 @@ -171,6 +171,12 @@ public class RecipientList extends Servi ServiceHelper.stopService(producerCache); } + protected void doShutdown() throws Exception { + // remove producer cache from service + camelContext.removeService(producerCache); + ServiceHelper.stopAndShutdownService(producerCache); + } + public boolean isStreaming() { return streaming; } Modified: camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/processor/RecipientListProcessor.java URL: http://svn.apache.org/viewvc/camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/processor/RecipientListProcessor.java?rev=1226085&r1=1226084&r2=1226085&view=diff ============================================================================== --- camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/processor/RecipientListProcessor.java (original) +++ camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/processor/RecipientListProcessor.java Sat Dec 31 13:39:08 2011 @@ -233,6 +233,13 @@ public class RecipientListProcessor exte super.doStop(); } + protected void doShutdown() throws Exception { + // remove producer cache from service + getCamelContext().removeService(producerCache); + ServiceHelper.stopAndShutdownService(producerCache); + super.doShutdown(); + } + @Override public String toString() { return "RecipientList"; Modified: camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/processor/RoutingSlip.java URL: http://svn.apache.org/viewvc/camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/processor/RoutingSlip.java?rev=1226085&r1=1226084&r2=1226085&view=diff ============================================================================== --- camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/processor/RoutingSlip.java (original) +++ camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/processor/RoutingSlip.java Sat Dec 31 13:39:08 2011 @@ -353,6 +353,12 @@ public class RoutingSlip extends Service ServiceHelper.stopService(producerCache); } + protected void doShutdown() throws Exception { + // remove producer cache from service + camelContext.removeService(producerCache); + ServiceHelper.stopAndShutdownService(producerCache); + } + /** * Returns the outbound message if available. Otherwise return the inbound message. */ Modified: camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/processor/SendProcessor.java URL: http://svn.apache.org/viewvc/camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/processor/SendProcessor.java?rev=1226085&r1=1226084&r2=1226085&view=diff ============================================================================== --- camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/processor/SendProcessor.java (original) +++ camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/processor/SendProcessor.java Sat Dec 31 13:39:08 2011 @@ -167,4 +167,9 @@ public class SendProcessor extends Servi ServiceHelper.stopService(producerCache); } + protected void doShutdown() throws Exception { + // remove producer cache from service + camelContext.removeService(producerCache); + ServiceHelper.stopAndShutdownService(producerCache); + } } Modified: camel/branches/camel-2.8.x/camel-core/src/test/java/org/apache/camel/util/ObjectHelperTest.java URL: http://svn.apache.org/viewvc/camel/branches/camel-2.8.x/camel-core/src/test/java/org/apache/camel/util/ObjectHelperTest.java?rev=1226085&r1=1226084&r2=1226085&view=diff ============================================================================== --- camel/branches/camel-2.8.x/camel-core/src/test/java/org/apache/camel/util/ObjectHelperTest.java (original) +++ camel/branches/camel-2.8.x/camel-core/src/test/java/org/apache/camel/util/ObjectHelperTest.java Sat Dec 31 13:39:08 2011 @@ -304,5 +304,18 @@ public class ObjectHelperTest extends Te assertTrue(ObjectHelper.hasDefaultPublicNoArgConstructor(ObjectHelperTest.class)); assertFalse(ObjectHelper.hasDefaultPublicNoArgConstructor(MyStaticClass.class)); } + + public void testIdentityHashCode() { + MyDummyObject dummy = new MyDummyObject("Camel"); + + String code = ObjectHelper.getIdentityHashCode(dummy); + String code2 = ObjectHelper.getIdentityHashCode(dummy); + + assertEquals(code, code2); + + MyDummyObject dummyB = new MyDummyObject("Camel"); + String code3 = ObjectHelper.getIdentityHashCode(dummyB); + assertNotSame(code, code3); + } }