Author: davsclaus Date: Sat Nov 26 10:36:16 2011 New Revision: 1206424 URL: http://svn.apache.org/viewvc?rev=1206424&view=rev Log: CAMEL-4514: Added onRemove callback to RoutePolicy. Thanks to Bilgin for the patch.
Added: camel/trunk/camel-core/src/test/java/org/apache/camel/impl/RoutePolicyOnRemoveTest.java Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/RoutePolicySupport.java camel/trunk/camel-core/src/main/java/org/apache/camel/impl/RouteService.java camel/trunk/camel-core/src/main/java/org/apache/camel/spi/RoutePolicy.java Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/RoutePolicySupport.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/RoutePolicySupport.java?rev=1206424&r1=1206423&r2=1206424&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/RoutePolicySupport.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/RoutePolicySupport.java Sat Nov 26 10:36:16 2011 @@ -42,6 +42,10 @@ public abstract class RoutePolicySupport // noop } + public void onRemove(Route route) { + // noop + } + public void onExchangeBegin(Route route, Exchange exchange) { // noop } 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=1206424&r1=1206423&r2=1206424&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 Sat Nov 26 10:36:16 2011 @@ -33,6 +33,7 @@ import org.apache.camel.Service; import org.apache.camel.model.RouteDefinition; import org.apache.camel.spi.LifecycleStrategy; import org.apache.camel.spi.RouteContext; +import org.apache.camel.spi.RoutePolicy; import org.apache.camel.support.ChildServiceSupport; import org.apache.camel.util.EventHelper; import org.apache.camel.util.ServiceHelper; @@ -212,6 +213,12 @@ public class RouteService extends ChildS // endpoints should only be stopped when Camel is shutting down // see more details in the warmUp method ServiceHelper.stopAndShutdownServices(route.getEndpoint()); + // invoke callbacks on route policy + if (routeDefinition.getRoutePolicies() != null) { + for (RoutePolicy routePolicy : routeDefinition.getRoutePolicies()) { + routePolicy.onRemove(route); + } + } } // need to call onRoutesRemove when the CamelContext is shutting down or Route is shutdown Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/spi/RoutePolicy.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/spi/RoutePolicy.java?rev=1206424&r1=1206423&r2=1206424&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/spi/RoutePolicy.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/spi/RoutePolicy.java Sat Nov 26 10:36:16 2011 @@ -37,6 +37,13 @@ public interface RoutePolicy { void onInit(Route route); /** + * Callback invoked when the {@link Route} is being removed from {@link org.apache.camel.CamelContext} + * + * @param route the route being removed + */ + void onRemove(Route route); + + /** * Callback invoked when an {@link Exchange} is started being routed on the given {@link Route} * * @param route the route where the exchange started from Added: camel/trunk/camel-core/src/test/java/org/apache/camel/impl/RoutePolicyOnRemoveTest.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/impl/RoutePolicyOnRemoveTest.java?rev=1206424&view=auto ============================================================================== --- camel/trunk/camel-core/src/test/java/org/apache/camel/impl/RoutePolicyOnRemoveTest.java (added) +++ camel/trunk/camel-core/src/test/java/org/apache/camel/impl/RoutePolicyOnRemoveTest.java Sat Nov 26 10:36:16 2011 @@ -0,0 +1,85 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.impl; + +import java.util.concurrent.atomic.AtomicInteger; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.Exchange; +import org.apache.camel.Route; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.spi.RoutePolicy; + +public class RoutePolicyOnRemoveTest extends ContextTestSupport { + private MyRoutPolicy routePolicy = new MyRoutPolicy(); + + public void testRemoveCalledWhenRouteIsRemovedById() throws Exception { + assertEquals(0, routePolicy.getRemoveCount()); + context.stopRoute("foo"); + assertEquals(0, routePolicy.getRemoveCount()); + + context.removeRoute("foo"); + assertEquals(1, routePolicy.getRemoveCount()); + } + + public void testRemoveCalledWhenCamelIsStopped() throws Exception { + assertTrue(context.getStatus().isStarted()); + assertEquals(0, routePolicy.getRemoveCount()); + context.stop(); + assertTrue(context.getStatus().isStopped()); + assertEquals(1, routePolicy.getRemoveCount()); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:start") + .routeId("foo") + .routePolicy(routePolicy) + .to("mock:result"); + } + }; + } + + private class MyRoutPolicy implements RoutePolicy { + private AtomicInteger removeCounter = new AtomicInteger(); + + @Override + public void onInit(Route route) { + } + + @Override + public void onRemove(Route route) { + removeCounter.incrementAndGet(); + } + + @Override + public void onExchangeBegin(Route route, Exchange exchange) { + } + + @Override + public void onExchangeDone(Route route, Exchange exchange) { + } + + public int getRemoveCount() { + return removeCounter.get(); + } + + } +}