Configuring Routes Startup Ordering and Autostartup
Camel supports configuring two aspects:
-
auto startup
-
order of starting routes
Autostartup
The autoStartup
option allows is to configure
Camel to not auto start routes when Camel starts.
The auto startup can be configured on two levels:
-
CamelContext - Globally
-
Route - Individually per route
For example the CamelContext below we have configured autoStartup=false
to
prevent Camel starting all the routes on startup.
<camelContext id="myCamel" xmlns="http://camel.apache.org/schema/spring"
autoStartup="false">
<route>
<from uri="direct:foo"/>
<to uri="mock:foo"/>
</route>
<route>
<from uri="direct:bar"/>
<to uri="mock:bar"/>
</route>
</camelContext>
So how do you start the routes?
The autoStartup
option on the <camelContext>
is only used once, so you
can manually start Camel later by invoking its start
method in Java as shown
below.
For example when using Spring, you can get hold of the CamelContext
via
the Spring ApplicationContext
:
ApplicationContext ac = ...
CamelContext camel = ac.getBean("myCamel", CamelContext.class);
// now start all the routes
camel.getRouteController().startAllRoutes();
The routes can also be started via JMX (requires camel-management JAR to be on the classpath) by invoking the startAllRoutes operation on the CamelContext MBean.
|
Autostartup per route level
You can also use the autoStartup
option to configure if a given route
should be started when Camel starts. By default, a route is auto started.
In XML DSL you disable auto startup 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>
And in Java DSL you can configure auto startup on CamelContext
as follows:
camelContext.setAutoStartup(false);
And on route level you can disable it via noAutoStartup
:
from("activemq:queue:special").noAutoStartup()
.to("file://backup");
To startup based on a boolean, String or Property, do one of the following:
// using a boolean
from("activemq:queue:special").autoStartup(false)
.to("file://backup");
// using a string
from("activemq:queue:special").autoStartup("false")
.to("file://backup");
// using property placeholders
from("activemq:queue:special").autoStartup("{{startupRouteProperty}}")
.to("file://backup");
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 an Integer
that states the order.
Camel then sorts the routes before starting time. The routes with the
lowest startupOrder
are started first and the ones with the highest are
started last.
All startupOrder defined must be unique among all routes in your
CamelContext. Otherwise, if there are clashes in
startupOrder numbers among routes, the routes will fail to start up throwing
org.apache.camel.FailedToStartRouteException .
|
Normally you should also use numbers that are lower than 1000, as routes without
an explicit startupOrder
definition will have a number starting from 1000
auto assigned. So view numbers from 1000 upwards as reserved internally
for Camel itself.
However, you can also utilise much higher numbers than 1000 (to avoid collisions with those auto assigned numbers) to specify the last routes to start up. Normally the usage of numbers starting from 10000 should be safe for the purpose.
In terms of the startupOrder
there are no strict rules that it must
start from 1 and increment by 1. You can for example use: 100, 200, 205,
89 if you like. Only rule of thumb is that the numbers must be unique.
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.
Camel will stop the routes in the reverse order that they were started.
Let’s try a couple of examples.
Startup ordering example
from("seda:foo").startupOrder(1)
.to("mock:result");
from("direct:start").startupOrder(2)
.to("seda:foo");
And the same example with XML DSL:
<routes>
<route startupOrder="1">
<from uri="seda:foo"/>
<to uri="mock:result"/>
</route>
<route startupOrder="2">
<from uri="direct:start"/>
<to uri="seda:foo"/>
</route>
</routes>
In this example we have two routes in which we have started that the direct:start route should be started after the seda:foo route. Because direct:start is considered the input, and we want seda:foo route to be up and running beforehand.
Using startOrder together with non startOrder
You can also mix and match routes with and without startupOrder
defined.
The first two routes below have start order defined, and the last route has not.
from("seda:foo").startupOrder(1)
.to("mock:result");
from("direct:start").startupOrder(2)
.to("seda:foo");
from("direct:bar")
.to("seda:bar");
And the same example with XML DSL:
<routes>
<route startupOrder="1">
<from uri="seda:foo"/>
<to uri="mock:result"/>
</route>
<route startupOrder="2">
<from uri="direct:start"/>
<to uri="seda:foo"/>
</route>
<route>
<from uri="direct:bar"/>
<to uri="seda:bar"/>
</route>
</routes>
In the route above we have not defined 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; 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.
Configuring routes to start up last
You can use a high number in startupOrder
to have a specific route startup last as shown below:
// use auto assigned startup ordering
from("direct:start").to("seda:foo");
// should start first
from("seda:foo").startupOrder(1).to("mock:result");
// should start last after the default routes
from("direct:bar").startupOrder(12345).to("seda:bar");
// use auto assigned startup ordering
from("seda:bar").to("mock:other");
In the example above the order of startups of routes should be:
-
seda:foo
-
direct:start
-
seda:bar
-
direct:bar
Shutting down routes
Camel will shut down the routes in the reverse order that they were started.
See more at Graceful Shutdown.