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 ffbd0c6  CAMEL-13687: NotifyBuilder - Add fromCurrentRoute 
functionality
ffbd0c6 is described below

commit ffbd0c60e956d0620512ae050390badb2785ae6a
Author: Claus Ibsen <claus.ib...@gmail.com>
AuthorDate: Mon Jul 1 17:51:42 2019 +0200

    CAMEL-13687: NotifyBuilder - Add fromCurrentRoute functionality
---
 .../org/apache/camel/builder/NotifyBuilder.java    | 44 ++++++++++++++++++++++
 .../camel/builder/NotifyBuilderFromRouteTest.java  | 28 ++++++++++++++
 2 files changed, 72 insertions(+)

diff --git 
a/core/camel-core/src/main/java/org/apache/camel/builder/NotifyBuilder.java 
b/core/camel-core/src/main/java/org/apache/camel/builder/NotifyBuilder.java
index 1fd4677..1fba0ff 100644
--- a/core/camel-core/src/main/java/org/apache/camel/builder/NotifyBuilder.java
+++ b/core/camel-core/src/main/java/org/apache/camel/builder/NotifyBuilder.java
@@ -37,6 +37,8 @@ import org.apache.camel.spi.CamelEvent.ExchangeCompletedEvent;
 import org.apache.camel.spi.CamelEvent.ExchangeCreatedEvent;
 import org.apache.camel.spi.CamelEvent.ExchangeFailedEvent;
 import org.apache.camel.spi.CamelEvent.ExchangeSentEvent;
+import org.apache.camel.spi.RouteContext;
+import org.apache.camel.spi.UnitOfWork;
 import org.apache.camel.support.EndpointHelper;
 import org.apache.camel.support.EventNotifierSupport;
 import org.apache.camel.support.PatternHelper;
@@ -179,6 +181,48 @@ public class NotifyBuilder {
         return this;
     }
 
+    /**
+     * Optionally a <tt>from</tt> current route which means that this 
expression should only be based
+     * on {@link Exchange} which is the current route(s).
+     *
+     * @param routeId id of route or pattern (see the EndpointHelper javadoc)
+     * @return the builder
+     * @see EndpointHelper#matchEndpoint(org.apache.camel.CamelContext, 
String, String)
+     */
+    public NotifyBuilder fromCurrentRoute(final String routeId) {
+        stack.add(new EventPredicateSupport() {
+
+            @Override
+            public boolean isAbstract() {
+                // is abstract as its a filter
+                return true;
+            }
+
+            @Override
+            public boolean onExchangeSent(Exchange exchange, Endpoint 
endpoint, long timeTaken) {
+                UnitOfWork uow = exchange.getUnitOfWork();
+                RouteContext rc = uow != null ? uow.getRouteContext() : null;
+                if (rc != null) {
+                    String id = rc.getRouteId();
+                    return PatternHelper.matchPattern(id, routeId);
+                } else {
+                    return false;
+                }
+            }
+
+            public boolean matches() {
+                // should be true as we use the onExchange to filter
+                return true;
+            }
+
+            @Override
+            public String toString() {
+                return "fromCurrentRoute(" + routeId + ")";
+            }
+        });
+        return this;
+    }
+
     private NotifyBuilder fromRoutesOnly() {
         // internal and should always be in top of stack
         stack.add(0, new EventPredicateSupport() {
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/builder/NotifyBuilderFromRouteTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/builder/NotifyBuilderFromRouteTest.java
index 0026a1c..850f401 100644
--- 
a/core/camel-core/src/test/java/org/apache/camel/builder/NotifyBuilderFromRouteTest.java
+++ 
b/core/camel-core/src/test/java/org/apache/camel/builder/NotifyBuilderFromRouteTest.java
@@ -42,6 +42,29 @@ public class NotifyBuilderFromRouteTest extends 
ContextTestSupport {
         assertTrue(builder.matchesMockWaitTime());
     }
 
+    @Test
+    public void testDoneFromCurrentRoute() throws Exception {
+        // notify when exchange is done
+        NotifyBuilder builder =
+                new NotifyBuilder(context).fromCurrentRoute("bar").whenDone(1);
+        builder.create();
+
+        template.sendBody("seda:foo", "Hello world!");
+
+        assertTrue(builder.matchesMockWaitTime());
+    }
+
+    @Test
+    public void testDoneFromCurrentRouteStartRoute() throws Exception {
+        // notify when exchange is done
+        NotifyBuilder builder =
+                new NotifyBuilder(context).fromCurrentRoute("foo").whenDone(1);
+        builder.create();
+
+        template.sendBody("seda:foo", "Hello world!");
+
+        assertTrue(builder.matchesMockWaitTime());
+    }
 
     @Override
     protected JndiRegistry createRegistry() throws Exception {
@@ -57,7 +80,12 @@ public class NotifyBuilderFromRouteTest extends 
ContextTestSupport {
             public void configure() throws Exception {
                 from("proxy:seda:foo")
                     .routeId("foo")
+                    .to("direct:bar")
                     .to("mock:foo");
+
+                from("direct:bar")
+                    .routeId("bar")
+                    .to("mock:bar");
             }
         };
     }

Reply via email to