骆驼,春,OSGI:有没有指定停止方法的方法吗? [英] Camel, Spring, OSGI: Is there a way to specify the stop method?

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

问题描述

我跑骆驼春OSGi应用程序。骆驼上下文是通过Spring初始化。
当包停止时,我需要做一些清理活动,如去注册消息监听器。我怎么做?有没有我可以覆盖的方法?据我所知,一个OSGi bundle必须提供激活启动和停止方法,但我的理解也就是骆驼/春/ OSGI框架覆盖这些方法。

我beanx.xml:

 <豆>
  < camelContext ID =骆驼的xmlns =htt​​p://camel.apache.org/schema/spring>
    < routeBuilder REF =outboundBuilder/>
  < / camelContext>
< /豆>

我的Java code:

 公共类MyRouteBuilder扩展RouteBuilder {
    公共无效配置(){
           .....
    }
}


解决方案

只是扩大了几分比尔金Ibryam的答案是正确的。

骆驼有策略应用于路线的能力。此策略控制在运行时的路线。这将允许你在的路线续航时间的某些事件进行自定义逻辑。

实现路由策略。

这是相当简单的声明延伸RoutePolicySupport然后重写你感兴趣的方法的新类。

 公共类MyRoutePolicy扩展RoutePolicySupport {    @覆盖
        公共无效调用onStart(路线路线){
        // TODO自动生成方法存根
        super.onStart(路线);
    }    @覆盖
    公共无效的onStop(路由路径){
        // TODO自动生成方法存根
        super.onStop(路线);
    }    @覆盖
    公共无效onExchangeBegin(干线航线,外汇兑换){
        // TODO自动生成方法存根
        super.onExchangeBegin(路由,交换);
    }
}

像这样的

现在使用route在routebuilder configure()方法:

  RoutePolicy政策=新MyRoutePolicy();
 从(定时器://嗒嗒)
   .routeId(测试1)。routePolicy(政策)
   .setBody()。常数(这样的消息的Hello World)
   。要(模拟:咩);

如果你只是使用Spring的XML与路由然后添加以下内容:

 和LT;豆的id =政策级=MyRoutePolicy/>
< camelContext的xmlns =htt​​p://camel.apache.org/schema/spring>
   <路线ID =富routePolicyRef =MyRoutePolicy>
     <从URI =定时器://嗒嗒/>
     < setBody><恒>将信息像的Hello World< /恒定GT;< / setBody>
     <到URI =模拟:咩/>
   < /路由>
 < / camelContext>

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>

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

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