camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > Configuring route startup ordering and autostartup
Date Wed, 17 Aug 2011 17:40:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/2042/9/1/_/styles/combined.css?spaceKey=CAMEL&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background: white;" bgcolor="white" class="email-body">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="https://cwiki.apache.org/confluence/display/CAMEL/Configuring+route+startup+ordering+and+autostartup">Configuring
route startup ordering and autostartup</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~boday">Ben
O&#39;Day</a>
    </h4>
        <div id="versionComment">
        <b>Comment:</b>
        updated per CAMEL-4217<br />
    </div>
        <br/>
                         <h4>Changes (5)</h4>
                                 
    
<div id="page-diffs">
                    <table class="diff" cellpadding="0" cellspacing="0">
    
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >{code} <br> <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">And
to explicit state it should be started <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">To
startup based on a boolean, String or [Property|http://camel.apache.org/properties.html],
do one of the following: <br></td></tr>
            <tr><td class="diff-unchanged" >{code} <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">boolean
startupRoute = true;    <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-changed-words">from(&quot;activemq:queue:special&quot;).autoStartup(<span
class="diff-added-chars"style="background-color: #dfd;">startRoute</span>).to(&quot;file://backup&quot;);</span>
<br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">...
<br>String startupRoute = &quot;true&quot;;    <br>from(&quot;activemq:queue:special&quot;).autoStartup(startRoute).to(&quot;file://backup&quot;);
<br>... <br>from(&quot;activemq:queue:special&quot;).autoStartup(&quot;{{startupRouteProperty}}&quot;).to(&quot;file://backup&quot;);
<br></td></tr>
            <tr><td class="diff-unchanged" >{code} <br> <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h2><a name="Configuringroutestartuporderingandautostartup-Configuringroutesstartuporderingandautostartup"></a>Configuring
routes startup ordering and autostartup</h2>
<p><b>Available as of Camel 2.1</b></p>

<p>Camel now supports configuring two aspects:</p>
<ul class="alternate" type="square">
	<li>auto startup</li>
	<li>order of starting routes</li>
</ul>


<h3><a name="Configuringroutestartuporderingandautostartup-ConfiguringwhetherCamelshouldbeautostartedornot"></a>Configuring
whether Camel should be auto started or not</h3>
<p>The old option <tt>shouldStartContext</tt> have been removed and replaced
with this new <tt>autoStartup</tt> option instead. What it allows is to configure
Camel to <b>not</b> auto start when Spring starts.</p>

<p>For example the route below we have configured <b>autoStartup=false</b>
to prevent Camel starting when Spring starts.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
    <span class="code-tag">&lt;camelContext id=<span class="code-quote">"myCamel"</span>
xmlns=<span class="code-quote">"http://camel.apache.org/schema/spring"</span>
autoStartup=<span class="code-quote">"false"</span>&gt;</span>
        <span class="code-tag">&lt;route&gt;</span>
            <span class="code-tag">&lt;from uri=<span class="code-quote">"direct:start"</span>/&gt;</span>
            <span class="code-tag">&lt;to uri=<span class="code-quote">"mock:result"</span>/&gt;</span>
        <span class="code-tag">&lt;/route&gt;</span>
    <span class="code-tag">&lt;/camelContext&gt;</span>
</pre>
</div></div>

<p>So how do you start Camel then? </p>

<p>The <b>autoStartup</b> option on the &lt;camelContext&gt; is
only used once, so you can manually start Camel later by invoking its <tt>start</tt>
method as shown below:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
    ApplicationContext ac = ...
    SpringCamelContext camel = (SpringCamelContext) ac.getBean(<span class="code-quote">"myCamel"</span>);
    
    <span class="code-comment">// now start Camel manually
</span>    camel.start();
</pre>
</div></div>


<h3><a name="Configuringroutestartuporderingandautostartup-Configuringwhetherarouteshouldbestartedornot"></a>Configuring
whether a route should be started or not</h3>
<p>You can use the <tt>autoStartup</tt> option to configure if a given route
should be started when Camel starts. By default a route is auto started.</p>

<p>You can disable or enable it as follows:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"activemq:queue:special"</span>).noAutoStartup().to(<span
class="code-quote">"file:<span class="code-comment">//backup"</span>);</span>
</pre>
</div></div>

<p>To startup based on a boolean, String or <a href="http://camel.apache.org/properties.html"
class="external-link" rel="nofollow">Property</a>, do one of the following:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-object">boolean</span> startupRoute = <span class="code-keyword">true</span>;
  
from(<span class="code-quote">"activemq:queue:special"</span>).autoStartup(startRoute).to(<span
class="code-quote">"file:<span class="code-comment">//backup"</span>);
</span>...
<span class="code-object">String</span> startupRoute = <span class="code-quote">"<span
class="code-keyword">true</span>"</span>;   
from(<span class="code-quote">"activemq:queue:special"</span>).autoStartup(startRoute).to(<span
class="code-quote">"file:<span class="code-comment">//backup"</span>);
</span>...
from(<span class="code-quote">"activemq:queue:special"</span>).autoStartup(<span
class="code-quote">"{{startupRouteProperty}}"</span>).to(<span class="code-quote">"file:<span
class="code-comment">//backup"</span>);</span>
</pre>
</div></div>

<p>In XML DSL you define it as follows:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;route autoStartup=<span class="code-quote">"false"</span>&gt;</span>
   <span class="code-tag">&lt;from uri=<span class="code-quote">"activemq:queue:special"</span>/&gt;</span>
   <span class="code-tag">&lt;to uri=<span class="code-quote">"file://backup"</span>/&gt;</span>
<span class="code-tag">&lt;/route&gt;</span>
</pre>
</div></div>

<p>And to explicit state it should be started</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;route autoStartup=<span class="code-quote">"true"</span>&gt;</span>
   <span class="code-tag">&lt;from uri=<span class="code-quote">"activemq:queue:special"</span>/&gt;</span>
   <span class="code-tag">&lt;to uri=<span class="code-quote">"file://backup"</span>/&gt;</span>
<span class="code-tag">&lt;/route&gt;</span>
</pre>
</div></div>

<h3><a name="Configuringroutestartuporderingandautostartup-Configuringstartingorderforroutes"></a>Configuring
starting order for routes</h3>
<p>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 <tt>startupOrder</tt> which
is a Integer that states the order. Camel then sorts the routes before starting time. The
routes with the lowest <tt>startupOrder</tt> is started first. All <tt>startupOrder</tt>
defined must be unique among all routes in your <a href="/confluence/display/CAMEL/CamelContext"
title="CamelContext">CamelContext</a>.<br/>
You should also use numbers that are lower than 1000, as routes without an explicit <tt>startupOrder</tt>
define will have a number starting from 1000 auto assigned. So view numbers from 1000 upwards
as reserved internally for Camel itself.</p>

<p>In terms of the <tt>startupOrder</tt> there are no strict rule 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.</p>

<h4><a name="Configuringroutestartuporderingandautostartup-Whydoyouwanttocontrolthestartingorder%3F"></a>Why
do you want to control the starting order?</h4>
<p>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.</p>

<div class='panelMacro'><table class='infoMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/information.gif" width="16"
height="16" align="absmiddle" alt="" border="0"></td><td><b>Stopping
routes</b><br />Camel will stop the routes in the <b>same</b> order
in which they was started.</td></tr></table></div>

<h3><a name="Configuringroutestartuporderingandautostartup-Examples"></a>Examples</h3>
<p>Lets try a couple of examples</p>

<h4><a name="Configuringroutestartuporderingandautostartup-Simpleexample"></a>Simple
example</h4>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
    from(<span class="code-quote">"seda:foo"</span>).startupOrder(1).to(<span
class="code-quote">"mock:result"</span>);
    from(<span class="code-quote">"direct:start"</span>).startupOrder(2).to(<span
class="code-quote">"seda:foo"</span>);
</pre>
</div></div>

<p>And the same example with XML DSL:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
    <span class="code-tag">&lt;route startupOrder=<span class="code-quote">"1"</span>&gt;</span>
        <span class="code-tag">&lt;from uri=<span class="code-quote">"seda:foo"</span>/&gt;</span>
        <span class="code-tag">&lt;to uri=<span class="code-quote">"mock:result"</span>/&gt;</span>
    <span class="code-tag">&lt;/route&gt;</span>

    <span class="code-tag">&lt;route startupOrder=<span class="code-quote">"2"</span>&gt;</span>
        <span class="code-tag">&lt;from uri=<span class="code-quote">"direct:start"</span>/&gt;</span>
        <span class="code-tag">&lt;to uri=<span class="code-quote">"seda:foo"</span>/&gt;</span>
    <span class="code-tag">&lt;/route&gt;</span>
</pre>
</div></div>

<p>In this example we have two routes in which we have started that the <tt>direct:start</tt>
route should be started <b>after</b> the <tt>seda:foo</tt> route.<br/>
As <tt>direct:start</tt> is consider the input and we want that <tt>seda:foo</tt>
route to be up and running beforehand.</p>

<p>You can also mix and match routes with and without <tt>startupOrder</tt>
define.</p>

<h4><a name="Configuringroutestartuporderingandautostartup-RouteswithstartupOrdermixedwithrouteswithout"></a>Routes
with startupOrder mixed with routes without</h4>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
    from(<span class="code-quote">"seda:foo"</span>).startupOrder(1).to(<span
class="code-quote">"mock:result"</span>);
    from(<span class="code-quote">"direct:start"</span>).startupOrder(2).to(<span
class="code-quote">"seda:foo"</span>);

    from(<span class="code-quote">"direct:bar"</span>).to(<span class="code-quote">"seda:bar"</span>);
</pre>
</div></div>

<p>And the same example with XML DSL:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
    <span class="code-tag">&lt;route startupOrder=<span class="code-quote">"1"</span>&gt;</span>
        <span class="code-tag">&lt;from uri=<span class="code-quote">"seda:foo"</span>/&gt;</span>
        <span class="code-tag">&lt;to uri=<span class="code-quote">"mock:result"</span>/&gt;</span>
    <span class="code-tag">&lt;/route&gt;</span>

    <span class="code-tag">&lt;route startupOrder=<span class="code-quote">"2"</span>&gt;</span>
        <span class="code-tag">&lt;from uri=<span class="code-quote">"direct:start"</span>/&gt;</span>
        <span class="code-tag">&lt;to uri=<span class="code-quote">"seda:foo"</span>/&gt;</span>
    <span class="code-tag">&lt;/route&gt;</span>

    <span class="code-tag">&lt;route&gt;</span>
        <span class="code-tag">&lt;from uri=<span class="code-quote">"direct:bar"</span>/&gt;</span>
        <span class="code-tag">&lt;to uri=<span class="code-quote">"seda:bar"</span>/&gt;</span>
    <span class="code-tag">&lt;/route&gt;</span>
</pre>
</div></div>

<p>In the route above we have <b>not</b> define a <tt>startupOrder</tt>
on the last route <tt>direct:bar</tt> 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.</p>

<p>So you can use this to your advantage to only assign a <tt>startupOrder</tt>
on the routes which really needs it.</p>

<h3><a name="Configuringroutestartuporderingandautostartup-Shutdown"></a>Shutdown</h3>
<p>Camel will shutdown the routes in the same order as they was started. See also <a
href="/confluence/display/CAMEL/Graceful+Shutdown" title="Graceful Shutdown">Graceful Shutdown</a>.</p>

<h3><a name="Configuringroutestartuporderingandautostartup-Seealso"></a>See
also</h3>
<ul class="alternate" type="square">
	<li><a href="/confluence/display/CAMEL/Configuring+Camel" title="Configuring Camel">Configuring
Camel</a></li>
	<li><a href="/confluence/display/CAMEL/Graceful+Shutdown" title="Graceful Shutdown">Graceful
Shutdown</a></li>
	<li><a href="/confluence/display/CAMEL/User+Guide" title="User Guide">User Guide</a></li>
</ul>

    </div>
        <div id="commentsSection" class="wiki-content pageSection">
        <div style="float: right;">
            <a href="https://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
        </div>
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/Configuring+route+startup+ordering+and+autostartup">View
Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=4587526&revisedVersion=6&originalVersion=5">View
Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/Configuring+route+startup+ordering+and+autostartup?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message