You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@felix.apache.org by Serge Merzliakov <sm...@epistatic.net> on 2010/03/04 03:10:33 UTC

BundleContext.getService() always returns same object

Hi,
   I hvae noticed that BundleContext.getService (I have not tried
ServiceTracker yet) always returns same object when used like this:

        ServiceReference ref =
bundleContext.getServiceReference(MyInterface.getClass().getName());
        MyInterface impl1 = (MyInterface)bundleContext().getService(ref);
        MyInterface impl2 = (MyInterface)bundleContext().getService(ref);

        // impl1 == impl2


Is there a way to create new instances of the service every time instead ?
If not, the simplest alternative appears to return a factory object of some
kind, and use that to create instances.


Regards,
Serge



-- 
View this message in context: http://old.nabble.com/BundleContext.getService%28%29-always-returns-same-object-tp27776283p27776283.html
Sent from the Apache Felix - Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org


Re: BundleContext.getService() always returns same object

Posted by Serge Merzliakov <sm...@epistatic.net>.
Richard,
   Re specification - OK, I suspected as much. I defined my own factory
service and it works fine.

Serge


Richard S. Hall wrote:
> 
> On 3/3/10 21:10, Serge Merzliakov wrote:
>> Hi,
>>     I hvae noticed that BundleContext.getService (I have not tried
>> ServiceTracker yet) always returns same object when used like this:
>>
>>          ServiceReference ref =
>> bundleContext.getServiceReference(MyInterface.getClass().getName());
>>          MyInterface impl1 =
>> (MyInterface)bundleContext().getService(ref);
>>          MyInterface impl2 =
>> (MyInterface)bundleContext().getService(ref);
>>
>>          // impl1 == impl2
>>
>>
>> Is there a way to create new instances of the service every time instead
>> ?
>> If not, the simplest alternative appears to return a factory object of
>> some
>> kind, and use that to create instances.
>>    
> 
> No, that's spec'ed behavior. The best you can do with the core spec is a 
> ServiceFactory which can return a different object per bundle. 
> Otherwise, you can use something like Declarative Service's component 
> factories or iPOJO to get factories like you want...or just define your 
> own factory service interface.
> 
> -> richard
> 
>>
>> Regards,
>> Serge
>>
>>
>>
>>    
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
> For additional commands, e-mail: users-help@felix.apache.org
> 
> 
> 

-- 
View this message in context: http://old.nabble.com/BundleContext.getService%28%29-always-returns-same-object-tp27776283p27777067.html
Sent from the Apache Felix - Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org


Re: BundleContext.getService() always returns same object

Posted by "Richard S. Hall" <he...@ungoverned.org>.
On 3/3/10 21:10, Serge Merzliakov wrote:
> Hi,
>     I hvae noticed that BundleContext.getService (I have not tried
> ServiceTracker yet) always returns same object when used like this:
>
>          ServiceReference ref =
> bundleContext.getServiceReference(MyInterface.getClass().getName());
>          MyInterface impl1 = (MyInterface)bundleContext().getService(ref);
>          MyInterface impl2 = (MyInterface)bundleContext().getService(ref);
>
>          // impl1 == impl2
>
>
> Is there a way to create new instances of the service every time instead ?
> If not, the simplest alternative appears to return a factory object of some
> kind, and use that to create instances.
>    

No, that's spec'ed behavior. The best you can do with the core spec is a 
ServiceFactory which can return a different object per bundle. 
Otherwise, you can use something like Declarative Service's component 
factories or iPOJO to get factories like you want...or just define your 
own factory service interface.

-> richard

>
> Regards,
> Serge
>
>
>
>    

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org