You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Claus Ibsen <cl...@gmail.com> on 2012/01/04 17:39:41 UTC

Re: Typesafe Bean injection in routes using Registry

Hi

I would like to discuss this a bit. I think its an interesting thought.
The old school spring IoC stuff did not really offer a lookup by type-only.
But now the new kids on the block like CDI and OSGi offers that.

My first wish would be that we had a out of the box camel-cdi component :).

We may add a type safe bean ref lookup by the class name.
In fact the org.apache.camel.spi.Registry SPI have a method to lookup
by type only.

One caveat is that the DSL is rather big already for bean/method call
expressions.
So we should be careful to now cause the end user to get lost in all
the options (eg all the methods for bean/beanRef/method etc.)

However if we go down this route, we should make sure to more clearly
document on the javadoc, and in the wiki documentation
what the diff lookup methods does.

And btw we love contributions, so patches is welcome :)


On Wed, Dec 21, 2011 at 5:57 PM, Billy <bi...@gmail.com> wrote:
> Hi guys (and gal's ;-)!
>
> I've been experimenting lately with utilizing CDI as my Camel bean registry,
> making it effective in an Java EE 6 context. Seems I was not the only one as
> I've noticed both the JBoss Switchyard guys as well as this
> https://github.com/obergner/camelpe/wiki project  seems to have the same
> philosophy.
>
> After implementing the registry SPI and hooking it up to the CDI BeanManager
> I could sucessfully inject my beans into a route using i.e.
>
> @Named("myBean")
> class SomeBusinessBean etc...
>
> from("something").beanRef("myBean")
>
> This really comes in handy using @Producer annotations to create factories
> for my beans, i.e.
>
> @Produces @Named("myBean")
> public SomeBusinessBean createBusinessBean(){...}
>
> This is all well, but I was kind of confused when I found out that
> .bean(SomeBusinessBean.class) does not utilize the registry for lookups, but
> rather instantiates the objects by itself.
>
> From my understanding the beanRef method however looks in the registry but
> does not have a type-safe way of looking it up as it's only used to look up
> by String name references.
>
> I could theoretically inject my beans in the routebuilder, i:e
>
> @Inject SomeBusinessBean myBeanObject
>
> configure(){
>    from("somewhere").bean(myBeanObject)
> }
>
> ...but I guess that this creates a singleton bean as it is given at route
> definition?
>
> Are my assumptions correct that I should only use the beanRef(String.class)
> method, or am I missing something obvious?
>
> Would i.e. a beanRef(SomeBusinessBean.class) extension be possible (or even
> a good thing?), using type-safety and Registry lookup's?
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Typesafe-Bean-injection-in-routes-using-Registry-tp5092420p5092420.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
FuseSource
Email: cibsen@fusesource.com
Web: http://fusesource.com
Twitter: davsclaus, fusenews
Blog: http://davsclaus.blogspot.com/
Author of Camel in Action: http://www.manning.com/ibsen/

Re: Typesafe Bean injection in routes using Registry

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

Charles Moulliard is working on a camel-cdi component. So I guess the
questions to make the bean and beanRef more modern to the worlds of
OSGi and CDI could make sense.

Either we could do

1)
Ensure all lookups is done only from beanRef (to refer to something
that is being looked up).
And then leave .bean as to invoke an existing bean. eg we @deprecated
the .bean(class) and .bean(String) methods.

2)
Let .bean be able to lookup as well as if you are using .beanRef. So
this will adjust .bean(class) to lookup instead of currently to
instantiate a new instance from the class. This is a API breaking and
thus can only be done in next release.
And we need to indicate this in the release notes.

The .bean(class) was also mostly added because back then we were lazy
in unit testing, and just wanted Camel to invoke a bean, without the
need to setup the registry and whatnot. I do not know how many uses it
for real as well?

3)
Anyway for CDI and OSGi we should possible also add a way to specify
that filter discrimination.


On Mon, Jan 9, 2012 at 12:49 PM, Billy <bi...@gmail.com> wrote:
> Hi again Claus,
>
> Yes, I also think that this is a neat concept and fits well with the
> "de-Spring-ification" that Camel is currently undergoing.
>
> I also noted that the registry has the type lookup option, which led me to
> believe that it could be used from the routes as well. My implementation of
> the registry SPI is basically a rip of the project I mentioned in the last
> post:
>
> https://github.com/obergner/camelpe/blob/master/impl/src/main/java/net/camelpe/extension/camel/spi/CdiRegistry.java
> https://github.com/obergner/camelpe/blob/master/impl/src/main/java/net/camelpe/extension/camel/spi/CdiRegistry.java
>
> Seems like the author has come a long way of integrating Camel with CDI,
> using i.e. CDI to scan for type converters, routes contexts etc. My need was
> much more basic so I opted to only use the registry to reference my business
> beans for the routes. Perhaps his work could be merged into a camel-cdi
> component if the author would like to contribute?
>
> I can only agree that the bean options are confusing, especially with
> regards to when the registry is used and when Camel will take care of
> instantiating classes. This obviously plays a large role if the bean has
> dependencies and in multi concurrent scenarios (are my objects scoped as
> singletons/prototypes, etc..).
>
> Would love to continue the discussion, thanks for your response!
>
> /Billy
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Typesafe-Bean-injection-in-routes-using-Registry-tp5092420p5131160.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
FuseSource
Email: cibsen@fusesource.com
Web: http://fusesource.com
Twitter: davsclaus, fusenews
Blog: http://davsclaus.blogspot.com/
Author of Camel in Action: http://www.manning.com/ibsen/

Re: Typesafe Bean injection in routes using Registry

Posted by Billy <bi...@gmail.com>.
Hi again Claus,

Yes, I also think that this is a neat concept and fits well with the
"de-Spring-ification" that Camel is currently undergoing.

I also noted that the registry has the type lookup option, which led me to
believe that it could be used from the routes as well. My implementation of
the registry SPI is basically a rip of the project I mentioned in the last
post:

https://github.com/obergner/camelpe/blob/master/impl/src/main/java/net/camelpe/extension/camel/spi/CdiRegistry.java
https://github.com/obergner/camelpe/blob/master/impl/src/main/java/net/camelpe/extension/camel/spi/CdiRegistry.java 

Seems like the author has come a long way of integrating Camel with CDI,
using i.e. CDI to scan for type converters, routes contexts etc. My need was
much more basic so I opted to only use the registry to reference my business
beans for the routes. Perhaps his work could be merged into a camel-cdi
component if the author would like to contribute?

I can only agree that the bean options are confusing, especially with
regards to when the registry is used and when Camel will take care of
instantiating classes. This obviously plays a large role if the bean has
dependencies and in multi concurrent scenarios (are my objects scoped as
singletons/prototypes, etc..).

Would love to continue the discussion, thanks for your response!

/Billy


--
View this message in context: http://camel.465427.n5.nabble.com/Typesafe-Bean-injection-in-routes-using-Registry-tp5092420p5131160.html
Sent from the Camel - Users mailing list archive at Nabble.com.