OSDir


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Is it possible to use request scoped beans with Camel CDI ?


Hi Julien,

> On 12 Jun 2018, at 16:40, Julien Rivière <julien.riviere.lievin@xxxxxxxxx> wrote:
> 
> Hello,
> 
> First, thanks for the great framework.
> 
> Second, I am kind of new to Apache Camel and I have a question regarding
> bean integration.
> 
> Is it possible to use request scoped beans with Camel CDI ?

The Camel CDI integration currently does not provide support for automatic propagation of scopes like request scope.

This is something that we’d like to support though that may require a large effort to support all the possible cases where propagating thread-bound contexts is necessary (splitter, SEDA, ...).

I would suggest you create an issue in JIRA so that we can track your use case and start discussion the best approach to tackle the overall issue.

> I am using Camel CDI with Weld (Java DSL without Spring).
> 
> The pattern I am trying to implement is synchronous request response using
> parallel split-join to enrich my message before replying:
> 
> from("jetty:http://localhost:8280/metrics";)
> .to("http://localhost:10542/api/public/simulations?bridgeEndpoint=true";)
> .split().jsonpath("$").parallelProcessing()
> .setBody().jsonpath("$.id")
> // My call to third service would be there
> .bean(MyBean.class, "addItem")
> .end()
> .bean(MyBean.class, "items")
> .end();
> 
> Without CDI scope annotation on MyBean class:
> - a first instance is created and shared for every parallel calls
> (MyBean.addItem method binding)
> - a second instance is created at the end of the main route (MyBean.items
> method binding)
> 
> Of course I want to retrieve items added by every parallel calls.
> 
> With singleton scope qualifier, it is working but shared by every calls to
> the main route (see Jetty /metrics endpoint).
> 
> I need a way to share a "request" instance of my bean between the main
> route and the parallel (sub)routes but a new instance must be created for
> each call to the main route. @RequestScoped annotation
> throws ContextNotActiveException.
> 
> Is it possible ?
> 
> I tried using exchange properties but they are not shared between the main
> route and the parallel (sub)routes.

You may use the onPrepareRef option to propagate properties.

It might be possible to propagate the context "manually”.

An other approach to your problem may be to use the aggregate EIP with a batchAggregationStrategy that would be aware of the request context.

> Also, I had a look to camel examples but couldn't figure it out.
> 
> Thanks.
> 
> Best regards,
> 
> Julien

Hope it helps!

Antonin