You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Bernard Ligny <be...@gmail.com> on 2012/11/08 22:29:22 UTC

Access camel context from BundleActivator.stop() ?

I am trying without success to look for CamelContext inside the stop method
of a BundleActivator.


I did follow the instructions of Jean-Baptiste Onofré's Blog ("/When Camel
is used in an OSGi environment, it automatically  exposes CamelContexts as
OSGi services/"), eg:

public class MyBundleActivator implements BundleActivator {
	
	public void stop(BundleContext bundleContext) throws Exception {
		ServiceReference[] references =
bundleContext.getServiceReferences(CamelContext.class.getName(), null);
		CamelContext cc = null;
		if ( references != null && references.length>0 ) {
		      cc = (CamelContext) bundleContext.getService(references[0]);
		}
	}
	
}

but it does not work :-(
(I am using Camel 2.8.5 , ServiceMix 4.4.2)

What am i doing wrong ?
Is it possible that at the time the shutdown hook is called, the camel
context has been already deregistered ?
If so, what would be an alternate(*) solution to do some work at bundle stop
time, using spring beans ?
(*) rather than getting camel context, and looking for such bean throuhg
camel registry...
 
Thx,

Bernard.

Bernard.






--
View this message in context: http://camel.465427.n5.nabble.com/Access-camel-context-from-BundleActivator-stop-tp5722376.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Access camel context from BundleActivator.stop() ?

Posted by Bernard Ligny <be...@gmail.com>.
Great idea...that i previously tried ;-).
Unfortunately, I need also stuff from other bundles...which may have been
shut down in the meantime.
Therefore, I need to intervene a bit earlier in the shutdown process.

Up to now, the most satisfying found solution is this one:

public class StatsPersister implements Processor {
	
	// Injected by Spring
	public void setBundleContext(BundleContext bundleContext) {
		this.bundleContext = bundleContext;
		bundleContext.addBundleListener( new MyBundleListener() );
	}
	
	public void persistStats() {
		...
	}

	/**
	 * Shutdown hook
	 * We catch the STOPPING event of the felix framework OSGI bundle
	 * (which is hopefully the very first bundle that is shut down :-D)
	 */
	private class MyBundleListener implements SynchronousBundleListener {
		
		public void bundleChanged(BundleEvent event) {
			if ( event.getType()==BundleEvent.STOPPING &&
				 "org.apache.felix.framework".equals(
event.getBundle().getSymbolicName() ) ) {
					persistStats();
				}
			}			
		}
	}


Bernard.




--
View this message in context: http://camel.465427.n5.nabble.com/Access-camel-context-from-BundleActivator-stop-tp5722376p5722395.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Access camel context from BundleActivator.stop() ?

Posted by Claus Ibsen <cl...@gmail.com>.
On Fri, Nov 9, 2012 at 9:42 AM, Bernard Ligny <be...@gmail.com> wrote:
>
>
> Willem.Jiang wrote
>> Did you create the camel context in the BundleActivator or using
>> Blueprint, Spring to create the camel context?
>
> It is created using Spring,
> (but my osgi bundle activator is registered through the manifest, not by
> Spring)
>
>
> Willem.Jiang wrote
>> Why do you need to access the camel context when the bundle is stop?
>
> Because upon shutdown, I need to persist some statistics into a database.
> And this persistency job is provided by Spring beans (dao and service - in
> the Spring meaning, not osgi - implementations).
> eg:
>
> ServiceReference[] references =
> bundleContext.getServiceReferences(CamelContext.class.getName(), null);
> CamelContext cc = null;
> if ( references != null && references.length>0 ) {
>   cc = (CamelContext) bundleContext.getService(references[0]);
>   MySpringBean bean = Pcc.getRegistry().lookup("myBean",
> MySpringBean.class);
>   bean.persistStats();
> }
>
> So the question is: where can we place some "finalization" work when a camel
> context context (created by Spring) is shut down ? Note that as that work is
> delegating most of a job to spring beans, all these beans must not be
> deregistered yet. In that sense, it is more a "*before*-shutdown" hook that
> we really need in my case...
>

You can maybe just define a Spring <bean> and have a stop method being
invoked from it. There is a attribute you can set in the <bean> to
refer to a start|stop method.

And then have maybe a "depends-on" -> Camel is needed. To help Spring
with the ordering.

<bean id="myStatBean" depends-on="myCamel">

<camelContext id="myCamel" ...>


>
>
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Access-camel-context-from-BundleActivator-stop-tp5722376p5722381.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
Red Hat, Inc.
FuseSource is now part of Red Hat
Email: cibsen@redhat.com
Web: http://fusesource.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen

Re: Access camel context from BundleActivator.stop() ?

Posted by Bernard Ligny <be...@gmail.com>.

Willem.Jiang wrote
> Did you create the camel context in the BundleActivator or using
> Blueprint, Spring to create the camel context?

It is created using Spring,
(but my osgi bundle activator is registered through the manifest, not by
Spring)


Willem.Jiang wrote
> Why do you need to access the camel context when the bundle is stop?

Because upon shutdown, I need to persist some statistics into a database.
And this persistency job is provided by Spring beans (dao and service - in
the Spring meaning, not osgi - implementations).
eg:

ServiceReference[] references = 
bundleContext.getServiceReferences(CamelContext.class.getName(), null); 
CamelContext cc = null; 
if ( references != null && references.length>0 ) { 
  cc = (CamelContext) bundleContext.getService(references[0]);
  MySpringBean bean = Pcc.getRegistry().lookup("myBean",
MySpringBean.class); 
  bean.persistStats();
}

So the question is: where can we place some "finalization" work when a camel
context context (created by Spring) is shut down ? Note that as that work is
delegating most of a job to spring beans, all these beans must not be
deregistered yet. In that sense, it is more a "*before*-shutdown" hook that
we really need in my case...






--
View this message in context: http://camel.465427.n5.nabble.com/Access-camel-context-from-BundleActivator-stop-tp5722376p5722381.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Access camel context from BundleActivator.stop() ?

Posted by Willem jiang <wi...@gmail.com>.
Hi,

Did you create the camel context in the BundleActivator or using Blueprint, Spring to create the camel context?

Why do you need to access the camel context when the bundle is stop?
 I think  the camel context should be deregistered already.

--  
Willem Jiang

Red Hat, Inc.
FuseSource is now part of Red Hat
Web: http://www.fusesource.com | http://www.redhat.com
Blog: http://willemjiang.blogspot.com (http://willemjiang.blogspot.com/) (English)
          http://jnn.javaeye.com (http://jnn.javaeye.com/) (Chinese)
Twitter: willemjiang  
Weibo: willemjiang





On Friday, November 9, 2012 at 5:29 AM, Bernard Ligny wrote:

> I am trying without success to look for CamelContext inside the stop method
> of a BundleActivator.
>  
>  
> I did follow the instructions of Jean-Baptiste Onofré's Blog ("/When Camel
> is used in an OSGi environment, it automatically exposes CamelContexts as
> OSGi services/"), eg:
>  
> public class MyBundleActivator implements BundleActivator {
>  
> public void stop(BundleContext bundleContext) throws Exception {
> ServiceReference[] references =
> bundleContext.getServiceReferences(CamelContext.class.getName(), null);
> CamelContext cc = null;
> if ( references != null && references.length>0 ) {
> cc = (CamelContext) bundleContext.getService(references[0]);
> }
> }
>  
> }
>  
> but it does not work :-(
> (I am using Camel 2.8.5 , ServiceMix 4.4.2)
>  
> What am i doing wrong ?
> Is it possible that at the time the shutdown hook is called, the camel
> context has been already deregistered ?
> If so, what would be an alternate(*) solution to do some work at bundle stop
> time, using spring beans ?
> (*) rather than getting camel context, and looking for such bean throuhg
> camel registry...
>  
> Thx,
>  
> Bernard.
>  
> Bernard.
>  
>  
>  
>  
>  
>  
> --
> View this message in context: http://camel.465427.n5.nabble.com/Access-camel-context-from-BundleActivator-stop-tp5722376.html
> Sent from the Camel - Users mailing list archive at Nabble.com (http://Nabble.com).
>