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/12/29 10:09:24 UTC

[2/2] camel git commit: CAMEL-9457: Camel Main - Stop from JMX should trigger stop of the Main/JVM

CAMEL-9457: Camel Main - Stop from JMX should trigger stop of the Main/JVM


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

Branch: refs/heads/camel-2.16.x
Commit: 3d70517939a78d793347c9ba8c75fdbdcfb8b168
Parents: 76cf2f8
Author: Claus Ibsen <da...@apache.org>
Authored: Tue Dec 29 09:57:18 2015 +0100
Committer: Claus Ibsen <da...@apache.org>
Committed: Tue Dec 29 10:09:12 2015 +0100

----------------------------------------------------------------------
 .../camel/main/MainLifecycleStrategy.java       | 49 ++++++++++++++++++++
 .../java/org/apache/camel/main/MainSupport.java |  2 +
 .../java/org/apache/camel/main/MainExample.java |  4 +-
 3 files changed, 53 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/3d705179/camel-core/src/main/java/org/apache/camel/main/MainLifecycleStrategy.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/main/MainLifecycleStrategy.java b/camel-core/src/main/java/org/apache/camel/main/MainLifecycleStrategy.java
new file mode 100644
index 0000000..9011134
--- /dev/null
+++ b/camel-core/src/main/java/org/apache/camel/main/MainLifecycleStrategy.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.main;
+
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.support.LifecycleStrategySupport;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * A {@link org.apache.camel.spi.LifecycleStrategy} to trigger shutdown of the Main JVM
+ * when {@link CamelContext} is stopped from JMX or its stop method is invoked from Java code.
+ */
+public class MainLifecycleStrategy extends LifecycleStrategySupport {
+
+    private static final Logger LOG = LoggerFactory.getLogger(MainLifecycleStrategy.class);
+    private final AtomicBoolean completed;
+    private final CountDownLatch latch;
+
+    public MainLifecycleStrategy(AtomicBoolean completed, CountDownLatch latch) {
+        this.completed = completed;
+        this.latch = latch;
+    }
+
+    @Override
+    public void onContextStop(CamelContext context) {
+        LOG.info("CamelContext: {} has been shutdown, triggering shutdown of the JVM.", context.getName());
+        // trigger stopping the Main
+        completed.set(true);
+        latch.countDown();
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/3d705179/camel-core/src/main/java/org/apache/camel/main/MainSupport.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/main/MainSupport.java b/camel-core/src/main/java/org/apache/camel/main/MainSupport.java
index 0f85a71..8b42a03 100644
--- a/camel-core/src/main/java/org/apache/camel/main/MainSupport.java
+++ b/camel-core/src/main/java/org/apache/camel/main/MainSupport.java
@@ -417,6 +417,8 @@ public abstract class MainSupport extends ServiceSupport {
         for (RouteBuilder routeBuilder : routeBuilders) {
             camelContext.addRoutes(routeBuilder);
         }
+        // register lifecycle so we are notified in Camel is stopped from JMX or somewhere else
+        camelContext.addLifecycleStrategy(new MainLifecycleStrategy(completed, latch));
         // allow to do configuration before its started
         for (MainListener listener : listeners) {
             listener.configure(camelContext);

http://git-wip-us.apache.org/repos/asf/camel/blob/3d705179/camel-core/src/test/java/org/apache/camel/main/MainExample.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/main/MainExample.java b/camel-core/src/test/java/org/apache/camel/main/MainExample.java
index df64578..d5160a9 100644
--- a/camel-core/src/test/java/org/apache/camel/main/MainExample.java
+++ b/camel-core/src/test/java/org/apache/camel/main/MainExample.java
@@ -40,7 +40,7 @@ public class MainExample {
         main = new Main();
         // enable hangup support so you can press ctrl + c to terminate the JVM
         main.enableHangupSupport();
-        // bind MyBean into the registery
+        // bind MyBean into the registry
         main.bind("foo", new MyBean());
         // add routes
         main.addRouteBuilder(new MyRouteBuilder());
@@ -67,7 +67,7 @@ public class MainExample {
 
     public static class MyBean {
         public void callMe() {
-            System.out.println("MyBean.calleMe method has been called");
+            System.out.println("MyBean.callMe method has been called");
         }
     }