2

According to http://camel.apache.org/cdi.html

@Inject
@Uri("direct:event")
ProducerTemplate producer;

void observeCdiEvents(@Observes String event) {
   producer.sendBody(event);
}

from("direct:event")
 .log("CDI event received: ${body}");

is equivalent to

@Inject
CdiEventEndpoint<String> cdiEventEndpoint;

from(cdiEventEndpoint).log("CDI event received: ${body}");

How do I convert the example with

 producer.asyncSendBody(...)

to use CdiEventEndpoint . Thanks in advance!

JoshOvi
  • 88
  • 7
  • Not really familiar with Camel, but are you talking about CDI 2.0 async events? – Siliarus Apr 04 '17 at 15:20
  • Not really, I know that I can write an EJB to fire events async. As far as I understand a thread in the EJB pool then waits for the event to be processed. Camel already offers an asynchronous routing engine and the method "asyncSendBody". I would prefer to use it directly (as in the working code above) but with the nicer CdiEventEndpoint . To be more precise, I have working code in my project that uses a ProducerTemplate to queue CDI-Events (pretty much the first Code snippet). I would like to refactor them to the second snippet, but on some keep the async nature of the third snippet. – JoshOvi Apr 04 '17 at 16:50

1 Answers1

1

I never actually tested this, but from the docs you should be able to replace "direct" with "seda" to go for asych:

@Inject
@Uri("seda:event")
ProducerTemplate producer;
...

After your clarification, I think you might be looking for the asynchronous routing engine in camel, which would be used by inserting threads() into the java dsl setup:

from("direct:event") // using a producer "direct:event" in an @Observes method
    .threads()
    .log("...")

or regarding the cdi event setup

from(cdiEventEndpoint) // using @Inject CdiEventEndpoint<String> cdiEventEndpoint
    .threads()
    .log("...")
mtj
  • 3,381
  • 19
  • 30
  • Hi @mtj, unfortunately that is not what I was asking, I would like to use CdiEventEndpoint and get rid of the ProducerTemplate. Then in the route definition the URI you would give it comes before the cdiEventEndpoint, and thus does not play a role. I have edited the question in incorporate your suggestion in a slightly modified form. This might work! – JoshOvi Apr 12 '17 at 10:48
  • @JoshOvi Please see edit. I hope I got your question right this time. :-) – mtj Apr 12 '17 at 11:10
  • Yeah @mtj! That's what I did. Approximately `from(cdiEventEndpoint).inOnly("seda:asyc").log("CDI event sent: ${body}");` – JoshOvi Apr 13 '17 at 08:54