[ 
https://issues.apache.org/jira/browse/CAMEL-12056?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16288894#comment-16288894
 ] 

ASF GitHub Bot commented on CAMEL-12056:
----------------------------------------

davsclaus closed pull request #2128: CAMEL-12056 Add NotifyBuilder.destroy() 
method
URL: https://github.com/apache/camel/pull/2128
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

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 e5933494d67..1d74bfa4af9 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,11 +39,11 @@
 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;
 import org.apache.camel.util.ServiceHelper;
+import org.apache.camel.util.StringHelper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -64,16 +64,16 @@
     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>();
+    private final List<EventPredicateHolder> predicates = new ArrayList<>();
 
     // latch to be used to signal predicates matches
     private CountDownLatch latch = new CountDownLatch(1);
 
     // the current state while building an event predicate where we use a 
stack and the operation
-    private final List<EventPredicate> stack = new ArrayList<EventPredicate>();
+    private final List<EventPredicate> stack = new ArrayList<>();
     private EventOperation operation;
     private boolean created;
     // keep state of how many wereSentTo we have added
@@ -252,7 +252,7 @@ public String toString() {
      * @return the builder
      */
     public ExpressionClauseSupport<NotifyBuilder> filter() {
-        final ExpressionClauseSupport<NotifyBuilder> clause = new 
ExpressionClauseSupport<NotifyBuilder>(this);
+        final ExpressionClauseSupport<NotifyBuilder> clause = new 
ExpressionClauseSupport<>(this);
         stack.add(new EventPredicateSupport() {
 
             @Override
@@ -295,7 +295,7 @@ public String toString() {
     public NotifyBuilder wereSentTo(final String endpointUri) {
         // insert in start of stack but after the previous wereSentTo
         stack.add(wereSentToIndex++, new EventPredicateSupport() {
-            private ConcurrentMap<String, String> sentTo = new 
ConcurrentHashMap<String, String>();
+            private ConcurrentMap<String, String> sentTo = new 
ConcurrentHashMap<>();
 
             @Override
             public boolean isAbstract() {
@@ -1054,7 +1054,7 @@ public NotifyBuilder whenExactBodiesReceived(Object... 
bodies) {
      * @see #whenExactBodiesDone(Object...)
      */
     public NotifyBuilder whenExactBodiesDone(Object... bodies) {
-        List<Object> bodyList = new ArrayList<Object>();
+        List<Object> bodyList = new ArrayList<>();
         bodyList.addAll(Arrays.asList(bodies));
         return doWhenBodies(bodyList, false, true);
     }
@@ -1164,10 +1164,28 @@ public NotifyBuilder not() {
      */
     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/>
@@ -1261,7 +1279,7 @@ public String toString() {
             sb.append(eventPredicateHolder.toString());
         }
         // a crude way of skipping the first invisible operation
-        return ObjectHelper.after(sb.toString(), "().");
+        return StringHelper.after(sb.toString(), "().");
     }
 
     private void doCreate(EventOperation newOperation) {
@@ -1401,7 +1419,7 @@ protected void doStop() throws Exception {
     }
 
     private enum EventOperation {
-        and, or, not;
+        and, or, not
     }
 
     private interface EventPredicate {
@@ -1526,7 +1544,7 @@ public String toString() {
      */
     private final class CompoundEventPredicate implements EventPredicate {
 
-        private List<EventPredicate> predicates = new 
ArrayList<EventPredicate>();
+        private List<EventPredicate> predicates = new ArrayList<>();
 
         private CompoundEventPredicate(List<EventPredicate> predicates) {
             this.predicates.addAll(predicates);
@@ -1595,7 +1613,7 @@ public boolean onExchangeFailed(Exchange exchange) {
         public boolean onExchangeSent(Exchange exchange, Endpoint endpoint, 
long timeTaken) {
             for (EventPredicate predicate : predicates) {
                 boolean answer = predicate.onExchangeSent(exchange, endpoint, 
timeTaken);
-                LOG.trace("onExchangeSent() {} {} -> {}", new 
Object[]{endpoint, predicate, answer});
+                LOG.trace("onExchangeSent() {} {} -> {}", endpoint, predicate, 
answer);
                 if (!answer) {
                     // break at first false
                     return false;
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 8c5671308f9..05dd247bf0b 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 void testMustBeCreated() throws Exception {
         }
     }
 
+    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)


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


> Add NotifyBuilder.destroy() method
> ----------------------------------
>
>                 Key: CAMEL-12056
>                 URL: https://issues.apache.org/jira/browse/CAMEL-12056
>             Project: Camel
>          Issue Type: Improvement
>          Components: camel-core
>    Affects Versions: 2.20.1
>            Reporter: Marc Carter
>
> Once constructed, NotifyBuilders remain registered against their CamelContext 
> with no (neat) means of getting rid of them.
> Dev: Add a destroy() method to complement the create() method. Destroy is 
> irreversible, object should be unusable after this (much as when !created).



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

Reply via email to