camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > How can I stop a route from a route
Date Sun, 05 Feb 2012 15:33: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/How+can+I+stop+a+route+from+a+route">How
can I stop a route from a route</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~davsclaus">Claus
Ibsen</a>
    </h4>
        <br/>
                         <h4>Changes (1)</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-added-lines" style="background-color: #dfd;">*Important:*
It is *not* best practice to stop a route as shown above, as stopping a route using the same
thread that routes messages can be tricky. Its better to spin off a separate thread that stops
the route, or to use a boolean flag, and then flip the flag, from the route. And then have
another process that monitors that flag, and reacts, to stop the route.  <br> <br></td></tr>
            <tr><td class="diff-unchanged" >Camel provides another feature for
managing routes at runtime which is [RoutePolicy]. <br> <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h2><a name="HowcanIstoparoutefromaroute-HowcanIstoparoutefromaroute"></a>How
can I stop a route from a route</h2>

<p>The <a href="/confluence/display/CAMEL/CamelContext" title="CamelContext">CamelContext</a>
provides API for managing routes at runtime. It has a <tt>stopRoute(id)</tt> and
<tt>startRoute(id)</tt> methods.</p>

<p>Stopping a route during routing an existing message is a bit tricky. The reason for
that is Camel will <a href="/confluence/display/CAMEL/Graceful+Shutdown" title="Graceful
Shutdown">Graceful Shutdown</a> the route you are stopping. And if you do that while
a message is being routed the <a href="/confluence/display/CAMEL/Graceful+Shutdown" title="Graceful
Shutdown">Graceful Shutdown</a> will try to wait until that message has been processed.<br/>
Now that message can easily be yourself. So to cater for that you have to tell Camel that
you are done routing this message which you do by removing it from the in flight registry.
The follow code shows how you can stop a route from a <a href="/confluence/display/CAMEL/Processor"
title="Processor">Processor</a>:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"direct:start"</span>)
    .to(<span class="code-quote">"bean:foo?method=doSomething"</span>).routeId(<span
class="code-quote">"myCoolRoute"</span>)
    .process(<span class="code-keyword">new</span> Processor() { 
        <span class="code-keyword">public</span> void process(Exchange exchange)
<span class="code-keyword">throws</span> Exception {
            <span class="code-comment">// remove myself from the in flight registry
so we can stop <span class="code-keyword">this</span> route without trouble
</span>            context.getInflightRepository().remove(exchange);
            <span class="code-comment">// stop <span class="code-keyword">this</span>
route
</span>            context.stopRoute(<span class="code-quote">"myCoolRoute"</span>);
    });
</pre>
</div></div>

<p><b>Important:</b> It is <b>not</b> best practice to stop
a route as shown above, as stopping a route using the same thread that routes messages can
be tricky. Its better to spin off a separate thread that stops the route, or to use a boolean
flag, and then flip the flag, from the route. And then have another process that monitors
that flag, and reacts, to stop the route. </p>

<p>Camel provides another feature for managing routes at runtime which is <a href="/confluence/display/CAMEL/RoutePolicy"
title="RoutePolicy">RoutePolicy</a>.</p>

<p>And <a href="/confluence/display/CAMEL/CamelContext" title="CamelContext">CamelContext</a>
also provides API for suspend/resume of routes, and shutdown as well.</p>
<ul class="alternate" type="square">
	<li>suspend/resume is faster than stop/start. For example a HTTP server will still
run but deny any incoming requests.<br/>
  Where as if it was stopped the HTTP listener would have been stopped.</li>
	<li>shutdown means the route is being removed from <a href="/confluence/display/CAMEL/CamelContext"
title="CamelContext">CamelContext</a> and cannot be started again. Its also removed
from JMX.<br/>
  A route must have been stopped prior to be shutdown.</li>
</ul>


<p>See more details about the <a href="/confluence/display/CAMEL/Lifecycle" title="Lifecycle">Lifecycle</a>.</p>

<h3><a name="HowcanIstoparoutefromaroute-SeeAlso"></a>See Also</h3>
<ul class="alternate" type="square">
	<li><a href="/confluence/display/CAMEL/RoutePolicy" title="RoutePolicy">RoutePolicy</a></li>
	<li><a href="/confluence/display/CAMEL/Graceful+Shutdown" title="Graceful Shutdown">Graceful
Shutdown</a></li>
	<li><a href="/confluence/display/CAMEL/Lifecycle" title="Lifecycle">Lifecycle</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/How+can+I+stop+a+route+from+a+route">View
Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=23339650&revisedVersion=3&originalVersion=2">View
Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/How+can+I+stop+a+route+from+a+route?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message