You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Ron Cecchini <ro...@comcast.net> on 2020/06/21 21:41:09 UTC

custom component vs. sending to a route

Hi guys. I have to integrate a 3rd party’s message listener code into my routes. Their API is pretty simple:

    listener(“foo”, fooHandler());

creates a listener using the underlying configured JMS and calls fooHandler() whenever it sees a “foo“ message.

Instead of creating a full-blown Camel component (something that I’ve never done yet) which would allow me to do something like:

    from(“mylistener:foo“).bean(fooProcessor)

I was thinking about doing something simpler and creating a class that calls listener() and where the handler injects the messages into a “direct:foo” route. Something like:

    class FooListener
    {
        @PostConstruct
        void initialize()
        {
            listener("foo", fooHandler();
        }
    
        void fooHandler(msg)
        {
            ProducerTemplate template = context.createProducerTemplate();
            template.asyncSendBody("direct:foo", msg);
        }
    }

and then have a route like:

    from("direct:foo").bean(fooProcessor);

So, is this a dumb approach?  I realize creating a custom component would be nicer and more easily reusable, but are there any other major pros and cons?

Thanks.

Re: custom component vs. sending to a route

Posted by Claus Ibsen <cl...@gmail.com>.
On Fri, Jun 26, 2020 at 12:40 AM Ron Cecchini <ro...@comcast.net> wrote:
>
> Thanks as always, Claus.
>
> Correct, I will want to unregister / close the listener's connection upon shutdown.
>
> Correct, I don't want to create the ProducerTemplate over and over.  I'll do it once at initialization.
>
> I'll look into what it takes to create a custom component, but in the meantime, what is the best way to detect a graceful shutdown?
>
> I'm running in Spring Boot.  I found an example where you could have your class implement "SmartLifecycle".
>
> Is that the best way or is there some annotation I can use to detect a shutdown (sort of like the @PostConstruct upon startup) ?
>

SmartLifecycle is from spring, and if so then you are tied to spring.
Also then you are using two different means - @PostConstruct for init
and smart lifecycle for stop. Maybe try to find a single thing that
are the same and can do both.

In Camel we have a Service API for lifecycle that has start/stop logic
https://camel.apache.org/manual/latest/lifecycle.html

That are built into the default component / default endpoint etc,
where you can override the doStart / doStop methods.

But this entails that you build a custom Camel component.

For a bean then maybe look at those spring way of start / stop callbacks.
Although if you use the bean in a Camel route - then the bean can also
extend ServiceSupport or implement Service and have Camel call its
start/stop methods.



> Thanks again.
>
> > On 06/22/2020 12:57 AM Claus Ibsen <cl...@gmail.com> wrote:
> >
> >
> > Hi
> >
> > I would consider writing a custom component. As dont you also need to
> > unregister your listener when your app shuts down?
> > All of the lifecycle and management of this can be built into the
> > component and then as end user it becomes easier to use as its just
> > like another Camel endpoint.
> >
> > And btw in your sample code, then you should only create the producer
> > template once - not per message.
> >
> > Also as alternative you can use Camels POJO routing to hide some of Camel
> > https://camel.apache.org/manual/latest/pojo-producing.html
> >
> >
> > On Sun, Jun 21, 2020 at 11:41 PM Ron Cecchini <ro...@comcast.net> wrote:
> > >
> > > Hi guys. I have to integrate a 3rd party’s message listener code into my routes. Their API is pretty simple:
> > >
> > >     listener(“foo”, fooHandler());
> > >
> > > creates a listener using the underlying configured JMS and calls fooHandler() whenever it sees a “foo“ message.
> > >
> > > Instead of creating a full-blown Camel component (something that I’ve never done yet) which would allow me to do something like:
> > >
> > >     from(“mylistener:foo“).bean(fooProcessor)
> > >
> > > I was thinking about doing something simpler and creating a class that calls listener() and where the handler injects the messages into a “direct:foo” route. Something like:
> > >
> > >     class FooListener
> > >     {
> > >         @PostConstruct
> > >         void initialize()
> > >         {
> > >             listener("foo", fooHandler();
> > >         }
> > >
> > >         void fooHandler(msg)
> > >         {
> > >             ProducerTemplate template = context.createProducerTemplate();
> > >             template.asyncSendBody("direct:foo", msg);
> > >         }
> > >     }
> > >
> > > and then have a route like:
> > >
> > >     from("direct:foo").bean(fooProcessor);
> > >
> > > So, is this a dumb approach?  I realize creating a custom component would be nicer and more easily reusable, but are there any other major pros and cons?
> > >
> > > Thanks.
> >
> >
> >
> > --
> > Claus Ibsen
> > -----------------
> > http://davsclaus.com @davsclaus
> > Camel in Action 2: https://www.manning.com/ibsen2



-- 
Claus Ibsen
-----------------
http://davsclaus.com @davsclaus
Camel in Action 2: https://www.manning.com/ibsen2

Re: custom component vs. sending to a route

Posted by Ron Cecchini <ro...@comcast.net>.
Thanks as always, Claus.

Correct, I will want to unregister / close the listener's connection upon shutdown.

Correct, I don't want to create the ProducerTemplate over and over.  I'll do it once at initialization.

I'll look into what it takes to create a custom component, but in the meantime, what is the best way to detect a graceful shutdown?

I'm running in Spring Boot.  I found an example where you could have your class implement "SmartLifecycle".

Is that the best way or is there some annotation I can use to detect a shutdown (sort of like the @PostConstruct upon startup) ?

Thanks again.

> On 06/22/2020 12:57 AM Claus Ibsen <cl...@gmail.com> wrote:
> 
>  
> Hi
> 
> I would consider writing a custom component. As dont you also need to
> unregister your listener when your app shuts down?
> All of the lifecycle and management of this can be built into the
> component and then as end user it becomes easier to use as its just
> like another Camel endpoint.
> 
> And btw in your sample code, then you should only create the producer
> template once - not per message.
> 
> Also as alternative you can use Camels POJO routing to hide some of Camel
> https://camel.apache.org/manual/latest/pojo-producing.html
> 
> 
> On Sun, Jun 21, 2020 at 11:41 PM Ron Cecchini <ro...@comcast.net> wrote:
> >
> > Hi guys. I have to integrate a 3rd party’s message listener code into my routes. Their API is pretty simple:
> >
> >     listener(“foo”, fooHandler());
> >
> > creates a listener using the underlying configured JMS and calls fooHandler() whenever it sees a “foo“ message.
> >
> > Instead of creating a full-blown Camel component (something that I’ve never done yet) which would allow me to do something like:
> >
> >     from(“mylistener:foo“).bean(fooProcessor)
> >
> > I was thinking about doing something simpler and creating a class that calls listener() and where the handler injects the messages into a “direct:foo” route. Something like:
> >
> >     class FooListener
> >     {
> >         @PostConstruct
> >         void initialize()
> >         {
> >             listener("foo", fooHandler();
> >         }
> >
> >         void fooHandler(msg)
> >         {
> >             ProducerTemplate template = context.createProducerTemplate();
> >             template.asyncSendBody("direct:foo", msg);
> >         }
> >     }
> >
> > and then have a route like:
> >
> >     from("direct:foo").bean(fooProcessor);
> >
> > So, is this a dumb approach?  I realize creating a custom component would be nicer and more easily reusable, but are there any other major pros and cons?
> >
> > Thanks.
> 
> 
> 
> -- 
> Claus Ibsen
> -----------------
> http://davsclaus.com @davsclaus
> Camel in Action 2: https://www.manning.com/ibsen2

Re: custom component vs. sending to a route

Posted by Claus Ibsen <cl...@gmail.com>.
Hi

I would consider writing a custom component. As dont you also need to
unregister your listener when your app shuts down?
All of the lifecycle and management of this can be built into the
component and then as end user it becomes easier to use as its just
like another Camel endpoint.

And btw in your sample code, then you should only create the producer
template once - not per message.

Also as alternative you can use Camels POJO routing to hide some of Camel
https://camel.apache.org/manual/latest/pojo-producing.html


On Sun, Jun 21, 2020 at 11:41 PM Ron Cecchini <ro...@comcast.net> wrote:
>
> Hi guys. I have to integrate a 3rd party’s message listener code into my routes. Their API is pretty simple:
>
>     listener(“foo”, fooHandler());
>
> creates a listener using the underlying configured JMS and calls fooHandler() whenever it sees a “foo“ message.
>
> Instead of creating a full-blown Camel component (something that I’ve never done yet) which would allow me to do something like:
>
>     from(“mylistener:foo“).bean(fooProcessor)
>
> I was thinking about doing something simpler and creating a class that calls listener() and where the handler injects the messages into a “direct:foo” route. Something like:
>
>     class FooListener
>     {
>         @PostConstruct
>         void initialize()
>         {
>             listener("foo", fooHandler();
>         }
>
>         void fooHandler(msg)
>         {
>             ProducerTemplate template = context.createProducerTemplate();
>             template.asyncSendBody("direct:foo", msg);
>         }
>     }
>
> and then have a route like:
>
>     from("direct:foo").bean(fooProcessor);
>
> So, is this a dumb approach?  I realize creating a custom component would be nicer and more easily reusable, but are there any other major pros and cons?
>
> Thanks.



-- 
Claus Ibsen
-----------------
http://davsclaus.com @davsclaus
Camel in Action 2: https://www.manning.com/ibsen2