You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by yuncil <jo...@hotmail.de> on 2015/03/12 15:11:04 UTC

Bean invocation .class vs new

Hi Guys,

according to the camel documentation I have two different options to invoke
a method of a bean. 


/// Explicit selection of bean method to be invoked.
from("direct:start").bean(new ExampleBean(), "methodName");
 
// Camel will create the instance of bean and cache it for you.
from("direct:start").bean(ExampleBean.class);/

I guess the first solution creates a new bean instance every time. The
second one caches the instance of the object, but what does that mean
explicitly? How long is the object available? During one invocation or for
the whole lifecycle of my Webservice?

I'll give you an example, within my route I have the following:

.bean(MyBeanA.class, "doMyBeanA")
.bean(MyBeanB.class, "doMyBeanB")
.bean(MyBeanA.class, "doMyBeanA2")

I must invoke one method of one bean, then one method of another bean and
finally another method of the first bean. I want to avoid to create more
than one instance of MyBeanA. Every instance of every bean should be created
just once per route-invocation.

I hope It was understandable, maybe one of you can give me a hint. Thank you
in advance

regards







--
View this message in context: http://camel.465427.n5.nabble.com/Bean-invocation-class-vs-new-tp5764039.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Bean invocation .class vs new

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

There is
- no blocking
- no pool
- thread safe if you code your bean for that - that is a general
principle how to code for thread safety that is not Camel specific, so
you can find tutorials on the internet about that.



On Fri, Mar 20, 2015 at 11:34 AM, yuncil <jo...@hotmail.de> wrote:
> I have to ask further questions, because the caching process is still
> unclear.
>
> We use camel to realize a servlet request-response scenario. It is
> multithreaded, therefore we are a little worried about following situation:
>
> There are several incoming requests at the same time and all of them use the
> cached object.
>
> 1. Does camel block some request until the object is available again?
>
> 2. Does camel create a new object if it's necessery and does the whole
> caching process works like a pool?
>
> 3. Is the caching process thread safe? I mean can I be sure that my data
> integrety is safeguarded?
>
>
> Alternatively we have to create an object for each request. How do I realize
> this within a camel route?
>
> E.g
>
> from(direct:in)
>
>
>   //Aftwerward use it here
>   .bean(myBean, "myMethod")
>
> ....
> .end();
>
>
> Thanks
>
> Regards
>
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Bean-invocation-class-vs-new-tp5764039p5764474.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
Red Hat, Inc.
Email: cibsen@redhat.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen
hawtio: http://hawt.io/
fabric8: http://fabric8.io/

Re: Bean invocation .class vs new

Posted by yuncil <jo...@hotmail.de>.
No ideas? Anyone?



--
View this message in context: http://camel.465427.n5.nabble.com/Bean-invocation-class-vs-new-tp5764039p5765004.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Bean invocation .class vs new

Posted by yuncil <jo...@hotmail.de>.
I have to ask further questions, because the caching process is still
unclear.

We use camel to realize a servlet request-response scenario. It is
multithreaded, therefore we are a little worried about following situation:

There are several incoming requests at the same time and all of them use the
cached object.

1. Does camel block some request until the object is available again?

2. Does camel create a new object if it's necessery and does the whole
caching process works like a pool?

3. Is the caching process thread safe? I mean can I be sure that my data
integrety is safeguarded?


Alternatively we have to create an object for each request. How do I realize
this within a camel route?

E.g

from(direct:in)

  
  //Aftwerward use it here
  .bean(myBean, "myMethod")
  
....
.end();


Thanks

Regards




--
View this message in context: http://camel.465427.n5.nabble.com/Bean-invocation-class-vs-new-tp5764039p5764474.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Bean invocation .class vs new

Posted by yuncil <jo...@hotmail.de>.
Hey Claus,

thank you I got it now.



--
View this message in context: http://camel.465427.n5.nabble.com/Bean-invocation-class-vs-new-tp5764039p5764217.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Bean invocation .class vs new

Posted by Claus Ibsen <cl...@gmail.com>.
On Thu, Mar 12, 2015 at 3:40 PM, yuncil <jo...@hotmail.de> wrote:
> Hi Claus,
>
> thank you for your answer. What if I do the following:
>
> .bean(new MyBeanA(), "doMyBeanA")
> .bean(MyBeanB.class, "doMyBeanB")
> .bean(new MyBeanA(), "doMyBeanA2")
>

Yes new is a java constructor so it will create a new instance. If you
want to reuse the same instance, then create it once in standard Java
code, and use the instance in the route

MyBeanA beanA = new MyBeanA();

...

.bean(beanA, "xxx")
.bean(beanA, "yyy")
.bean(beanA, "zzz")


> Do I get two instances of MyBeanA? If not, how is the syntax if I want to
> reuse MyBeanA in the third line?
>
> You say the bean component caches and reuse the object, but when does the
> cache gets cleared? I guess only if my application will shut down, am I
> wrong?
>
> thanks
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Bean-invocation-class-vs-new-tp5764039p5764042.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
Red Hat, Inc.
Email: cibsen@redhat.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen
hawtio: http://hawt.io/
fabric8: http://fabric8.io/

Re: Bean invocation .class vs new

Posted by yuncil <jo...@hotmail.de>.
Hi Claus,

thank you for your answer. What if I do the following:

.bean(new MyBeanA(), "doMyBeanA")
.bean(MyBeanB.class, "doMyBeanB")
.bean(new MyBeanA(), "doMyBeanA2") 

Do I get two instances of MyBeanA? If not, how is the syntax if I want to
reuse MyBeanA in the third line?

You say the bean component caches and reuse the object, but when does the
cache gets cleared? I guess only if my application will shut down, am I
wrong?

thanks



--
View this message in context: http://camel.465427.n5.nabble.com/Bean-invocation-class-vs-new-tp5764039p5764042.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Bean invocation .class vs new

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

The bean component will create one time and reuse, (it caches by default)

And the 1st solution will only create it once, the new constructor is
only invoked once during defining the routes using the route builders.


On Thu, Mar 12, 2015 at 3:11 PM, yuncil <jo...@hotmail.de> wrote:
> Hi Guys,
>
> according to the camel documentation I have two different options to invoke
> a method of a bean.
>
>
> /// Explicit selection of bean method to be invoked.
> from("direct:start").bean(new ExampleBean(), "methodName");
>
> // Camel will create the instance of bean and cache it for you.
> from("direct:start").bean(ExampleBean.class);/
>
> I guess the first solution creates a new bean instance every time. The
> second one caches the instance of the object, but what does that mean
> explicitly? How long is the object available? During one invocation or for
> the whole lifecycle of my Webservice?
>
> I'll give you an example, within my route I have the following:
>
> .bean(MyBeanA.class, "doMyBeanA")
> .bean(MyBeanB.class, "doMyBeanB")
> .bean(MyBeanA.class, "doMyBeanA2")
>
> I must invoke one method of one bean, then one method of another bean and
> finally another method of the first bean. I want to avoid to create more
> than one instance of MyBeanA. Every instance of every bean should be created
> just once per route-invocation.
>
> I hope It was understandable, maybe one of you can give me a hint. Thank you
> in advance
>
> regards
>
>
>
>
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Bean-invocation-class-vs-new-tp5764039.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
Red Hat, Inc.
Email: cibsen@redhat.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen
hawtio: http://hawt.io/
fabric8: http://fabric8.io/