This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch camel-2.x in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/camel-2.x by this push: new 4ce2007 CAMEL-13687: NotifyBuilder - Add fromCurrentRoute functionality 4ce2007 is described below commit 4ce20076a063e52086b93e53773925bb61857391 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Mon Jul 1 18:01:22 2019 +0200 CAMEL-13687: NotifyBuilder - Add fromCurrentRoute functionality --- .../org/apache/camel/builder/NotifyBuilder.java | 44 ++++++++++++++++++++++ .../camel/builder/NotifyBuilderFromRouteTest.java | 16 ++++++++ 2 files changed, 60 insertions(+) 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 275bd25..7f210ee 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,6 +39,8 @@ 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.RouteContext; +import org.apache.camel.spi.UnitOfWork; import org.apache.camel.support.EventNotifierSupport; import org.apache.camel.util.EndpointHelper; import org.apache.camel.util.ObjectHelper; @@ -176,6 +178,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.getRoute().getId(); + return EndpointHelper.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/camel-core/src/test/java/org/apache/camel/builder/NotifyBuilderFromRouteTest.java b/camel-core/src/test/java/org/apache/camel/builder/NotifyBuilderFromRouteTest.java index b945b0c..c2a4f33 100644 --- a/camel-core/src/test/java/org/apache/camel/builder/NotifyBuilderFromRouteTest.java +++ b/camel-core/src/test/java/org/apache/camel/builder/NotifyBuilderFromRouteTest.java @@ -45,6 +45,17 @@ 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()); + } @Override protected JndiRegistry createRegistry() throws Exception { @@ -60,7 +71,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"); } }; }