Quantcast

Intercept processor/bean

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Intercept processor/bean

Łukasz Dywicki
Hey all,
Is there a way to intercept custom processor execution?

For example my route is following:
process(new LoggingProcessor())
or
processRef("loggingProcessor")

Also, is it's even possible to use interceptSendToEndpoint with

bean or beanRef DSL elements?

Kind regards,
Lukasz
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Intercept processor/bean

Babak Vahdat
Hi Lukasz,

Nice to hear from you :-)

> For example my route is following:
> process(new LoggingProcessor())
> or
> processRef("loggingProcessor")
As far as I know No, as interception for a given processor is not given at the DSL level. However what you could do is to make use of your own InterceptStrategy, something like:

    private static class LoggingProcessorInterceptor implements InterceptStrategy {

        @Override
        public Processor wrapProcessorInInterceptors(CamelContext context, ProcessorDefinition<?> definition, final Processor target, Processor nextTarget) throws Exception {
            if ("process[MyLoggingProcessor]".equals(definition.toString())) {
                return new Processor() {
                    public void process(Exchange exchange) throws Exception {
                        // do some interception logic here...
                        exchange.getContext().createProducerTemplate().requestBody("mock:intercepted", exchange.getIn().getBody());

                        // and then continue with the given target
                        target.process(exchange);
                    }
                };
            }

            return target;
        }
    }

Where your LoggingProcessor is given as something like:

    private static class LoggingProcessor implements Processor {

        @Override
        public void process(Exchange exchange) throws Exception {
            // your logging logic
        }

        @Override
        public String toString() {
            return "MyLoggingProcessor";
        }

    }

And then add this InterceptorStrategy to your Camel context:

   context.addInterceptStrategy(new LoggingProcessorInterceptor());

> Also, is it's even possible to use interceptSendToEndpoint with bean or beanRef DSL elements?
Yes as interceptSendToEndpoint can be used for any kind of Endpoint including the bean endpoint:

   interceptSendToEndpoint("bean:hello?method=camel").to("mock:intercepted");

Also note that you could make use of wildcard or regular expressions while specifying the endpoint uri:

http://camel.apache.org/intercept.html

Babak
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Intercept processor/bean

Claus Ibsen-2
In reply to this post by Łukasz Dywicki
On Fri, Aug 17, 2012 at 10:17 PM, Łukasz Dywicki <[hidden email]> wrote:

> Hey all,
> Is there a way to intercept custom processor execution?
>
> For example my route is following:
> process(new LoggingProcessor())
> or
> processRef("loggingProcessor")
>
> Also, is it's even possible to use interceptSendToEndpoint with
>
> bean or beanRef DSL elements?
>

Hi

I think Babak have covered this well. Just to add that if you use
bean/beanRef instead of process/processRef then you can use the
interceptSendToEndpoint etc. to intercept sending to the bean.

in the bean you can still implement org.apache.camel.Processor, and
have Camel leverage that. So its just a matter of changing in the
route DSL.

> Kind regards,
> Lukasz



--
Claus Ibsen
-----------------
FuseSource
Email: [hidden email]
Web: http://fusesource.com
Twitter: davsclaus, fusenews
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Intercept processor/bean

Christian Mueller
Yeah, I also favor to use

to("bean://myBean?method=foo")

which you can intercept...

Best,
Christian

On Mon, Aug 20, 2012 at 8:11 AM, Claus Ibsen <[hidden email]> wrote:

> On Fri, Aug 17, 2012 at 10:17 PM, Łukasz Dywicki <[hidden email]>
> wrote:
> > Hey all,
> > Is there a way to intercept custom processor execution?
> >
> > For example my route is following:
> > process(new LoggingProcessor())
> > or
> > processRef("loggingProcessor")
> >
> > Also, is it's even possible to use interceptSendToEndpoint with
> >
> > bean or beanRef DSL elements?
> >
>
> Hi
>
> I think Babak have covered this well. Just to add that if you use
> bean/beanRef instead of process/processRef then you can use the
> interceptSendToEndpoint etc. to intercept sending to the bean.
>
> in the bean you can still implement org.apache.camel.Processor, and
> have Camel leverage that. So its just a matter of changing in the
> route DSL.
>
> > Kind regards,
> > Lukasz
>
>
>
> --
> Claus Ibsen
> -----------------
> FuseSource
> Email: [hidden email]
> Web: http://fusesource.com
> Twitter: davsclaus, fusenews
> Blog: http://davsclaus.com
> Author of Camel in Action: http://www.manning.com/ibsen
>



--
Loading...