Вопрос

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). However when the application shuts down, I get errors because the bean doesn't exist anymore and during shutdown it cannot be created. 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>
Это было полезно?

Решение 2

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>

Другие советы

If you are on Camel 2.11.x or newer, you might be able to do that with the new ControlBus component.

Maybe easier and also available in earlier Camel versions is to subclass org.apache.camel.support.ServiceSupport. If myBean extends ServiceSupport you have to implement the doStop() method. There you can set a flag (AtomicBoolean?) to signify that shutdown is on progress. In updateAfterDelay() you can check that flag.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top