camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Claus Ibsen (Confluence)" <conflue...@apache.org>
Subject [CONF] Apache Camel > How can I stop a route from a route
Date Tue, 17 Sep 2013 13:42:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/en/2176/1/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 (2)</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" >See more details about the [Lifecycle].
<br> <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">{tip}
<br>You can also use the [ControlBus] component to let it stop/start routes. <br>{tip}
<br> <br></td></tr>
            <tr><td class="diff-unchanged" >h3. See Also <br>- [RoutePolicy]
<br>- [Graceful Shutdown] <br>- [Lifecycle] <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">-
[ControlBus] <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="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<script type="syntaxhighlighter" class="theme: Default; brush: java; gutter: false"><![CDATA[
// use a latch as signal when to stop Camel
private final CountDownLatch latch = new CountDownLatch(1);

public void testStopCamelFromRoute() throws Exception {
    // create camel, add routes, and start camel
    CamelContext context = new DefaultCamelContext();
    context.addRoutes(createMyRoutes());
    context.start();

    // setup mock expectations for unit test
    MockEndpoint start = context.getEndpoint("mock:start", MockEndpoint.class);
    start.expectedMessageCount(1);
    MockEndpoint done = context.getEndpoint("mock:done", MockEndpoint.class);
    done.expectedMessageCount(1);

    // send a message to the route
    ProducerTemplate template = context.createProducerTemplate();
    template.sendBody("direct:start", "Hello Camel");

    // wait for the latch (use 1 minute as fail safe, due unit test)
    assertTrue(latch.await(1, TimeUnit.MINUTES));

    // stop camel
    context.stop();

    // unit test assertions
    start.assertIsSatisfied();
    done.assertIsSatisfied();
}
]]></script>
</div></div>
<p>And in the route we call the latch as shown:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<script type="syntaxhighlighter" class="theme: Default; brush: java; gutter: false"><![CDATA[
public RouteBuilder createMyRoutes() throws Exception {
    return new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct:start").routeId("myRoute")
                .to("mock:start")
                .process(new Processor() {
                    @Override
                    public void process(Exchange exchange) throws Exception {
                        // stop Camel by signalling to the latch
                        latch.countDown();
                    }
                }).to("mock:done");
        }
    };
}
]]></script>
</div></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="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<script type="syntaxhighlighter" class="theme: Default; brush: java; gutter: false"><![CDATA[
public void testStopRouteFromRoute() throws Exception {
    // create camel, add routes, and start camel
    CamelContext context = new DefaultCamelContext();
    context.addRoutes(createMyRoutes());
    context.start();

    assertTrue("Route myRoute should be started", context.getRouteStatus("myRoute").isStarted());
    assertTrue("Route bar should be started", context.getRouteStatus("bar").isStarted());

    // setup mock expectations for unit test
    MockEndpoint start = context.getEndpoint("mock:start", MockEndpoint.class);
    start.expectedMessageCount(1);
    MockEndpoint done = context.getEndpoint("mock:done", MockEndpoint.class);
    done.expectedMessageCount(1);

    // send a message to the route
    ProducerTemplate template = context.createProducerTemplate();
    template.sendBody("direct:start", "Hello Camel");

    // just wait a bit for the thread to stop the route
    Thread.sleep(1500);

    // the route should now be stopped
    assertTrue("Route myRoute should be stopped", context.getRouteStatus("myRoute").isStopped());
    assertTrue("Route bar should be started", context.getRouteStatus("bar").isStarted());

    // stop camel
    context.stop();

    // unit test assertions
    start.assertIsSatisfied();
    done.assertIsSatisfied();
}
]]></script>
</div></div>
<p>And in the route we create the thread and call the <tt>stopRoute</tt>
method as shown:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<script type="syntaxhighlighter" class="theme: Default; brush: java; gutter: false"><![CDATA[
public RouteBuilder createMyRoutes() throws Exception {
    return new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct:start").routeId("myRoute")
                .to("mock:start")
                .process(new Processor() {
                    Thread stop;

                    @Override
                    public void process(final Exchange exchange) throws Exception {
                        // stop this route using a thread that will stop
                        // this route gracefully while we are still running
                        if (stop == null) {
                            stop = new Thread() {
                                @Override
                                public void run() {
                                    try {
                                        exchange.getContext().stopRoute("myRoute");
                                    } catch (Exception e) {
                                        // ignore
                                    }
                                }
                            };
                        }

                        // start the thread that stops this route
                        stop.start();
                    }
                }).to("mock:done");
            
            from("direct:bar").routeId("bar")
                .to("mock:bar");
        }
    };
}
]]></script>
</div></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>

<div class='panelMacro'><table class='tipMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/check.gif" width="16" height="16"
align="absmiddle" alt="" border="0"></td><td>You can also use the <a href="/confluence/display/CAMEL/ControlBus"
title="ControlBus">ControlBus</a> component to let it stop/start routes.</td></tr></table></div>

<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>
	<li><a href="/confluence/display/CAMEL/ControlBus" title="ControlBus">ControlBus</a></li>
</ul>

    </div>
        <div id="commentsSection" class="wiki-content pageSection">
        <div style="float: right;" class="grey">
                        <a href="https://cwiki.apache.org/confluence/users/removespacenotification.action?spaceKey=CAMEL">Stop
watching space</a>
            <span style="padding: 0px 5px;">|</span>
                <a href="https://cwiki.apache.org/confluence/users/editmyemailsettings.action">Change
email 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=5&originalVersion=4">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