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 Mon, 06 Feb 2012 06:03: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 (6)</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" > <br>Stopping a route during
routing an existing message is a bit tricky. The reason for that is Camel will [Graceful Shutdown]
the route you are stopping. And if you do that while a message is being routed the [Graceful
Shutdown] will try to wait until that message has been processed. <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">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 [Processor]: <br></td></tr>
            <tr><td class="diff-unchanged" > <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">{code:java}
<br>from(&quot;direct:start&quot;) <br>    .to(&quot;bean:foo?method=doSomething&quot;).routeId(&quot;myCoolRoute&quot;)
<br>    .process(new Processor() {  <br>        public void process(Exchange exchange)
throws Exception { <br>            // remove myself from the in flight registry so we
can stop this route without trouble <br>            context.getInflightRepository().remove(exchange);
<br>            // stop this route <br>            context.stopRoute(&quot;myCoolRoute&quot;);
<br>    }); <br>{code} <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">The
best practice for stopping a route from a route, is to either <br>- signal to another
thread to stop the route <br>- spin off a new thread to stop the route <br></td></tr>
            <tr><td class="diff-unchanged" > <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">*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></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">Using
another thread to stop the route is also what is normally used when stopping Camel itself,
or for example when an application in a server is stopped etc. Its too tricky and hard to
stop a route using the same thread that currently is processing a message from the route.
This is not advised to do, and can cause unforeseen side effects. <br></td></tr>
            <tr><td class="diff-unchanged" > <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">h3.
Using a latch to stop Camel from a route <br>In this example we use a {{CountdownLatch}}
to signal when Camel should stop, triggered from a route. <br>{snippet:id=e1|lang=java|url=camel/trunk/camel-core/src/test/java/org/apache/camel/issues/StopCamelFromRouteTest.java}
<br>And in the route we call the latch as shown: <br>{snippet:id=e2|lang=java|url=camel/trunk/camel-core/src/test/java/org/apache/camel/issues/StopCamelFromRouteTest.java}
<br> <br>h3. Using a thread to stop a route from a route <br>In this example
we use a separate {{Thread}} to stop the route, triggered from the route itself. <br>{snippet:id=e1|lang=java|url=camel/trunk/camel-core/src/test/java/org/apache/camel/issues/StopRouteFromRouteTest.java}
<br>And in the route we create the thread and call the {{stopRoute}} method as shown:
<br>{snippet:id=e2|lang=java|url=camel/trunk/camel-core/src/test/java/org/apache/camel/issues/StopRouteFromRouteTest.java}
<br> <br> <br>h3. Alternative solutions <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.</p>

<p>The best practice for stopping a route from a route, is to either</p>
<ul class="alternate" type="square">
	<li>signal to another thread to stop the route</li>
	<li>spin off a new thread to stop the route</li>
</ul>


<p>Using another thread to stop the route is also what is normally used when stopping
Camel itself, or for example when an application in a server is stopped etc. Its too tricky
and hard to stop a route using the same thread that currently is processing a message from
the route. This is not advised to do, and can cause unforeseen side effects.</p>

<h3><a name="HowcanIstoparoutefromaroute-UsingalatchtostopCamelfromaroute"></a>Using
a latch to stop Camel from a route</h3>
<p>In this example we use a <tt>CountdownLatch</tt> to signal when Camel
should stop, triggered from a route.</p>
<div class="error"><span class="error">Error formatting macro: snippet: java.lang.IndexOutOfBoundsException:
Index: 20, Size: 20</span> </div>
<p>And in the route we call the latch as shown:</p>
<div class="error"><span class="error">Error formatting macro: snippet: java.lang.IndexOutOfBoundsException:
Index: 20, Size: 20</span> </div>

<h3><a name="HowcanIstoparoutefromaroute-Usingathreadtostoparoutefromaroute"></a>Using
a thread to stop a route from a route</h3>
<p>In this example we use a separate <tt>Thread</tt> to stop the route,
triggered from the route itself.</p>
<div class="error"><span class="error">Error formatting macro: snippet: java.lang.IndexOutOfBoundsException:
Index: 20, Size: 20</span> </div>
<p>And in the route we create the thread and call the <tt>stopRoute</tt>
method as shown:</p>
<div class="error"><span class="error">Error formatting macro: snippet: java.lang.IndexOutOfBoundsException:
Index: 20, Size: 20</span> </div>


<h3><a name="HowcanIstoparoutefromaroute-Alternativesolutions"></a>Alternative
solutions</h3>
<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=4&originalVersion=3">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