Bundle start is an asynchronous call

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Bundle start is an asynchronous call

SAI3292
Hi

I am trying the update the resolved bundle and then starting it which is
providing implementation for Interface I. Afterwards immediately getting the
service reference for the Interface I. Interface I is in different module
which is started.
Following is the code:

Bundle bundle = bundleContext.getBundle(mvn:path);
bundle.update();
bundle.start();
Collection<ServiceReference&lt;I>> referenceList = bundleContext
                                                        .getServiceReferences(I.class, null);

Is that when i am getting service reference of the interface, the bundle
start is not yet registered the implementation of the Interface I in
Activator.  Is that an Asynchronous call.

Can anybody help

Regards
Sai



--
Sent from: http://karaf.922171.n3.nabble.com/Karaf-User-f930749.html
Reply | Threaded
Open this post in threaded view
|

Re: Bundle start is an asynchronous call

Tim Ward-2
You must never write code like this in OSGi. It is a serious error to assume that a service will be available immediately after a bundle has started, you must always listen for the service becoming available (which may happen a long time in the future).

I’m not sure what it is that you’re doing which requires you to use the low-level API like this. I would not recommend directly interacting with the bundle context, but instead using a dependency management container such as Declarative Services to inject you with the service when it becomes available. Using the low level API is typically a good way to ensure that your code is complex and unreadable.

Regards,

Tim

> On 31 Jan 2018, at 13:59, SAI3292 <[hidden email]> wrote:
>
> Hi
>
> I am trying the update the resolved bundle and then starting it which is
> providing implementation for Interface I. Afterwards immediately getting the
> service reference for the Interface I. Interface I is in different module
> which is started.
> Following is the code:
>
> Bundle bundle = bundleContext.getBundle(mvn:path);
> bundle.update();
> bundle.start();
> Collection<ServiceReference&lt;I>> referenceList = bundleContext
> .getServiceReferences(I.class, null);
>
> Is that when i am getting service reference of the interface, the bundle
> start is not yet registered the implementation of the Interface I in
> Activator.  Is that an Asynchronous call.
>
> Can anybody help
>
> Regards
> Sai
>
>
>
> --
> Sent from: http://karaf.922171.n3.nabble.com/Karaf-User-f930749.html

Reply | Threaded
Open this post in threaded view
|

Re: Bundle start is an asynchronous call

jbonofre
In reply to this post by SAI3292
Hi Sai,

I would use a service tracker for that and react as soon as the service is
available.

Calling start on the bundle actually call start() method of the Activator (or
equivalent when using SCR or blueprint). Depending of the Activator, the service
registration can happen async.

Is it a "classic" OSGi bundle ? a blueprint bundle ? a SCR bundle ?

Regards
JB

On 01/31/2018 02:59 PM, SAI3292 wrote:

> Hi
>
> I am trying the update the resolved bundle and then starting it which is
> providing implementation for Interface I. Afterwards immediately getting the
> service reference for the Interface I. Interface I is in different module
> which is started.
> Following is the code:
>
> Bundle bundle = bundleContext.getBundle(mvn:path);
> bundle.update();
> bundle.start();
> Collection<ServiceReference&lt;I>> referenceList = bundleContext
> .getServiceReferences(I.class, null);
>
> Is that when i am getting service reference of the interface, the bundle
> start is not yet registered the implementation of the Interface I in
> Activator.  Is that an Asynchronous call.
>
> Can anybody help
>
> Regards
> Sai
>
>
>
> --
> Sent from: http://karaf.922171.n3.nabble.com/Karaf-User-f930749.html
>

--
Jean-Baptiste Onofré
[hidden email]
http://blog.nanthrax.net
Talend - http://www.talend.com
Reply | Threaded
Open this post in threaded view
|

Re: Bundle start is an asynchronous call

SAI3292
In reply to this post by SAI3292
Hi

Firstly thank you for the reply and suggestion Tim Ward and Jbonofre.
It is an Classic OSGi bundle.
Actually we use Felix dependency manager for the activation and dependency
injection.
The reason of using low level API is that we are having an upgrade service
which runs periodically and now we want extra work to done in upgrade so i
have to download new module and start it and do some execution.
I tried with service tracker instead of service reference and it worked for
me

Thank You
Sai



--
Sent from: http://karaf.922171.n3.nabble.com/Karaf-User-f930749.html