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 2010/01/16 10:06:25 UTC
svn commit: r899909 - in /camel/trunk/camel-core/src:
main/java/org/apache/camel/builder/NotifyBuilder.java
test/java/org/apache/camel/builder/NotifyBuilderTest.java
Author: davsclaus
Date: Sat Jan 16 09:06:24 2010
New Revision: 899909
URL: http://svn.apache.org/viewvc?rev=899909&view=rev
Log:
CAMEL-2357: Added filter to the NotifyBuilder.
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/builder/NotifyBuilder.java
camel/trunk/camel-core/src/test/java/org/apache/camel/builder/NotifyBuilderTest.java
Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/builder/NotifyBuilder.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/NotifyBuilder.java?rev=899909&r1=899908&r2=899909&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/builder/NotifyBuilder.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/builder/NotifyBuilder.java Sat Jan 16 09:06:24 2010
@@ -93,18 +93,36 @@
stack.push(new EventPredicateSupport() {
@Override
- public boolean onExchangeCreated(Exchange exchange) {
+ public boolean onExchange(Exchange exchange) {
+ // filter non matching exchanges
return EndpointHelper.matchEndpoint(exchange.getFromEndpoint().getEndpointUri(), endpointUri);
}
+ public boolean matches() {
+ return true;
+ }
+
@Override
- public boolean onExchangeCompleted(Exchange exchange) {
- return EndpointHelper.matchEndpoint(exchange.getFromEndpoint().getEndpointUri(), endpointUri);
+ public String toString() {
+ return "from(" + endpointUri + ")";
}
+ });
+ return this;
+ }
+
+ /**
+ * Optionally a filter to only allow matching {@link Exchange} to be used for matching.
+ *
+ * @param predicate the predicate to use for the filter
+ * @return the builder
+ */
+ public NotifyBuilder filter(final Predicate predicate) {
+ stack.push(new EventPredicateSupport() {
@Override
- public boolean onExchangeFailure(Exchange exchange) {
- return EndpointHelper.matchEndpoint(exchange.getFromEndpoint().getEndpointUri(), endpointUri);
+ public boolean onExchange(Exchange exchange) {
+ // filter non matching exchanges
+ return predicate.matches(exchange);
}
public boolean matches() {
@@ -113,7 +131,7 @@
@Override
public String toString() {
- return "from(" + endpointUri + ")";
+ return "filter(" + predicate + ")";
}
});
return this;
Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/builder/NotifyBuilderTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/builder/NotifyBuilderTest.java?rev=899909&r1=899908&r2=899909&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/builder/NotifyBuilderTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/builder/NotifyBuilderTest.java Sat Jan 16 09:06:24 2010
@@ -199,6 +199,69 @@
assertEquals(false, notify.matches());
}
+ public void testFilterWhenExchangeDone() throws Exception {
+ NotifyBuilder notify = new NotifyBuilder(context)
+ .filter(body().contains("World")).whenDone(3)
+ .create();
+
+ assertEquals("filter(body contains World).whenDone(3)", notify.toString());
+
+ assertEquals(false, notify.matches());
+
+ template.sendBody("direct:foo", "Hello World");
+ template.sendBody("direct:foo", "Hi World");
+ template.sendBody("direct:foo", "A");
+
+ assertEquals(false, notify.matches());
+
+ template.sendBody("direct:bar", "B");
+ template.sendBody("direct:bar", "C");
+
+ assertEquals(false, notify.matches());
+
+ template.sendBody("direct:bar", "Bye World");
+
+ assertEquals(true, notify.matches());
+
+ template.sendBody("direct:foo", "D");
+ template.sendBody("direct:bar", "Hey World");
+
+ assertEquals(true, notify.matches());
+ }
+
+ public void testFromFilterWhenExchangeDone() throws Exception {
+ NotifyBuilder notify = new NotifyBuilder(context)
+ .from("direct:foo").filter(body().contains("World")).whenDone(3)
+ .create();
+
+ assertEquals(false, notify.matches());
+
+ template.sendBody("direct:foo", "Hello World");
+ template.sendBody("direct:foo", "Hi World");
+ template.sendBody("direct:foo", "A");
+
+ assertEquals(false, notify.matches());
+
+ template.sendBody("direct:bar", "B");
+ template.sendBody("direct:foo", "C");
+
+ assertEquals(false, notify.matches());
+
+ template.sendBody("direct:bar", "Bye World");
+
+ assertEquals(false, notify.matches());
+
+ template.sendBody("direct:bar", "D");
+ template.sendBody("direct:foo", "Hey World");
+
+ assertEquals(true, notify.matches());
+
+ template.sendBody("direct:bar", "E");
+ template.sendBody("direct:foo", "Hi Again World");
+
+ assertEquals(true, notify.matches());
+ }
+
public void testWhenExchangeCompleted() throws Exception {
NotifyBuilder notify = new NotifyBuilder(context)
.whenCompleted(5)