简体   繁体   English

如果Camel正在关闭,如何检查Spring-DSL?

[英]How can I check in Spring-DSL if Camel is shutting down?

I have a route that contains a wiretap element that redirects to a route that tries to make a bean invocation after a certain delay (approximately 20 seconds). 我有一条路由,其中​​包含一个窃听元素,该元素会重定向到在特定延迟(大约20秒)后尝试进行Bean调用的路由。 However when the application shuts down, I get errors because the bean doesn't exist anymore and during shutdown it cannot be created. 但是,当应用程序关闭时,出现错误,因为bean不再存在,并且在关闭期间无法创建。 I want to make my application shutdown gracefully by checking the state of the application after the delay. 我想通过延迟后检查应用程序的状态来使应用程序正常关闭。 How can I do that? 我怎样才能做到这一点?

<route id="triggerAfterDelay">
    ...
    <delay><constant>20000</constant></delay>

    <!-- TODO Check if we're not shutting down. -->

    <bean ref="myBean" method="updateAfterDelay"/>
</route>

If you are on Camel 2.11.x or newer, you might be able to do that with the new ControlBus component. 如果您使用的是Camel 2.11.x或更高版本,则可以使用新的ControlBus组件执行此操作。

Maybe easier and also available in earlier Camel versions is to subclass org.apache.camel.support.ServiceSupport . 子类org.apache.camel.support.ServiceSupport可能更容易且在较早的Camel版本中可用。 If myBean extends ServiceSupport you have to implement the doStop() method. 如果myBean扩展了ServiceSupport ,则必须实现doStop()方法。 There you can set a flag ( AtomicBoolean ?) to signify that shutdown is on progress. 您可以在其中设置一个标志( AtomicBoolean ?),以表明正在进行关闭。 In updateAfterDelay() you can check that flag. updateAfterDelay()您可以检查该标志。

Currently I have a working solution by using the following filter. 目前,我通过使用以下过滤器有一个可行的解决方案。 However I'm still interested in "native" camel ways to do this. 但是,我仍然对“本地”骆驼方法感兴趣。

public class CamelFilter implements ApplicationContextAware {
    public boolean isMyBeanNotAvailable() {
        boolean notAvailable = false;
        try {
            notAvailable = this.applicationContext.getBean("myBean") == null;
        } catch (BeanCreationNotAllowedException bcnae) {
            notAvailable = true;
        }
        return notAvailable;
    }
}

With the following route: 使用以下路线:

<route id="triggerAfterDelay">
    ...
    <delay><constant>20000</constant></delay>

    <filter>
        <method ref="camelFilter" method="isMyBeanNotAvailable"/>
        <stop/>
    </filter>

    <bean ref="myBean" method="updateAfterDelay"/>
</route>

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

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