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 <[email protected]> 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

Reply via email to