You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2015/04/22 11:53:38 UTC

[2/3] camel git commit: CAMEL-8682: Context scoped OnException should not be stopped if a route is stopped

CAMEL-8682: Context scoped OnException should not be stopped if a route is stopped


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/0403f066
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/0403f066
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/0403f066

Branch: refs/heads/camel-2.15.x
Commit: 0403f06668de5af9f375df35d3a6e22d503d83e9
Parents: f8d759b
Author: Claus Ibsen <da...@apache.org>
Authored: Wed Apr 22 10:50:16 2015 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Wed Apr 22 11:56:33 2015 +0200

----------------------------------------------------------------------
 .../processor/interceptor/DefaultChannel.java   |  22 +++-
 ...pedOnExceptionLoadBalancerStopRouteTest.java | 110 +++++++++++++++++++
 ...pedOnExceptionLoadBalancerStopRouteTest.java |  49 +++++++++
 3 files changed, 180 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/0403f066/camel-core/src/main/java/org/apache/camel/processor/interceptor/DefaultChannel.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/processor/interceptor/DefaultChannel.java b/camel-core/src/main/java/org/apache/camel/processor/interceptor/DefaultChannel.java
index 9338271..c9ae2f3 100644
--- a/camel-core/src/main/java/org/apache/camel/processor/interceptor/DefaultChannel.java
+++ b/camel-core/src/main/java/org/apache/camel/processor/interceptor/DefaultChannel.java
@@ -28,6 +28,8 @@ import org.apache.camel.Channel;
 import org.apache.camel.Exchange;
 import org.apache.camel.Processor;
 import org.apache.camel.model.ModelChannel;
+import org.apache.camel.model.OnCompletionDefinition;
+import org.apache.camel.model.OnExceptionDefinition;
 import org.apache.camel.model.ProcessorDefinition;
 import org.apache.camel.model.ProcessorDefinitionHelper;
 import org.apache.camel.model.RouteDefinition;
@@ -155,7 +157,25 @@ public class DefaultChannel extends CamelInternalProcessor implements ModelChann
 
     @Override
     protected void doStop() throws Exception {
-        ServiceHelper.stopServices(output, errorHandler);
+        if (!isContextScoped()) {
+            // only stop services if not context scoped (as context scoped is reused by others)
+            ServiceHelper.stopServices(output, errorHandler);
+        }
+    }
+
+    @Override
+    protected void doShutdown() throws Exception {
+        ServiceHelper.stopAndShutdownServices(output, errorHandler);
+    }
+
+    private boolean isContextScoped() {
+        if (definition instanceof OnExceptionDefinition) {
+            return !((OnExceptionDefinition) definition).isRouteScoped();
+        } else if (definition instanceof OnCompletionDefinition) {
+            return !((OnCompletionDefinition) definition).isRouteScoped();
+        }
+
+        return false;
     }
 
     @SuppressWarnings("deprecation")

http://git-wip-us.apache.org/repos/asf/camel/blob/0403f066/camel-core/src/test/java/org/apache/camel/processor/onexception/ContextScopedOnExceptionLoadBalancerStopRouteTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/processor/onexception/ContextScopedOnExceptionLoadBalancerStopRouteTest.java b/camel-core/src/test/java/org/apache/camel/processor/onexception/ContextScopedOnExceptionLoadBalancerStopRouteTest.java
new file mode 100644
index 0000000..6750231
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/processor/onexception/ContextScopedOnExceptionLoadBalancerStopRouteTest.java
@@ -0,0 +1,110 @@
+/**
+ * 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.processor.onexception;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.ServiceStatus;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.seda.SedaEndpoint;
+
+public class ContextScopedOnExceptionLoadBalancerStopRouteTest extends ContextTestSupport {
+
+    public void testOk() throws Exception {
+        getMockEndpoint("mock:error").expectedMessageCount(0);
+        getMockEndpoint("mock:start").expectedBodiesReceived("World");
+        getMockEndpoint("mock:result").expectedBodiesReceived("Bye World");
+        getMockEndpoint("mock:exception").expectedMessageCount(0);
+
+        template.sendBody("direct:start", "World");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    public void testError() throws Exception {
+        getMockEndpoint("mock:error").expectedBodiesReceived("Kaboom");
+        getMockEndpoint("mock:start").expectedBodiesReceived("Kaboom");
+        getMockEndpoint("mock:result").expectedMessageCount(0);
+        getMockEndpoint("mock:exception").expectedBodiesReceived("Kaboom");
+
+        template.sendBody("direct:start", "Kaboom");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    public void testErrorOk() throws Exception {
+        getMockEndpoint("mock:error").expectedBodiesReceived("Kaboom");
+        getMockEndpoint("mock:start").expectedBodiesReceived("Kaboom", "World");
+        getMockEndpoint("mock:result").expectedBodiesReceived("Bye World");
+        getMockEndpoint("mock:exception").expectedBodiesReceived("Kaboom");
+
+        template.sendBody("direct:start", "Kaboom");
+        template.sendBody("direct:start", "World");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    public void testErrorOkError() throws Exception {
+        getMockEndpoint("mock:error").expectedBodiesReceived("Kaboom");
+        getMockEndpoint("mock:start").expectedBodiesReceived("Kaboom", "World", "Kaboom");
+        getMockEndpoint("mock:result").expectedBodiesReceived("Bye World");
+        getMockEndpoint("mock:exception").expectedBodiesReceived("Kaboom", "Kaboom");
+
+        template.sendBody("direct:start", "Kaboom");
+        template.sendBody("direct:start", "World");
+
+        // give time for route to stop
+        Thread.sleep(1000);
+        assertEquals(ServiceStatus.Stopped, context.getRouteStatus("errorRoute"));
+
+        template.sendBody("direct:start", "Kaboom");
+
+        assertMockEndpointsSatisfied();
+
+        // should be 1 on the seda queue
+        SedaEndpoint seda = getMandatoryEndpoint("seda:error", SedaEndpoint.class);
+        SedaEndpoint seda2 = getMandatoryEndpoint("seda:error2", SedaEndpoint.class);
+        int size = seda.getQueue().size();
+        int size2 = seda2.getQueue().size();
+        assertTrue("There should be 1 exchange on the seda or seda2 queue", size == 1 || size2 == 1);
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                onException(Exception.class)
+                    .handled(true)
+                    .loadBalance().roundRobin().to("seda:error", "seda:error2").end()
+                    .to("mock:exception");
+
+                from("direct:start")
+                    .to("mock:start")
+                    .choice()
+                        .when(body().contains("Kaboom"))
+                            .throwException(new IllegalArgumentException("Forced"))
+                        .otherwise()
+                            .transform(body().prepend("Bye "))
+                    .to("mock:result");
+
+                from("seda:error").routeId("errorRoute")
+                    .to("controlbus:route?action=stop&routeId=errorRoute&async=true")
+                    .to("mock:error");
+            }
+        };
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/0403f066/camel-core/src/test/java/org/apache/camel/processor/onexception/RouteScopedOnExceptionLoadBalancerStopRouteTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/processor/onexception/RouteScopedOnExceptionLoadBalancerStopRouteTest.java b/camel-core/src/test/java/org/apache/camel/processor/onexception/RouteScopedOnExceptionLoadBalancerStopRouteTest.java
new file mode 100644
index 0000000..3aa06c3
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/processor/onexception/RouteScopedOnExceptionLoadBalancerStopRouteTest.java
@@ -0,0 +1,49 @@
+/**
+ * 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.processor.onexception;
+
+import org.apache.camel.builder.RouteBuilder;
+
+public class RouteScopedOnExceptionLoadBalancerStopRouteTest extends ContextScopedOnExceptionLoadBalancerStopRouteTest {
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start")
+                    .onException(Exception.class)
+                        .handled(true)
+                        .loadBalance().roundRobin().to("seda:error", "seda:error2").end()
+                        .to("mock:exception")
+                    .end()
+                    // route starts here
+                    .to("mock:start")
+                    .choice()
+                        .when(body().contains("Kaboom"))
+                            .throwException(new IllegalArgumentException("Forced"))
+                        .otherwise()
+                            .transform(body().prepend("Bye "))
+                    .to("mock:result");
+
+                from("seda:error").routeId("errorRoute")
+                    .to("controlbus:route?action=stop&routeId=errorRoute&async=true")
+                    .to("mock:error");
+            }
+        };
+    }
+}