You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@karaf.apache.org by Hervé BARRAULT <he...@gmail.com> on 2012/01/16 18:07:43 UTC

Starting/Stopping Programmatically Bundles

HI, i'm using Karaf 2.0.0 and i'm trying to find a way to start/stop a
bundle with java code.

I have difficulties to find the right API to use (and which service to import).

I have tried to create a BundleListener and expose it as an osgi
service (in order to being notified of bundles start/stop) but i'm not
notified.
I thought getting data about bundles and being able to manipulate it.

What are the correct steps to do this ?

Regards
Hervé

Re: Starting/Stopping Programmatically Bundles

Posted by Andreas Pieber <an...@gmail.com>.
Hey,

I'm not quite sure what you want to achieve... If you simply want to
start/stop installed bundles the best way is to use the BundleContext (you
can get it e.g. via the Activator) and use the install method (and start
stop ond bundleContext.getBundle()).

If you want to listen for bundles started and stopped the BundleListener is
the way to go and you really should be notified. Can you share your code
here?

Kind regards,
Andreas

On Mon, Jan 16, 2012 at 18:07, Hervé BARRAULT <he...@gmail.com>wrote:

> HI, i'm using Karaf 2.0.0 and i'm trying to find a way to start/stop a
> bundle with java code.
>
> I have difficulties to find the right API to use (and which service to
> import).
>
> I have tried to create a BundleListener and expose it as an osgi
> service (in order to being notified of bundles start/stop) but i'm not
> notified.
> I thought getting data about bundles and being able to manipulate it.
>
> What are the correct steps to do this ?
>
> Regards
> Hervé
>

Re: Starting/Stopping Programmatically Bundles

Posted by Hervé BARRAULT <he...@gmail.com>.
Hi,
I have chosen to start the "Watcher" Bundle before the other ones so
it receive signals when bundles are started (it is not the neater way
but it is working for my case).

I have noticed that the getBundles method in Jconsole using JMX is
really slow and CPU consuming, i think it is a solved in current
release (not tested).

So I would not try the call to bundleContext.getBundles (even if it
would be nicer to check previously started bundle).

Thanks

Regards
Hervé

On 1/16/12, Andreas Pieber <an...@gmail.com> wrote:
> Happy to hear that it works; btw, wouldn't it be easier retrieving all
> bundles using bundleContext.getBundles and find the ones you want to
> start/stop there?
>
> Kind regards,
> Andreas
>
> On Mon, Jan 16, 2012 at 18:46, Hervé BARRAULT
> <he...@gmail.com>wrote:
>
>> Hi,
>> thanks for quick answers.
>>
>> By registering using activator it is working well for notification :)
>>
>> public class Test implements BundleListener, BundleActivator {
>>
>>    public void start(BundleContext arg0) throws Exception {
>>        arg0.addBundleListener(this);
>>    }
>>
>>    public void stop(BundleContext arg0) throws Exception {
>>        arg0.removeBundleListener(this);
>>    }
>>
>>    public void bundleChanged(BundleEvent arg0) {
>>        final Bundle bundle = arg0.getBundle();
>>
>>        System.out.println("SIGNAL" + bundle.getSymbolicName() + " - "
>> + arg0.getType());
>>
>>    }
>>
>> }
>>
>> now i think i can start/stop bundles by keeping references.
>> As i can't determine before the number of the bundle i should use the
>> symbolicName (not perfect if we have to use different versions).
>>
>> Regards
>> Hervé
>>
>>
>> On 1/16/12, Andreas Pieber <an...@gmail.com> wrote:
>> > Hey,
>> >
>> > On Mon, Jan 16, 2012 at 18:23, Hervé BARRAULT
>> > <he...@gmail.com>wrote:
>> >
>> >> I haven't register the Bundle Listener, as i thought exposing a
>> >> BundleListener as an OSGI service does something like this.
>> >>
>> >
>> > "Exposing a BundleListener" typically means register it in the OSGi
>> > registry; and this is done as JB had shown.
>> >
>> >
>> >> Is this bundleContext linked to the current bundle or all bundle ?
>> >>
>> >
>> > Well, bundleContext.getBundle() will return the "current" bundle; but
>> > you
>> > can also access other bundles using bundleContext.getBundle(ID) or one
>> > of
>> > the other overloads.
>> >
>> >
>> >> My Bundle "Checker" shall listener to other bundle and manipulate
>> >> them. I will try the BundleActivator to get the BundleContext.
>> >>
>> >
>> > If you really want to check other bundles once they come up the
>> > BundleListener is the perfect way to go. You can access the bundle
>> > object
>> > of the bundles started through the event. so you can also manipulate
>> > them
>> > there.
>> >
>> > Btw, don't you want to upgrade to a newer Karaf version (although It's
>> not
>> > required for your specific use case) there where many bugs fixed between
>> > 2.0.0 and 2.2.5.
>> >
>> > Kind regards,
>> > Andreas
>> >
>> >
>> >>
>> >> Regards
>> >> Hervé
>> >>
>> >> On 1/16/12, Jean-Baptiste Onofré <jb...@nanthrax.net> wrote:
>> >> > Hi Hervé,
>> >> >
>> >> > Did you register your listener in the bundle context, with something
>> >> like ?:
>> >> >
>> >> > getBundleContext().addBundleListener(myBundleListener);
>> >> >
>> >> > To start/stop bundle, you can do:
>> >> >
>> >> > getBundleContext().getBundle(id).stop()...
>> >> >
>> >> > Regards
>> >> > JB
>> >> >
>> >> > On 01/16/2012 06:07 PM, Hervé BARRAULT wrote:
>> >> >> HI, i'm using Karaf 2.0.0 and i'm trying to find a way to start/stop
>> a
>> >> >> bundle with java code.
>> >> >>
>> >> >> I have difficulties to find the right API to use (and which service
>> to
>> >> >> import).
>> >> >>
>> >> >> I have tried to create a BundleListener and expose it as an osgi
>> >> >> service (in order to being notified of bundles start/stop) but i'm
>> not
>> >> >> notified.
>> >> >> I thought getting data about bundles and being able to manipulate
>> >> >> it.
>> >> >>
>> >> >> What are the correct steps to do this ?
>> >> >>
>> >> >> Regards
>> >> >> Hervé
>> >> >
>> >> > --
>> >> > Jean-Baptiste Onofré
>> >> > jbonofre@apache.org
>> >> > http://blog.nanthrax.net
>> >> > Talend - http://www.talend.com
>> >> >
>> >>
>> >
>>
>

Re: Starting/Stopping Programmatically Bundles

Posted by Andreas Pieber <an...@gmail.com>.
Happy to hear that it works; btw, wouldn't it be easier retrieving all
bundles using bundleContext.getBundles and find the ones you want to
start/stop there?

Kind regards,
Andreas

On Mon, Jan 16, 2012 at 18:46, Hervé BARRAULT <he...@gmail.com>wrote:

> Hi,
> thanks for quick answers.
>
> By registering using activator it is working well for notification :)
>
> public class Test implements BundleListener, BundleActivator {
>
>    public void start(BundleContext arg0) throws Exception {
>        arg0.addBundleListener(this);
>    }
>
>    public void stop(BundleContext arg0) throws Exception {
>        arg0.removeBundleListener(this);
>    }
>
>    public void bundleChanged(BundleEvent arg0) {
>        final Bundle bundle = arg0.getBundle();
>
>        System.out.println("SIGNAL" + bundle.getSymbolicName() + " - "
> + arg0.getType());
>
>    }
>
> }
>
> now i think i can start/stop bundles by keeping references.
> As i can't determine before the number of the bundle i should use the
> symbolicName (not perfect if we have to use different versions).
>
> Regards
> Hervé
>
>
> On 1/16/12, Andreas Pieber <an...@gmail.com> wrote:
> > Hey,
> >
> > On Mon, Jan 16, 2012 at 18:23, Hervé BARRAULT
> > <he...@gmail.com>wrote:
> >
> >> I haven't register the Bundle Listener, as i thought exposing a
> >> BundleListener as an OSGI service does something like this.
> >>
> >
> > "Exposing a BundleListener" typically means register it in the OSGi
> > registry; and this is done as JB had shown.
> >
> >
> >> Is this bundleContext linked to the current bundle or all bundle ?
> >>
> >
> > Well, bundleContext.getBundle() will return the "current" bundle; but you
> > can also access other bundles using bundleContext.getBundle(ID) or one of
> > the other overloads.
> >
> >
> >> My Bundle "Checker" shall listener to other bundle and manipulate
> >> them. I will try the BundleActivator to get the BundleContext.
> >>
> >
> > If you really want to check other bundles once they come up the
> > BundleListener is the perfect way to go. You can access the bundle object
> > of the bundles started through the event. so you can also manipulate them
> > there.
> >
> > Btw, don't you want to upgrade to a newer Karaf version (although It's
> not
> > required for your specific use case) there where many bugs fixed between
> > 2.0.0 and 2.2.5.
> >
> > Kind regards,
> > Andreas
> >
> >
> >>
> >> Regards
> >> Hervé
> >>
> >> On 1/16/12, Jean-Baptiste Onofré <jb...@nanthrax.net> wrote:
> >> > Hi Hervé,
> >> >
> >> > Did you register your listener in the bundle context, with something
> >> like ?:
> >> >
> >> > getBundleContext().addBundleListener(myBundleListener);
> >> >
> >> > To start/stop bundle, you can do:
> >> >
> >> > getBundleContext().getBundle(id).stop()...
> >> >
> >> > Regards
> >> > JB
> >> >
> >> > On 01/16/2012 06:07 PM, Hervé BARRAULT wrote:
> >> >> HI, i'm using Karaf 2.0.0 and i'm trying to find a way to start/stop
> a
> >> >> bundle with java code.
> >> >>
> >> >> I have difficulties to find the right API to use (and which service
> to
> >> >> import).
> >> >>
> >> >> I have tried to create a BundleListener and expose it as an osgi
> >> >> service (in order to being notified of bundles start/stop) but i'm
> not
> >> >> notified.
> >> >> I thought getting data about bundles and being able to manipulate it.
> >> >>
> >> >> What are the correct steps to do this ?
> >> >>
> >> >> Regards
> >> >> Hervé
> >> >
> >> > --
> >> > Jean-Baptiste Onofré
> >> > jbonofre@apache.org
> >> > http://blog.nanthrax.net
> >> > Talend - http://www.talend.com
> >> >
> >>
> >
>

Re: Starting/Stopping Programmatically Bundles

Posted by Hervé BARRAULT <he...@gmail.com>.
Hi,
thanks for quick answers.

By registering using activator it is working well for notification :)

public class Test implements BundleListener, BundleActivator {

    public void start(BundleContext arg0) throws Exception {
        arg0.addBundleListener(this);
    }

    public void stop(BundleContext arg0) throws Exception {
        arg0.removeBundleListener(this);
    }

    public void bundleChanged(BundleEvent arg0) {
        final Bundle bundle = arg0.getBundle();

        System.out.println("SIGNAL" + bundle.getSymbolicName() + " - "
+ arg0.getType());

    }

}

now i think i can start/stop bundles by keeping references.
As i can't determine before the number of the bundle i should use the
symbolicName (not perfect if we have to use different versions).

Regards
Hervé


On 1/16/12, Andreas Pieber <an...@gmail.com> wrote:
> Hey,
>
> On Mon, Jan 16, 2012 at 18:23, Hervé BARRAULT
> <he...@gmail.com>wrote:
>
>> I haven't register the Bundle Listener, as i thought exposing a
>> BundleListener as an OSGI service does something like this.
>>
>
> "Exposing a BundleListener" typically means register it in the OSGi
> registry; and this is done as JB had shown.
>
>
>> Is this bundleContext linked to the current bundle or all bundle ?
>>
>
> Well, bundleContext.getBundle() will return the "current" bundle; but you
> can also access other bundles using bundleContext.getBundle(ID) or one of
> the other overloads.
>
>
>> My Bundle "Checker" shall listener to other bundle and manipulate
>> them. I will try the BundleActivator to get the BundleContext.
>>
>
> If you really want to check other bundles once they come up the
> BundleListener is the perfect way to go. You can access the bundle object
> of the bundles started through the event. so you can also manipulate them
> there.
>
> Btw, don't you want to upgrade to a newer Karaf version (although It's not
> required for your specific use case) there where many bugs fixed between
> 2.0.0 and 2.2.5.
>
> Kind regards,
> Andreas
>
>
>>
>> Regards
>> Hervé
>>
>> On 1/16/12, Jean-Baptiste Onofré <jb...@nanthrax.net> wrote:
>> > Hi Hervé,
>> >
>> > Did you register your listener in the bundle context, with something
>> like ?:
>> >
>> > getBundleContext().addBundleListener(myBundleListener);
>> >
>> > To start/stop bundle, you can do:
>> >
>> > getBundleContext().getBundle(id).stop()...
>> >
>> > Regards
>> > JB
>> >
>> > On 01/16/2012 06:07 PM, Hervé BARRAULT wrote:
>> >> HI, i'm using Karaf 2.0.0 and i'm trying to find a way to start/stop a
>> >> bundle with java code.
>> >>
>> >> I have difficulties to find the right API to use (and which service to
>> >> import).
>> >>
>> >> I have tried to create a BundleListener and expose it as an osgi
>> >> service (in order to being notified of bundles start/stop) but i'm not
>> >> notified.
>> >> I thought getting data about bundles and being able to manipulate it.
>> >>
>> >> What are the correct steps to do this ?
>> >>
>> >> Regards
>> >> Hervé
>> >
>> > --
>> > Jean-Baptiste Onofré
>> > jbonofre@apache.org
>> > http://blog.nanthrax.net
>> > Talend - http://www.talend.com
>> >
>>
>

Re: Starting/Stopping Programmatically Bundles

Posted by Andreas Pieber <an...@gmail.com>.
Hey,

On Mon, Jan 16, 2012 at 18:23, Hervé BARRAULT <he...@gmail.com>wrote:

> I haven't register the Bundle Listener, as i thought exposing a
> BundleListener as an OSGI service does something like this.
>

"Exposing a BundleListener" typically means register it in the OSGi
registry; and this is done as JB had shown.


> Is this bundleContext linked to the current bundle or all bundle ?
>

Well, bundleContext.getBundle() will return the "current" bundle; but you
can also access other bundles using bundleContext.getBundle(ID) or one of
the other overloads.


> My Bundle "Checker" shall listener to other bundle and manipulate
> them. I will try the BundleActivator to get the BundleContext.
>

If you really want to check other bundles once they come up the
BundleListener is the perfect way to go. You can access the bundle object
of the bundles started through the event. so you can also manipulate them
there.

Btw, don't you want to upgrade to a newer Karaf version (although It's not
required for your specific use case) there where many bugs fixed between
2.0.0 and 2.2.5.

Kind regards,
Andreas


>
> Regards
> Hervé
>
> On 1/16/12, Jean-Baptiste Onofré <jb...@nanthrax.net> wrote:
> > Hi Hervé,
> >
> > Did you register your listener in the bundle context, with something
> like ?:
> >
> > getBundleContext().addBundleListener(myBundleListener);
> >
> > To start/stop bundle, you can do:
> >
> > getBundleContext().getBundle(id).stop()...
> >
> > Regards
> > JB
> >
> > On 01/16/2012 06:07 PM, Hervé BARRAULT wrote:
> >> HI, i'm using Karaf 2.0.0 and i'm trying to find a way to start/stop a
> >> bundle with java code.
> >>
> >> I have difficulties to find the right API to use (and which service to
> >> import).
> >>
> >> I have tried to create a BundleListener and expose it as an osgi
> >> service (in order to being notified of bundles start/stop) but i'm not
> >> notified.
> >> I thought getting data about bundles and being able to manipulate it.
> >>
> >> What are the correct steps to do this ?
> >>
> >> Regards
> >> Hervé
> >
> > --
> > Jean-Baptiste Onofré
> > jbonofre@apache.org
> > http://blog.nanthrax.net
> > Talend - http://www.talend.com
> >
>

Re: Starting/Stopping Programmatically Bundles

Posted by Hervé BARRAULT <he...@gmail.com>.
I haven't register the Bundle Listener, as i thought exposing a
BundleListener as an OSGI service does something like this.

Is this bundleContext linked to the current bundle or all bundle ?

My Bundle "Checker" shall listener to other bundle and manipulate
them. I will try the BundleActivator to get the BundleContext.

Regards
Hervé

On 1/16/12, Jean-Baptiste Onofré <jb...@nanthrax.net> wrote:
> Hi Hervé,
>
> Did you register your listener in the bundle context, with something like ?:
>
> getBundleContext().addBundleListener(myBundleListener);
>
> To start/stop bundle, you can do:
>
> getBundleContext().getBundle(id).stop()...
>
> Regards
> JB
>
> On 01/16/2012 06:07 PM, Hervé BARRAULT wrote:
>> HI, i'm using Karaf 2.0.0 and i'm trying to find a way to start/stop a
>> bundle with java code.
>>
>> I have difficulties to find the right API to use (and which service to
>> import).
>>
>> I have tried to create a BundleListener and expose it as an osgi
>> service (in order to being notified of bundles start/stop) but i'm not
>> notified.
>> I thought getting data about bundles and being able to manipulate it.
>>
>> What are the correct steps to do this ?
>>
>> Regards
>> Hervé
>
> --
> Jean-Baptiste Onofré
> jbonofre@apache.org
> http://blog.nanthrax.net
> Talend - http://www.talend.com
>

Re: Starting/Stopping Programmatically Bundles

Posted by Jean-Baptiste Onofré <jb...@nanthrax.net>.
Hi Hervé,

Did you register your listener in the bundle context, with something like ?:

getBundleContext().addBundleListener(myBundleListener);

To start/stop bundle, you can do:

getBundleContext().getBundle(id).stop()...

Regards
JB

On 01/16/2012 06:07 PM, Hervé BARRAULT wrote:
> HI, i'm using Karaf 2.0.0 and i'm trying to find a way to start/stop a
> bundle with java code.
>
> I have difficulties to find the right API to use (and which service to import).
>
> I have tried to create a BundleListener and expose it as an osgi
> service (in order to being notified of bundles start/stop) but i'm not
> notified.
> I thought getting data about bundles and being able to manipulate it.
>
> What are the correct steps to do this ?
>
> Regards
> Hervé

-- 
Jean-Baptiste Onofré
jbonofre@apache.org
http://blog.nanthrax.net
Talend - http://www.talend.com