简体   繁体   English

Camel,Spring,OSGI:有没有一种方法可以指定stop方法?

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

I'm running a Camel Spring OSGI application. 我正在运行Camel Spring OSGI应用程序。 The Camel context is initialized through Spring. Camel上下文是通过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. 我了解OSGI捆绑包必须提供激活器的启动和停止方法,但我的理解还在于Camel / Spring / OSGI框架会覆盖这些方法。

My beanx.xml: 我的beanx.xml:

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

My java code: 我的Java代码:

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

Just to expand a little on the answer of Bilgin Ibryam which is correct. 只是为了扩大对Bilgin Ibryam的回答,这是正确的。

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. 声明一个新类很简单,该类扩展了RoutePolicySupport然后覆盖了您感兴趣的方法。

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: 现在,在您的routebuilder configure()方法中使用该路由,如下所示:

 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: 如果您仅使用带有路径的Spring XML,请添加以下内容:

<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>

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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