Camel、Spring、OSGI:有没有办法指定停止方法? [英] Camel, Spring, OSGI: Is there a way to specify the stop method?

查看:42
本文介绍了Camel、Spring、OSGI:有没有办法指定停止方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行 Camel Spring OSGI 应用程序.Camel 上下文是通过 Spring 初始化的.当 bundle 停止时,我需要做一些清理活动,比如取消注册消息监听器.我怎么做?有没有我可以覆盖的方法?我知道 OSGI 包必须提供激活器启动和停止方法,但我的理解是 Camel/Spring/OSGI 框架覆盖了这些方法.

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.

我的 beanx.xml:

My beanx.xml:

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

我的java代码:

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

推荐答案

只是对 Bilgin Ibryam 的正确答案进行一点扩展.

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

Camel 能够将策略应用于路由.此策略在运行时控制路由.这将允许您在路线生命周期的某些事件中执行自定义逻辑.

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.

实施路线政策.

声明一个扩展 RoutePolicySupport 然后覆盖您感兴趣的方法的新类相当简单.

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);
    }


}

现在在你的路由构建器 configure() 方法中使用路由,如下所示:

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");

如果您只是使用带有路由的 Spring XML,请添加以下内容:

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>

这篇关于Camel、Spring、OSGI:有没有办法指定停止方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆