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 2016/01/26 18:44:10 UTC

camel git commit: CAMEL-9449 - Allow to specify custom exit code when camel exits due to expired Duration

Repository: camel
Updated Branches:
  refs/heads/master 09e0b4663 -> f24361075


CAMEL-9449 - Allow to specify custom exit code when camel exits due to expired Duration


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

Branch: refs/heads/master
Commit: f2436107592f69ea1ed179f8bbfc9d2add853e16
Parents: 09e0b46
Author: lburgazzoli <lb...@gmail.com>
Authored: Tue Jan 26 17:52:58 2016 +0100
Committer: Claus Ibsen <da...@apache.org>
Committed: Tue Jan 26 18:44:03 2016 +0100

----------------------------------------------------------------------
 .../main/java/org/apache/camel/main/Main.java   |  2 ++
 .../java/org/apache/camel/main/MainSupport.java | 30 ++++++++++++++++++++
 2 files changed, 32 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/f2436107/camel-core/src/main/java/org/apache/camel/main/Main.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/main/Main.java b/camel-core/src/main/java/org/apache/camel/main/Main.java
index 5a3287a..6b012ae 100644
--- a/camel-core/src/main/java/org/apache/camel/main/Main.java
+++ b/camel-core/src/main/java/org/apache/camel/main/Main.java
@@ -43,6 +43,8 @@ public class Main extends MainSupport {
         Main main = new Main();
         instance = main;
         main.run(args);
+
+        System.exit(main.getExitCode());
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/camel/blob/f2436107/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 7dd337e..e015e55 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
@@ -26,6 +26,7 @@ import java.util.Set;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicInteger;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.ProducerTemplate;
@@ -46,10 +47,13 @@ import org.slf4j.LoggerFactory;
  */
 public abstract class MainSupport extends ServiceSupport {
     protected static final Logger LOG = LoggerFactory.getLogger(MainSupport.class);
+    protected static final int UNINITIALIZED_EXIT_CODE = Integer.MIN_VALUE;
+    protected static final int DEFAULT_EXIT_CODE = 0;
     protected final List<MainListener> listeners = new ArrayList<MainListener>();
     protected final List<Option> options = new ArrayList<Option>();
     protected final CountDownLatch latch = new CountDownLatch(1);
     protected final AtomicBoolean completed = new AtomicBoolean(false);
+    protected final AtomicInteger exitCode = new AtomicInteger(UNINITIALIZED_EXIT_CODE);
     protected long duration = -1;
     protected TimeUnit timeUnit = TimeUnit.MILLISECONDS;
     protected boolean trace;
@@ -58,6 +62,7 @@ public abstract class MainSupport extends ServiceSupport {
     protected final List<CamelContext> camelContexts = new ArrayList<CamelContext>();
     protected ProducerTemplate camelTemplate;
     protected boolean hangupInterceptorEnabled = true;
+    protected int durationHitExitCode = DEFAULT_EXIT_CODE;
 
     /**
      * A class for intercepting the hang up signal and do a graceful shutdown of the Camel.
@@ -113,6 +118,13 @@ public abstract class MainSupport extends ServiceSupport {
                 enableTrace();
             }
         });
+        addOption(new ParameterOption("e", "exitcode",
+                "Sets the exit code if duration was hit",
+                "exitcode")  {
+            protected void doProcess(String arg, String parameter, LinkedList<String> remainingArgs) {
+                setDurationHitExitCode(Integer.parseInt(parameter));
+            }
+        });
     }
 
     /**
@@ -230,6 +242,7 @@ public abstract class MainSupport extends ServiceSupport {
      */
     public void completed() {
         completed.set(true);
+        exitCode.compareAndSet(UNINITIALIZED_EXIT_CODE, DEFAULT_EXIT_CODE);
         latch.countDown();
     }
 
@@ -301,6 +314,22 @@ public abstract class MainSupport extends ServiceSupport {
         this.timeUnit = timeUnit;
     }
 
+    /**
+     * Sets the exit code for the application if duration was hit
+     */
+    public void setDurationHitExitCode(int durationHitExitCode) {
+        this.durationHitExitCode = durationHitExitCode;
+    }
+
+    public int getDurationHitExitCode() {
+        return durationHitExitCode;
+    }
+
+    public int getExitCode() {
+        return exitCode.get();
+    }
+
+
     public void setRouteBuilderClasses(String builders) {
         this.routeBuilderClasses = builders;
     }
@@ -332,6 +361,7 @@ public abstract class MainSupport extends ServiceSupport {
                     TimeUnit unit = getTimeUnit();
                     LOG.info("Waiting for: " + duration + " " + unit);
                     latch.await(duration, unit);
+                    exitCode.compareAndSet(UNINITIALIZED_EXIT_CODE, durationHitExitCode);
                     completed.set(true);
                 } else {
                     latch.await();