Configuring routes startup ordering and autostartup
Available as of Camel 2.1
When Camel starts (in fact CamelContext) you can configure how routes should be started
- whether a given route should be auto started or not
- the order in which routes should be started
Configuring whether a route should be started or not
You can use the autoStartup
option to configure if a given route should be started when Camel starts. By default a route is auto started.
You can disable or enable it as follows:
from("activemq:queue:special").noAutoStartup().to("file://backup");
And to explicit state it should be started
from("activemq:queue:special").autoStartup().to("file://backup");
In XML DSL you define it as follows:
<route autoStartup="false"> <from uri="activemq:queue:special"/> <to uri="file://backup"/> </route>
And to explicit state it should be started
<route autoStartup="true"> <from uri="activemq:queue:special"/> <to uri="file://backup"/> </route>
Configuring starting order for routes
You can also configure the order in which routes are started. Previously Camel started the routes in a non deterministic order. Now you have fine grained control in which order the routes should be started. There is a new attribute startupOrder
which is a Integer that states the order. Camel then sorts the routes before starting time. The routes with the lowest startupOrder
is started first. All startupOrder
defined must be unique among all routes in your CamelContext.
You should also use numbers that are lower than 1000, as routes without an explicit startupOrder
define will have a number starting from 1000 auto assigned. So view numbers from 1000 upwards as reserved internally for Camel itself.
Why do you want to control the starting order?
It can help in cases where routes are inter dependent on each other and also help with graceful shutting down Camel as Camel can stop the routes in the correct order as well.
Stopping routes
Camel will stop the routes in the same order in which they was started.
Examples
Lets try a couple of examples
Simple example
from("seda:foo").startupOrder(2).to("mock:result"); from("direct:start").startupOrder(1).to("seda:foo");
And the same example with XML DSL:
<route startupOrder="2"> <from uri="seda:foo"/> <to uri="mock:result"/> </route> <route startupOrder="1"> <from uri="direct:start"/> <to uri="seda:foo"/> </route>
In this example we have two routes in which we have started that the direct:start
route should be started before the seda:foo
route.
You can also mix and match routes with and without startupOrder
define.
Routes with startupOrder mixed with routes without
from("seda:foo").startupOrder(2).to("mock:result"); from("direct:start").startupOrder(1).to("seda:foo"); from("direct:bar").to("seda:bar");
And the same example with XML DSL:
<route startupOrder="2"> <from uri="seda:foo"/> <to uri="mock:result"/> </route> <route startupOrder="1"> <from uri="direct:start"/> <to uri="seda:foo"/> </route> <route> <from uri="direct:bar"/> <to uri="seda:bar"/> </route>
In the route above we have not define a startupOrder
on the last route direct:bar
in which Camel will auto assign a number for it, in which this case will be 1000. So therefore the route will be started last.
So you can use this to your advantage to only assign a startupOrder
on the routes which really needs it.