简体   繁体   中英

Camel, Spring, OSGI: Is there a way to specify the stop method?

I'm running a Camel Spring OSGI application. The Camel context is initialized through Spring. When the bundle stops, I need to do some clean-up activities, like de-registering the message listener. How do I do that? Is there a method I can override? I understand that an OSGI bundle must provide the activator start and stop methods but my understanding also is that the Camel/Spring/OSGI framework overrides these methods.

My beanx.xml:

<beans>
  <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
    <routeBuilder ref="outboundBuilder" />
  </camelContext>
</beans>

My java code:

public class MyRouteBuilder extends RouteBuilder {
    public void configure() {
           .....
    }
}

Just to expand a little on the answer of Bilgin Ibryam which is correct.

Camel has the ability to apply a policy to a route. This Policy controls routes at runtime. This will allow you to do custom logic at certain events of the route life time.

Implementing a route policy.

It is rather simple declare a new class which extends RoutePolicySupport then override the methods you are interested in.

public class MyRoutePolicy extends RoutePolicySupport{

    @Override
        public void onStart(Route route) {
        // TODO Auto-generated method stub
        super.onStart(route);
    } 

    @Override
    public void onStop(Route route) {
        // TODO Auto-generated method stub
        super.onStop(route);
    }

    @Override
    public void onExchangeBegin(Route route, Exchange exchange) {
        // TODO Auto-generated method stub
        super.onExchangeBegin(route, exchange);
    }


}

Now use the route in your routebuilder configure() method like this:

 RoutePolicy policy = new MyRoutePolicy();
 from("timer://blah")
   .routeId("Test1").routePolicy(policy)
   .setBody().constant("A Message Like Hello World")
   .to("mock:meh");

If you were just using a Spring XML with a route then add the following:

<bean id="policy" class="MyRoutePolicy"/>


<camelContext xmlns="http://camel.apache.org/schema/spring">
   <route id="foo" routePolicyRef="MyRoutePolicy">
     <from uri="timer://blah"/>
     <setBody><constant>A Message Like Hello World</constant></setBody>        
     <to uri="mock:meh"/>
   </route>
 </camelContext>

您可以使用骆驼路线策略,并在路线即将停止或从上下文中删除路线时编写代码以清理资源。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM