You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Rajith Muditha Attapattu <ra...@gmail.com> on 2016/08/10 17:48:53 UTC

Loading all beans before the Camel Context starts

I'm using CDI and want to make sure all my beans are loaded before the
context and properly initialized.

I have them as attributes in my CamelContext class with @Inject.

However I override the start method in the context to make sure I call the
various initi methods for those beans to make sure they are loaded and
ready when the routes start.

@Override
public void start() throws Exception{
   myBean1.init();
   myBean2.init().
   super.start();
}

Is this advisable ? is there a better way to do this ?

Regards,

Rajith Muditha Attapattu <http://rajith.2rlabs.com/>

Re: Loading all beans before the Camel Context starts

Posted by Rajith Muditha Attapattu <ra...@gmail.com>.
I've always used blueprint too and now feeling my way around CDI.

I'm loading a drools engine programatically using a kjar.
I want that in place before my routes start.

On Wed, Aug 10, 2016 at 3:13 PM, Brad Johnson <br...@mediadriver.com>
wrote:

> If you don't need to initialized them you could override the start like
> that but then have a verify method.  That would make sure you aren't
> inadvertently changing any behavior.
>
> @Override
> public void start() throws Exception{
>
>    super.start();
>    verifyBeans();
> }
>
> private void verifyBeans()
> {
>     verify(myBean1);
>     verify(myBean2).
> }
>
> private void verify(Object o)
> {
>     if(o==null) throw new RuntimeException("Bean not found at start up.");
>
> }
>
> I'm curious as to how or where you're overriding the CamelContext.  Curious
> because I've always used Blueprint with Fuse and not CDI and only now
> looking at CDI with the latest version of Fuse which is due soon.
>
>
> Brad
>
> On Wed, Aug 10, 2016 at 2:00 PM, Rajith Muditha Attapattu <
> rajith77@gmail.com> wrote:
>
> > I'm using Camel 2.15 bcos thats the version coming Fuse 6.2.1
> > But my understanding is that CDI events are not supported until 2.17
> >
> > What's the drawback of overriding start() ?
> >
> > On Wed, Aug 10, 2016 at 2:33 PM, Antonin Stefanutti <
> antonin@stefanutti.fr
> > >
> > wrote:
> >
> > > One alternative is to observe for the CamelContextStartingEvent that’s
> > > fired before the corresponding Camel contexts get started, e.g.:
> > >
> > > void initMyBeansBeforeContextStart(@Observes CamelContextStartingEvent
> > > event, MyBean bean) {
> > >     bean.init();
> > > }
> > >
> > > Either method argument or class field injection will work. And that
> > avoids
> > > having to override the start method on the context.
> > >
> > > You can find more information in: http://camel.apache.org/cdi.
> html#CDI-
> > > CameleventstoCDIevents
> > >
> > > Antonin
> > >
> > > > On 10 Aug 2016, at 19:48, Rajith Muditha Attapattu <
> rajith77@gmail.com
> > >
> > > wrote:
> > > >
> > > > I'm using CDI and want to make sure all my beans are loaded before
> the
> > > > context and properly initialized.
> > > >
> > > > I have them as attributes in my CamelContext class with @Inject.
> > > >
> > > > However I override the start method in the context to make sure I
> call
> > > the
> > > > various initi methods for those beans to make sure they are loaded
> and
> > > > ready when the routes start.
> > > >
> > > > @Override
> > > > public void start() throws Exception{
> > > >   myBean1.init();
> > > >   myBean2.init().
> > > >   super.start();
> > > > }
> > > >
> > > > Is this advisable ? is there a better way to do this ?
> > > >
> > > > Regards,
> > > >
> > > > Rajith Muditha Attapattu <http://rajith.2rlabs.com/>
> > >
> > >
> >
> >
> > --
> > Regards,
> >
> > Rajith Muditha Attapattu <http://rajith.2rlabs.com/>
> >
>



-- 
Regards,

Rajith Muditha Attapattu <http://rajith.2rlabs.com/>

Re: Loading all beans before the Camel Context starts

Posted by Brad Johnson <br...@mediadriver.com>.
If you don't need to initialized them you could override the start like
that but then have a verify method.  That would make sure you aren't
inadvertently changing any behavior.

@Override
public void start() throws Exception{

   super.start();
   verifyBeans();
}

private void verifyBeans()
{
    verify(myBean1);
    verify(myBean2).
}

private void verify(Object o)
{
    if(o==null) throw new RuntimeException("Bean not found at start up.");

}

I'm curious as to how or where you're overriding the CamelContext.  Curious
because I've always used Blueprint with Fuse and not CDI and only now
looking at CDI with the latest version of Fuse which is due soon.


Brad

On Wed, Aug 10, 2016 at 2:00 PM, Rajith Muditha Attapattu <
rajith77@gmail.com> wrote:

> I'm using Camel 2.15 bcos thats the version coming Fuse 6.2.1
> But my understanding is that CDI events are not supported until 2.17
>
> What's the drawback of overriding start() ?
>
> On Wed, Aug 10, 2016 at 2:33 PM, Antonin Stefanutti <antonin@stefanutti.fr
> >
> wrote:
>
> > One alternative is to observe for the CamelContextStartingEvent that’s
> > fired before the corresponding Camel contexts get started, e.g.:
> >
> > void initMyBeansBeforeContextStart(@Observes CamelContextStartingEvent
> > event, MyBean bean) {
> >     bean.init();
> > }
> >
> > Either method argument or class field injection will work. And that
> avoids
> > having to override the start method on the context.
> >
> > You can find more information in: http://camel.apache.org/cdi.html#CDI-
> > CameleventstoCDIevents
> >
> > Antonin
> >
> > > On 10 Aug 2016, at 19:48, Rajith Muditha Attapattu <rajith77@gmail.com
> >
> > wrote:
> > >
> > > I'm using CDI and want to make sure all my beans are loaded before the
> > > context and properly initialized.
> > >
> > > I have them as attributes in my CamelContext class with @Inject.
> > >
> > > However I override the start method in the context to make sure I call
> > the
> > > various initi methods for those beans to make sure they are loaded and
> > > ready when the routes start.
> > >
> > > @Override
> > > public void start() throws Exception{
> > >   myBean1.init();
> > >   myBean2.init().
> > >   super.start();
> > > }
> > >
> > > Is this advisable ? is there a better way to do this ?
> > >
> > > Regards,
> > >
> > > Rajith Muditha Attapattu <http://rajith.2rlabs.com/>
> >
> >
>
>
> --
> Regards,
>
> Rajith Muditha Attapattu <http://rajith.2rlabs.com/>
>

Re: Loading all beans before the Camel Context starts

Posted by Antonin Stefanutti <an...@stefanutti.fr>.
Hi Rajith,

> On 10 Aug 2016, at 21:00, Rajith Muditha Attapattu <ra...@gmail.com> wrote:
> 
> I'm using Camel 2.15 bcos thats the version coming Fuse 6.2.1
> But my understanding is that CDI events are not supported until 2.17

Your understanding is correct. This will be available in Fuse 6.3 which is expected to be released soon.

> What's the drawback of overriding start() ?

Assuming you can use the observer method, I find it more straightforward and explicit that customising and overriding the default Camel context bean. Though that’s a matter of style.

Antonin

> On Wed, Aug 10, 2016 at 2:33 PM, Antonin Stefanutti <an...@stefanutti.fr>
> wrote:
> 
>> One alternative is to observe for the CamelContextStartingEvent that’s
>> fired before the corresponding Camel contexts get started, e.g.:
>> 
>> void initMyBeansBeforeContextStart(@Observes CamelContextStartingEvent
>> event, MyBean bean) {
>>    bean.init();
>> }
>> 
>> Either method argument or class field injection will work. And that avoids
>> having to override the start method on the context.
>> 
>> You can find more information in: http://camel.apache.org/cdi.html#CDI-
>> CameleventstoCDIevents
>> 
>> Antonin
>> 
>>> On 10 Aug 2016, at 19:48, Rajith Muditha Attapattu <ra...@gmail.com>
>> wrote:
>>> 
>>> I'm using CDI and want to make sure all my beans are loaded before the
>>> context and properly initialized.
>>> 
>>> I have them as attributes in my CamelContext class with @Inject.
>>> 
>>> However I override the start method in the context to make sure I call
>> the
>>> various initi methods for those beans to make sure they are loaded and
>>> ready when the routes start.
>>> 
>>> @Override
>>> public void start() throws Exception{
>>>  myBean1.init();
>>>  myBean2.init().
>>>  super.start();
>>> }
>>> 
>>> Is this advisable ? is there a better way to do this ?
>>> 
>>> Regards,
>>> 
>>> Rajith Muditha Attapattu <http://rajith.2rlabs.com/>
>> 
>> 
> 
> 
> -- 
> Regards,
> 
> Rajith Muditha Attapattu <http://rajith.2rlabs.com/>


Re: Loading all beans before the Camel Context starts

Posted by Rajith Muditha Attapattu <ra...@gmail.com>.
I'm using Camel 2.15 bcos thats the version coming Fuse 6.2.1
But my understanding is that CDI events are not supported until 2.17

What's the drawback of overriding start() ?

On Wed, Aug 10, 2016 at 2:33 PM, Antonin Stefanutti <an...@stefanutti.fr>
wrote:

> One alternative is to observe for the CamelContextStartingEvent that’s
> fired before the corresponding Camel contexts get started, e.g.:
>
> void initMyBeansBeforeContextStart(@Observes CamelContextStartingEvent
> event, MyBean bean) {
>     bean.init();
> }
>
> Either method argument or class field injection will work. And that avoids
> having to override the start method on the context.
>
> You can find more information in: http://camel.apache.org/cdi.html#CDI-
> CameleventstoCDIevents
>
> Antonin
>
> > On 10 Aug 2016, at 19:48, Rajith Muditha Attapattu <ra...@gmail.com>
> wrote:
> >
> > I'm using CDI and want to make sure all my beans are loaded before the
> > context and properly initialized.
> >
> > I have them as attributes in my CamelContext class with @Inject.
> >
> > However I override the start method in the context to make sure I call
> the
> > various initi methods for those beans to make sure they are loaded and
> > ready when the routes start.
> >
> > @Override
> > public void start() throws Exception{
> >   myBean1.init();
> >   myBean2.init().
> >   super.start();
> > }
> >
> > Is this advisable ? is there a better way to do this ?
> >
> > Regards,
> >
> > Rajith Muditha Attapattu <http://rajith.2rlabs.com/>
>
>


-- 
Regards,

Rajith Muditha Attapattu <http://rajith.2rlabs.com/>

Re: Loading all beans before the Camel Context starts

Posted by Antonin Stefanutti <an...@stefanutti.fr>.
One alternative is to observe for the CamelContextStartingEvent that’s fired before the corresponding Camel contexts get started, e.g.:

void initMyBeansBeforeContextStart(@Observes CamelContextStartingEvent event, MyBean bean) {
    bean.init();
}

Either method argument or class field injection will work. And that avoids having to override the start method on the context.

You can find more information in: http://camel.apache.org/cdi.html#CDI-CameleventstoCDIevents

Antonin

> On 10 Aug 2016, at 19:48, Rajith Muditha Attapattu <ra...@gmail.com> wrote:
> 
> I'm using CDI and want to make sure all my beans are loaded before the
> context and properly initialized.
> 
> I have them as attributes in my CamelContext class with @Inject.
> 
> However I override the start method in the context to make sure I call the
> various initi methods for those beans to make sure they are loaded and
> ready when the routes start.
> 
> @Override
> public void start() throws Exception{
>   myBean1.init();
>   myBean2.init().
>   super.start();
> }
> 
> Is this advisable ? is there a better way to do this ?
> 
> Regards,
> 
> Rajith Muditha Attapattu <http://rajith.2rlabs.com/>