camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r1241000 - in /camel/trunk/camel-core/src/test/java/org/apache/camel/issues: StopCamelFromRouteTest.java StopRouteFromRouteTest.java
Date Mon, 06 Feb 2012 12:47:08 GMT
Author: davsclaus
Date: Mon Feb  6 12:47:08 2012
New Revision: 1241000

URL: http://svn.apache.org/viewvc?rev=1241000&view=rev
Log:
Added FAQ examples

Added:
    camel/trunk/camel-core/src/test/java/org/apache/camel/issues/StopCamelFromRouteTest.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/issues/StopRouteFromRouteTest.java

Added: camel/trunk/camel-core/src/test/java/org/apache/camel/issues/StopCamelFromRouteTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/issues/StopCamelFromRouteTest.java?rev=1241000&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/issues/StopCamelFromRouteTest.java
(added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/issues/StopCamelFromRouteTest.java
Mon Feb  6 12:47:08 2012
@@ -0,0 +1,86 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.issues;
+
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+import junit.framework.TestCase;
+import org.apache.camel.CamelContext;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.ProducerTemplate;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.impl.DefaultCamelContext;
+
+/**
+ *
+ */
+public class StopCamelFromRouteTest extends TestCase {
+
+    // START SNIPPET: e1
+    // 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)
+        latch.await(1, TimeUnit.MINUTES);
+
+        // stop camel
+        context.stop();
+
+        // unit test assertions
+        start.assertIsSatisfied();
+        done.assertIsSatisfied();
+    }
+    // END SNIPPET: e1
+
+    // START SNIPPET: e2
+    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");
+            }
+        };
+    }
+    // END SNIPPET: e2
+}

Added: camel/trunk/camel-core/src/test/java/org/apache/camel/issues/StopRouteFromRouteTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/issues/StopRouteFromRouteTest.java?rev=1241000&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/issues/StopRouteFromRouteTest.java
(added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/issues/StopRouteFromRouteTest.java
Mon Feb  6 12:47:08 2012
@@ -0,0 +1,107 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.issues;
+
+import junit.framework.TestCase;
+import org.apache.camel.CamelContext;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.ProducerTemplate;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.impl.DefaultCamelContext;
+
+/**
+ *
+ */
+public class StopRouteFromRouteTest extends TestCase {
+
+    // START SNIPPET: e1
+    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(1000);
+
+        // 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();
+    }
+    // END SNIPPET: e1
+
+    // START SNIPPET: e2
+    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");
+            }
+        };
+    }
+    // END SNIPPET: e2
+}



Mime
View raw message