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 2017/12/13 09:15:53 UTC

[camel] 01/04: CAMEL-12056: Introduce NotifyBuilder.destroy()

This is an automated email from the ASF dual-hosted git repository.

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

commit 5f49d5aee13999057a31794031936f398568a9f0
Author: Marc Carter <dr...@fastmail.fm>
AuthorDate: Sun Dec 3 15:14:27 2017 +0000

    CAMEL-12056: Introduce NotifyBuilder.destroy()
---
 .../org/apache/camel/builder/NotifyBuilder.java    | 21 ++++++++++--
 .../apache/camel/builder/NotifyBuilderTest.java    | 39 ++++++++++++++++++++++
 2 files changed, 58 insertions(+), 2 deletions(-)

diff --git a/camel-core/src/main/java/org/apache/camel/builder/NotifyBuilder.java b/camel-core/src/main/java/org/apache/camel/builder/NotifyBuilder.java
index e5933494..0fc7045 100644
--- a/camel-core/src/main/java/org/apache/camel/builder/NotifyBuilder.java
+++ b/camel-core/src/main/java/org/apache/camel/builder/NotifyBuilder.java
@@ -39,7 +39,6 @@ import org.apache.camel.management.event.ExchangeCompletedEvent;
 import org.apache.camel.management.event.ExchangeCreatedEvent;
 import org.apache.camel.management.event.ExchangeFailedEvent;
 import org.apache.camel.management.event.ExchangeSentEvent;
-import org.apache.camel.spi.EventNotifier;
 import org.apache.camel.support.EventNotifierSupport;
 import org.apache.camel.util.EndpointHelper;
 import org.apache.camel.util.ObjectHelper;
@@ -64,7 +63,7 @@ public class NotifyBuilder {
     private final CamelContext context;
 
     // notifier to hook into Camel to listen for events
-    private final EventNotifier eventNotifier;
+    private final EventNotifierSupport eventNotifier;
 
     // the predicates build with this builder
     private final List<EventPredicateHolder> predicates = new ArrayList<EventPredicateHolder>();
@@ -1164,11 +1163,29 @@ public class NotifyBuilder {
      */
     public NotifyBuilder create() {
         doCreate(EventOperation.and);
+        if (eventNotifier.isStopped()) {
+            throw new IllegalStateException("A destroyed NotifyBuilder cannot be re-created.");
+        }
         created = true;
         return this;
     }
 
     /**
+     * De-registers this builder from its {@link CamelContext}.
+     * <p/>
+     * Once destroyed, this instance will not function again.
+     */
+    public void destroy() {
+        context.getManagementStrategy().removeEventNotifier(eventNotifier);
+        try {
+            ServiceHelper.stopService(eventNotifier);
+        } catch (Exception e) {
+            throw ObjectHelper.wrapRuntimeCamelException(e);
+        }
+        created = false;
+    }
+
+    /**
      * Does all the expression match?
      * <p/>
      * This operation will return immediately which means it can be used for testing at this very moment.
diff --git a/camel-core/src/test/java/org/apache/camel/builder/NotifyBuilderTest.java b/camel-core/src/test/java/org/apache/camel/builder/NotifyBuilderTest.java
index 8c56713..05dd247 100644
--- a/camel-core/src/test/java/org/apache/camel/builder/NotifyBuilderTest.java
+++ b/camel-core/src/test/java/org/apache/camel/builder/NotifyBuilderTest.java
@@ -36,6 +36,45 @@ public class NotifyBuilderTest extends ContextTestSupport {
         }
     }
 
+    public void testDestroyUnregistersBuilder() throws Exception {
+        // Given:
+        NotifyBuilder notify = new NotifyBuilder(context).whenDone(1).create();
+        // When:
+        int withReg = context.getManagementStrategy().getEventNotifiers().size();
+        notify.destroy();
+        int afterDestroy = context.getManagementStrategy().getEventNotifiers().size();
+        // Then:
+        assertEquals(withReg - afterDestroy, 1);
+    }
+
+    public void testDestroyResetsBuilder() throws Exception {
+        // Given:
+        NotifyBuilder notify = new NotifyBuilder(context).whenDone(1).create();
+        // When:
+        notify.destroy();
+        // Then:
+        try {
+            notify.matches();
+            fail("Should have thrown an exception");
+        } catch (IllegalStateException e) {
+            assertEquals("NotifyBuilder has not been created. Invoke the create() method before matching.", e.getMessage());
+        }
+    }
+
+    public void testDestroyedBuilderCannotBeRecreated() throws Exception {
+        // Given:
+        NotifyBuilder notify = new NotifyBuilder(context).whenDone(1).create();
+        // When:
+        notify.destroy();
+        // Then:
+        try {
+            notify.create();
+            fail("Should have thrown an exception");
+        } catch (IllegalStateException e) {
+            assertEquals("A destroyed NotifyBuilder cannot be re-created.", e.getMessage());
+        }
+    }
+
     public void testDirectWhenExchangeDoneSimple() throws Exception {
         NotifyBuilder notify = new NotifyBuilder(context)
                 .from("direct:foo").whenDone(1)

-- 
To stop receiving notification emails like this one, please contact
"commits@camel.apache.org" <co...@camel.apache.org>.