Shutting down Camel after X messages

It’s a common question - how to get Apache Camel and the JVM it’s running in to shut down after processing a certain number of messages.

Previously, if you wanted to shut down a Camel app after a certain amount of time, or a certain number of messages, you would need to implement your own logic and possibly an explicit shut down command.

However from Camel 2.19 onwards, if you’re using Spring Boot as a container, there is out-of-the-box support to do this.

Auto shutdown of Camel in Spring Boot - settings

To make Apache Camel shut down after a certain amount of time or messages, any of the following properties to your application.properties file.

To shut down a Camel application after processing a certain number of messages:

camel.springboot.duration-max-messages=100

To shut down a Camel application after a fixed period of time:

camel.springboot.duration-max-seconds=60

To shut down a Camel application after Camel has been idle for a certain number of seconds (in other words, if it’s not currently processing an Exchange):

camel.springboot.duration-max-idle-seconds=15

This will really help you out if you’re implementing microservices with Camel. These settings mean it’s much easier to use Camel to just process a single file, or run for a short amount of time.

Look out for these messages in the log:

2018-05-29 16:39:06.963  INFO 82642 --- [           main] o.a.camel.spring.boot.RoutesCollector    : CamelSpringBoot will terminate after processing 4 messages
...
2018-05-29 16:35:01.674  INFO 60394 --- [otTerminateTask] o.a.camel.spring.boot.RoutesCollector    : CamelSpringBoot triggering shutdown of the JVM.

See Camel issue CAMEL-10596.