camel-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jesse Sanford <jessesanf...@gmail.com>
Subject Re: need help with interceptor in camel 1.6.2 using custom predicate
Date Mon, 12 Apr 2010 22:30:21 GMT
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("jessesanford@gmail.com")) {
                    return true;
                } else {
                    return false;
                }
            }
        };
    }

}


On Mon, Apr 12, 2010 at 6:28 PM, Jesse Sanford <jessesanford@gmail.com>wrote:

> 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("jessesanford@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.ConnectionPool@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: jessesanford@gmail.com
> 18:19:04,079 DEBUG commentFilter:matches:31 - Skipping this exchange
> because it was sent by blacklisted email address: jessesanford@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.ibsen@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 <jessesanford@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("jessesanford@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("jessesanford@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
>>
>
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message