0

I have camel Processor which is starting Thread this Processor implementing spring ShutdownAware When I send TERM signal (graceful shutdown) to application I do not receive any information about - any method from ShutdownAware interface were called. I mentioned about Thread because I want to stop it correctly.

import org.apache.camel.Processor;
import org.apache.camel.spi.ShutdownAware;

public class MyProcessor implements Processor, ShutdownAware {
...
}

Should I register this Processor|ShutdownAware somehow or what?

THM
  • 579
  • 6
  • 25

2 Answers2

2

Solution is:

import org.apache.camel.Processor;
import org.apache.camel.Service;
import org.apache.camel.spi.ShutdownAware;

public class MyProcessor implements Processor, ShutdownAware, Service {
...
}

then methods from ShutdownAware will be called during shutdown even start(), stop() methods from 'Service' interface are not used.

THM
  • 579
  • 6
  • 25
1

ShutdownAware is a Camel interface, not Spring.

If MyProcessor is a Spring bean then it could implement DisposableBean, or have a @PreDestroy method, or otherwise annotated with something that Spring understands to call when the context is shutting down.

If you're using XML, you might do this

<bean class="...MyProcessor" destroy-method="shutdown"/>

If you're using @Bean perhaps

@Bean(destroyMethod = "shutdown")
public MyProcessor myProcessor() { ... }

Of course MyProcessor will need a shutdown method; nothing in ShutdownAware seems suitable.

How is the Spring context created? Perhaps you need to register the shutdown hook, eg: context.registerShutdownHook(); See the Javadoc

Really, the way to deal with this is to have the CamelContext be aware that Spring is shutting down. That should then inform your processors.

ptomli
  • 11,730
  • 4
  • 40
  • 68
  • Yes our you can extend `ServiceSupport` or implement `Service` in your `Processor` class and then Camel will invoke the `doStart` / `doStop` methods as part of the lifecycle on start/stopping. See more at: http://camel.apache.org/lifecycle – Claus Ibsen Oct 10 '17 at 14:39
  • Thanks for answer. Then what for is `ShutdownAware` / how to use it? – THM Oct 11 '17 at 06:47
  • The javadoc for `ShutdownAware` says this is mainly for in-memory consumers to be provided an chance to ensure their pending exchanges are not lost during shutdown. See https://camel.apache.org/maven/camel-2.15.0/camel-core/apidocs/org/apache/camel/spi/ShutdownAware.html – ptomli Oct 11 '17 at 06:51
  • OK, I see. Looks like everything is fine when I add `Service` interface tin my code. – THM Oct 11 '17 at 07:06