camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [camel] branch master updated: CAMEL-7970: Added destroy callback to camel context tracker.
Date Tue, 06 Aug 2019 05:20:55 GMT
This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/master by this push:
     new ea130fb  CAMEL-7970: Added destroy callback to camel context tracker.
ea130fb is described below

commit ea130fb0c652a580b850b55eefc2dd05ce6011d9
Author: Claus Ibsen <claus.ibsen@gmail.com>
AuthorDate: Tue Aug 6 07:20:38 2019 +0200

    CAMEL-7970: Added destroy callback to camel context tracker.
---
 .../org/apache/camel/spi/CamelContextTracker.java  | 25 +++++++++++++++++-----
 .../camel/impl/engine/AbstractCamelContext.java    |  8 ++++---
 .../apache/camel/impl/CamelContextTrackerTest.java | 13 +++++------
 3 files changed, 30 insertions(+), 16 deletions(-)

diff --git a/core/camel-api/src/main/java/org/apache/camel/spi/CamelContextTracker.java b/core/camel-api/src/main/java/org/apache/camel/spi/CamelContextTracker.java
index b3282da..7318db5 100644
--- a/core/camel-api/src/main/java/org/apache/camel/spi/CamelContextTracker.java
+++ b/core/camel-api/src/main/java/org/apache/camel/spi/CamelContextTracker.java
@@ -48,11 +48,7 @@ public class CamelContextTracker implements Closeable {
     private final Filter filter;
 
     public CamelContextTracker() {
-        filter = new Filter() {
-            public boolean accept(CamelContext camelContext) {
-                return !camelContext.getClass().getName().contains("Proxy");
-            }
-        };
+        filter = camelContext -> !camelContext.getClass().getName().contains("Proxy");
     }
 
     public CamelContextTracker(Filter filter) {
@@ -73,6 +69,13 @@ public class CamelContextTracker implements Closeable {
         // do nothing
     }
 
+    /**
+     * Called when a context has been shutdown.
+     */
+    public void contextDestroyed(CamelContext camelContext) {
+        // do nothing
+    }
+
     public final void open() {
         TRACKERS.add(this);
     }
@@ -93,4 +96,16 @@ public class CamelContextTracker implements Closeable {
             }
         }
     }
+
+    public static synchronized void notifyContextDestroyed(CamelContext camelContext) {
+        for (CamelContextTracker tracker : TRACKERS) {
+            try {
+                if (tracker.accept(camelContext)) {
+                    tracker.contextDestroyed(camelContext);
+                }
+            } catch (Exception e) {
+                LOG.warn("Error calling CamelContext tracker. This exception is ignored.",
e);
+            }
+        }
+    }
 }
\ No newline at end of file
diff --git a/core/camel-base/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
b/core/camel-base/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
index 4aba49c..29e2fdf 100644
--- a/core/camel-base/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
+++ b/core/camel-base/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
@@ -323,9 +323,7 @@ public abstract class AbstractCamelContext extends ServiceSupport implements
Ext
     @Override
     public void doInit() throws Exception {
         // setup management first since end users may use it to add event
-        // notifiers
-        // using the management strategy before the CamelContext has been
-        // started
+        // notifiers using the management strategy before the CamelContext has been started
         setupManagement(null);
 
         // Call all registered trackers with this context
@@ -2817,6 +2815,10 @@ public abstract class AbstractCamelContext extends ServiceSupport implements
Ext
 
         // and clear start date
         startDate = null;
+
+        // Call all registered trackers with this context
+        // Note, this may use a partially constructed object
+        CamelContextTracker.notifyContextDestroyed(this);
     }
 
     /**
diff --git a/core/camel-core/src/test/java/org/apache/camel/impl/CamelContextTrackerTest.java
b/core/camel-core/src/test/java/org/apache/camel/impl/CamelContextTrackerTest.java
index 56be9d0..b1dd603 100644
--- a/core/camel-core/src/test/java/org/apache/camel/impl/CamelContextTrackerTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/impl/CamelContextTrackerTest.java
@@ -21,7 +21,6 @@ import java.util.List;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.spi.CamelContextTracker;
-import org.apache.camel.support.LifecycleStrategySupport;
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -33,19 +32,17 @@ public class CamelContextTrackerTest extends Assert {
 
         @Override
         public void contextCreated(CamelContext camelContext) {
-            camelContext.addLifecycleStrategy(new LifecycleStrategySupport() {
-                @Override
-                public void onContextStop(CamelContext context) {
-                    names.remove(context.getName());
-                }
-            });
             names.add(camelContext.getName());
         }
+
+        @Override
+        public void contextDestroyed(CamelContext camelContext) {
+            names.remove(camelContext.getName());
+        }
     }
 
     @Test
     public void testContainerSet() throws Exception {
-
         MyContextTracker tracker = new MyContextTracker();
 
         CamelContext camel1 = new DefaultCamelContext();


Mime
View raw message