Re: need help with interceptor in camel 1.6.2 using custom predicate
Hi I have created an example in 1.x as unit test, which is invoked 3 times. Apache SVN have issue currently with committing so I paste the test here davsclaus:(svn)camel-1x[camel-1.x:925986]/$ svn diff Index: camel-core/src/test/java/org/apache/camel/issues/CBRCustomPredicateAsFilterTest.java === --- camel-core/src/test/java/org/apache/camel/issues/CBRCustomPredicateAsFilterTest.java (revision 0) +++ camel-core/src/test/java/org/apache/camel/issues/CBRCustomPredicateAsFilterTest.java (revision 0) @@ -0,0 +1,82 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the License); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.issues; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.Exchange; +import org.apache.camel.Predicate; +import org.apache.camel.builder.RouteBuilder; + +/** + * @version $Revision$ + */ +public class CBRCustomPredicateAsFilterTest extends ContextTestSupport { + +private MyFiler filter = new MyFiler(); + +private class MyFiler implements Predicate { + +private ListString bodies = new ArrayListString(); + +public boolean matches(Object exchange) { +Exchange exc = (Exchange) exchange; +String body = exc.getIn().getBody(String.class); +bodies.add(body); + +return !secret.equals(body); +} + +public void assertMatches(String text, Object exchange) throws AssertionError { +// noop +} + +public ListString getBodies() { +return bodies; +} +} + +public void testCBRCustomPredicateAsFilter() throws Exception { +getMockEndpoint(mock:good).expectedBodiesReceived(Hello World, Bye World); +getMockEndpoint(mock:secret).expectedBodiesReceived(secret); + +template.sendBody(direct:start, Hello World); +template.sendBody(direct:start, secret); +template.sendBody(direct:start, Bye World); + +assertMockEndpointsSatisfied(); + +assertEquals(Hello World, filter.getBodies().get(0)); +assertEquals(secret, filter.getBodies().get(1)); +assertEquals(Bye World, filter.getBodies().get(2)); +} + +@Override +protected RouteBuilder createRouteBuilder() throws Exception { +return new RouteBuilder() { +@Override +public void configure() throws Exception { +from(direct:start) +.choice() +.when(filter).to(mock:good) +.otherwise().to(mock:secret); +} +}; +} +} -- Claus Ibsen Apache Camel Committer Author of Camel in Action: http://www.manning.com/ibsen/ Open Source Integration: http://fusesource.com Blog: http://davsclaus.blogspot.com/ Twitter: http://twitter.com/davsclaus
Re: need help with interceptor in camel 1.6.2 using custom predicate
Claus, Thanks for your help. Of course you deserve the kudos! I tried the filter method that you described with 1.6.2 and I also upgraded to 2.2 and tried again with interceptors and also with choice().when() What I am finding is this. When I use the custom predicate: public class commentFilter implements Predicate { public static final Log log = LogFactory.getLog(commentFilter.class); //camel 1.x //public boolean matches(Object ex) { public boolean matches(Exchange exchange) { log.debug(Starting filter); //came 1.x //Exchange exchange = (Exchange)ex; String email = exchange.getIn().getBody(Comment.class).getEmail(); if(email.equalsIgnoreCase(jessesanf...@gmail.com)) { log.debug(Skipping this exchange because it was + sent by blacklisted email address: + email); return false; } else { log.debug(Email not blacklisted. Allowing exchange to be routed + email); return true; } } //camel 1.x //public void assertMatches(String s, Object o) { //do nothing //this just satisfies the interface //} } It only executes the predicate one time. On the first message that is passed. I can tell this because I see the following in the logs: 18:19:03,894 DEBUG CommentCaptureController:captureWithJms:117 - captureWithJms 18:19:03,894 DEBUG CommentCaptureController:captureWithJms:117 - captureWithJms 18:19:03,895 DEBUG CommentCaptureController:capture:88 - Start 18:19:03,895 DEBUG CommentCaptureController:capture:88 - Start 18:19:03,896 DEBUG JmsCatcher:catchComment:84 - sending a message. 18:19:03,896 DEBUG JmsCatcher:catchComment:84 - sending a message. 18:19:03,923 INFO SingleConnectionFactory:initConnection:293 - Established shared JMS Connection: PooledConnection { org.apache.activemq.pool.connectionp...@4800ef96 } 18:19:03,934 INFO FailoverTransport:doReconnect:756 - Successfully connected to tcp://localhost:61716 18:19:04,024 INFO VelocityEngine:logVelocityMessage:49 - ResourceManager : found index.vm with loader org.apache.velocity.runtime.resource.loader.FileResourceLoader 18:19:04,077 DEBUG commentFilter:matches:25 - Starting filter 18:19:04,077 DEBUG commentFilter:matches:25 - Starting filter 18:19:04,079 DEBUG commentFilter:matches:31 - Skipping this exchange because it was sent by blacklisted email address: jessesanf...@gmail.com 18:19:04,079 DEBUG commentFilter:matches:31 - Skipping this exchange because it was sent by blacklisted email address: jessesanf...@gmail.com 18:19:28,423 DEBUG CommentCaptureController:captureWithJms:117 - captureWithJms 18:19:28,423 DEBUG CommentCaptureController:captureWithJms:117 - captureWithJms 18:19:28,424 DEBUG CommentCaptureController:capture:88 - Start 18:19:28,424 DEBUG CommentCaptureController:capture:88 - Start 18:19:28,424 DEBUG JmsCatcher:catchComment:84 - sending a message. 18:19:28,424 DEBUG JmsCatcher:catchComment:84 - sending a message. 18:19:28,597 DEBUG JdbcCatcher:capture:204 - done 18:19:28,597 DEBUG JdbcCatcher:capture:204 - done You can see that on the second submit (of the same exact data so the exchange should hold the same info) that the predicate is not run or at least it does not fire the section of the code that has my log message in it. Can you think of any reason why this might be? Ultimately I would like to have the predicate do a jdbc call to a blacklisted email table to check that the messages are allowed through before processing them. Am I pursuing this in the wrong fashion? How would you go about achieving this functionality? Thanks so much! Jesse On Mon, Apr 12, 2010 at 12:09 AM, Claus Ibsen claus.ib...@gmail.com wrote: Hi Jesse btw thanks for the kudo. The interceptors in Camel 1.x is not working super duper. And hence why they have been overhauled in the 2.0 onwards, which would allow you to do what you want. I suggest to search in the camel-core src/test/java directory for any intercept unit tests and see if you can find an example that looks like what you are doing. I assume upgrading to 2.x is not an option. You may instead want to use a Filter EIP in 1.x to build a solution where you can use the predicate to include the good messages in the filter. Then the bad messages can be skipped. On Sun, Apr 11, 2010 at 11:11 PM, Jesse Sanford jessesanf...@gmail.com wrote: I am trying to intercept an exchange using a custom predicate and I am having trouble re-routing the exchange or even simply stoping it. Here is my routebuilder: public class commentRoute extends RouteBuilder{ @Override public void configure(){ intercept().when(isBlacklisted()).to(mock:intercepted).stop(); from(comment-queue).to(comment-catcher); } private Predicate isBlacklisted(){ return new Predicate(){ public boolean matches(Object ex) { Exchange exchange
Re: need help with interceptor in camel 1.6.2 using custom predicate
FYI Here is my routeConfigurator class. You can see from the commented out sections in the configure method that I have tried a lot of different dsl configurations. public class commentRoute extends RouteBuilder { private Predicate commentFilter; @Override public void configure(){ //intercept().when(isBlacklisted()).to(log:comment).stop(); //intercept().to(log:comment).stop(); //from(comment-queue).to(comment-catcher); //from(comment-queue).filter(commentFilter).to(comment-catcher); errorHandler( deadLetterChannel(jms:queue:incoming.deadletterqueue). delayPattern(20s). maximumRedeliveries(5) ); from(comment-queue). choice().when(commentFilter).to(comment-catcher). otherwise().to(jms:queue:incoming.invalid); } /** * Your standard getter * @return the commentFilter */ public Predicate getCommentFilter() { return commentFilter; } /** * Your standard setter * @param commentFilter the commentFilter to set */ public void setCommentFilter(Predicate commentFilter) { this.commentFilter = commentFilter; } private Predicate isBlacklisted(){ return new Predicate(){ public boolean matches(Exchange exchange) { String email = exchange.getIn().getBody(Comment.class).getEmail(); if(email.equalsIgnoreCase(jessesanf...@gmail.com)) { return true; } else { return false; } } }; } } On Mon, Apr 12, 2010 at 6:28 PM, Jesse Sanford jessesanf...@gmail.comwrote: Claus, Thanks for your help. Of course you deserve the kudos! I tried the filter method that you described with 1.6.2 and I also upgraded to 2.2 and tried again with interceptors and also with choice().when() What I am finding is this. When I use the custom predicate: public class commentFilter implements Predicate { public static final Log log = LogFactory.getLog(commentFilter.class); //camel 1.x //public boolean matches(Object ex) { public boolean matches(Exchange exchange) { log.debug(Starting filter); //came 1.x //Exchange exchange = (Exchange)ex; String email = exchange.getIn().getBody(Comment.class).getEmail(); if(email.equalsIgnoreCase(jessesanf...@gmail.com)) { log.debug(Skipping this exchange because it was + sent by blacklisted email address: + email); return false; } else { log.debug(Email not blacklisted. Allowing exchange to be routed + email); return true; } } //camel 1.x //public void assertMatches(String s, Object o) { //do nothing //this just satisfies the interface //} } It only executes the predicate one time. On the first message that is passed. I can tell this because I see the following in the logs: 18:19:03,894 DEBUG CommentCaptureController:captureWithJms:117 - captureWithJms 18:19:03,894 DEBUG CommentCaptureController:captureWithJms:117 - captureWithJms 18:19:03,895 DEBUG CommentCaptureController:capture:88 - Start 18:19:03,895 DEBUG CommentCaptureController:capture:88 - Start 18:19:03,896 DEBUG JmsCatcher:catchComment:84 - sending a message. 18:19:03,896 DEBUG JmsCatcher:catchComment:84 - sending a message. 18:19:03,923 INFO SingleConnectionFactory:initConnection:293 - Established shared JMS Connection: PooledConnection { org.apache.activemq.pool.connectionp...@4800ef96 } 18:19:03,934 INFO FailoverTransport:doReconnect:756 - Successfully connected to tcp://localhost:61716 18:19:04,024 INFO VelocityEngine:logVelocityMessage:49 - ResourceManager : found index.vm with loader org.apache.velocity.runtime.resource.loader.FileResourceLoader 18:19:04,077 DEBUG commentFilter:matches:25 - Starting filter 18:19:04,077 DEBUG commentFilter:matches:25 - Starting filter 18:19:04,079 DEBUG commentFilter:matches:31 - Skipping this exchange because it was sent by blacklisted email address: jessesanf...@gmail.com 18:19:04,079 DEBUG commentFilter:matches:31 - Skipping this exchange because it was sent by blacklisted email address: jessesanf...@gmail.com 18:19:28,423 DEBUG CommentCaptureController:captureWithJms:117 - captureWithJms 18:19:28,423 DEBUG CommentCaptureController:captureWithJms:117 - captureWithJms 18:19:28,424 DEBUG CommentCaptureController:capture:88 - Start 18:19:28,424 DEBUG CommentCaptureController:capture:88 - Start 18:19:28,424 DEBUG JmsCatcher:catchComment:84 - sending a message. 18:19:28,424 DEBUG JmsCatcher:catchComment:84 - sending a message. 18:19:28,597 DEBUG JdbcCatcher:capture:204 - done 18:19:28,597 DEBUG JdbcCatcher:capture:204 - done You can see that on the second submit (of the same exact data so the
Re: need help with interceptor in camel 1.6.2 using custom predicate
I should mention that I am using spring 2.5.x On Mon, Apr 12, 2010 at 6:30 PM, Jesse Sanford jessesanf...@gmail.comwrote: FYI Here is my routeConfigurator class. You can see from the commented out sections in the configure method that I have tried a lot of different dsl configurations. public class commentRoute extends RouteBuilder { private Predicate commentFilter; @Override public void configure(){ //intercept().when(isBlacklisted()).to(log:comment).stop(); //intercept().to(log:comment).stop(); //from(comment-queue).to(comment-catcher); //from(comment-queue).filter(commentFilter).to(comment-catcher); errorHandler( deadLetterChannel(jms:queue:incoming.deadletterqueue). delayPattern(20s). maximumRedeliveries(5) ); from(comment-queue). choice().when(commentFilter).to(comment-catcher). otherwise().to(jms:queue:incoming.invalid); } /** * Your standard getter * @return the commentFilter */ public Predicate getCommentFilter() { return commentFilter; } /** * Your standard setter * @param commentFilter the commentFilter to set */ public void setCommentFilter(Predicate commentFilter) { this.commentFilter = commentFilter; } private Predicate isBlacklisted(){ return new Predicate(){ public boolean matches(Exchange exchange) { String email = exchange.getIn().getBody(Comment.class).getEmail(); if(email.equalsIgnoreCase(jessesanf...@gmail.com)) { return true; } else { return false; } } }; } } On Mon, Apr 12, 2010 at 6:28 PM, Jesse Sanford jessesanf...@gmail.comwrote: Claus, Thanks for your help. Of course you deserve the kudos! I tried the filter method that you described with 1.6.2 and I also upgraded to 2.2 and tried again with interceptors and also with choice().when() What I am finding is this. When I use the custom predicate: public class commentFilter implements Predicate { public static final Log log = LogFactory.getLog(commentFilter.class); //camel 1.x //public boolean matches(Object ex) { public boolean matches(Exchange exchange) { log.debug(Starting filter); //came 1.x //Exchange exchange = (Exchange)ex; String email = exchange.getIn().getBody(Comment.class).getEmail(); if(email.equalsIgnoreCase(jessesanf...@gmail.com)) { log.debug(Skipping this exchange because it was + sent by blacklisted email address: + email); return false; } else { log.debug(Email not blacklisted. Allowing exchange to be routed + email); return true; } } //camel 1.x //public void assertMatches(String s, Object o) { //do nothing //this just satisfies the interface //} } It only executes the predicate one time. On the first message that is passed. I can tell this because I see the following in the logs: 18:19:03,894 DEBUG CommentCaptureController:captureWithJms:117 - captureWithJms 18:19:03,894 DEBUG CommentCaptureController:captureWithJms:117 - captureWithJms 18:19:03,895 DEBUG CommentCaptureController:capture:88 - Start 18:19:03,895 DEBUG CommentCaptureController:capture:88 - Start 18:19:03,896 DEBUG JmsCatcher:catchComment:84 - sending a message. 18:19:03,896 DEBUG JmsCatcher:catchComment:84 - sending a message. 18:19:03,923 INFO SingleConnectionFactory:initConnection:293 - Established shared JMS Connection: PooledConnection { org.apache.activemq.pool.connectionp...@4800ef96 } 18:19:03,934 INFO FailoverTransport:doReconnect:756 - Successfully connected to tcp://localhost:61716 18:19:04,024 INFO VelocityEngine:logVelocityMessage:49 - ResourceManager : found index.vm with loader org.apache.velocity.runtime.resource.loader.FileResourceLoader 18:19:04,077 DEBUG commentFilter:matches:25 - Starting filter 18:19:04,077 DEBUG commentFilter:matches:25 - Starting filter 18:19:04,079 DEBUG commentFilter:matches:31 - Skipping this exchange because it was sent by blacklisted email address: jessesanf...@gmail.com 18:19:04,079 DEBUG commentFilter:matches:31 - Skipping this exchange because it was sent by blacklisted email address: jessesanf...@gmail.com 18:19:28,423 DEBUG CommentCaptureController:captureWithJms:117 - captureWithJms 18:19:28,423 DEBUG CommentCaptureController:captureWithJms:117 - captureWithJms 18:19:28,424 DEBUG CommentCaptureController:capture:88 - Start 18:19:28,424 DEBUG CommentCaptureController:capture:88 - Start 18:19:28,424 DEBUG JmsCatcher:catchComment:84 - sending a message. 18:19:28,424 DEBUG JmsCatcher:catchComment:84 - sending a message.
Re: need help with interceptor in camel 1.6.2 using custom predicate
Hi Jesse btw thanks for the kudo. The interceptors in Camel 1.x is not working super duper. And hence why they have been overhauled in the 2.0 onwards, which would allow you to do what you want. I suggest to search in the camel-core src/test/java directory for any intercept unit tests and see if you can find an example that looks like what you are doing. I assume upgrading to 2.x is not an option. You may instead want to use a Filter EIP in 1.x to build a solution where you can use the predicate to include the good messages in the filter. Then the bad messages can be skipped. On Sun, Apr 11, 2010 at 11:11 PM, Jesse Sanford jessesanf...@gmail.com wrote: I am trying to intercept an exchange using a custom predicate and I am having trouble re-routing the exchange or even simply stoping it. Here is my routebuilder: public class commentRoute extends RouteBuilder{ �...@override public void configure(){ intercept().when(isBlacklisted()).to(mock:intercepted).stop(); from(comment-queue).to(comment-catcher); } private Predicate isBlacklisted(){ return new Predicate(){ public boolean matches(Object ex) { Exchange exchange = (Exchange)ex; String email = exchange.getIn().getBody(Comment.class).getEmail(); if(email.equalsIgnoreCase(jessesanf...@gmail.com)) { return true; } else { return false; } } public void assertMatches(String s, Object o) { //do nothing //this just satisfies the interface } }; } } I know that the predicate is being run because If I set a breakpoint at if(email.equalsIgnoreCase(jessesanf...@gmail.com)) { and step through the code from there the return true; is reached when i send an exchange with the email address filled in with my email address. Am I doing this all wrong? If this predicate is run and returns true why doesn't camel respect my intercept re-routing or stoping? Thanks so much, jesse -- Claus Ibsen Apache Camel Committer Author of Camel in Action: http://www.manning.com/ibsen/ Open Source Integration: http://fusesource.com Blog: http://davsclaus.blogspot.com/ Twitter: http://twitter.com/davsclaus