Carsten, thank you! I got what I wanted!

>Пятница, 17 июля 2015, 13:16 +02:00 от Carsten Ziegeler <[email protected]>:
>
>Am 17.07.15 um 13:09 schrieb Alex Sviridov:
>>  I use org.apache.felix.framework-4.6.1.jar and a bundle 
>> org.apache.felix.scr-1.8.3-SNAPSHOT.jar
>> 
>> And I have two bundles - bundleA and bundleB. => totally I have three 
>> bundles (A,B,scr)
>> 
>> BundleA   
>> #############################################################################################
>> I have a bundleA with the class TempClass:
>> 
>> import org.osgi.service.component.annotations.Component;
>> import org.osgi.service.component.annotations.ServiceScope;
>> 
>> @Component(
>>     immediate = false,
>>      scope=ServiceScope.PROTOTYPE
>> )
>> public class TempClass implements TempInterface{
>> private String temp;
>> @Override
>> public setTemp(String temp);{...}
>> @Override
>> public String getTemp(){...}
>> }
>> 
>> In order to generate ds file I use the latest version of maven bundle plugin
>>     <groupId>org.apache.felix</groupId>
>>     <artifactId>maven-bundle-plugin</artifactId>
>>     <version>2.5.4</version>
>> 
>> This plugin generates the following ds xml file:
>> <?xml version="1.0"?>
>> <scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.3.0";  
>> immediate="false">
>>   <implementation class="TempClass"/>
>>   <service scope="prototype">
>>     <provide interface="TempInterface"/>
>>   </service>
>> </scr:component>
>> 
>> BundleB   
>> #############################################################################################
>> In bundle I manually try to get two instances of service:
>> 
>> ServiceReference sr1  =  bc.getServiceReference(TempInterface.class);
>> TempInterface bean1     = (TempInterface)bc.getService(sr1);
>> bean1.setTemp("I am bean1");
>> 
>> ServiceReference sr2  =  bc.getServiceReference(TempInterface.class);
>> TempInterface bean2     = (TempInterface)bc.getService(sr2);
>> bean2.setTemp("I am bean2");
>> System.out.println("Test bean1:"+bean1.getTemp()+",test 
>> bean2:"+bean2.getTemp());
>> 
>> And this is the output:
>> Test bean1:I am bean2, test bean2:I am bean2
>> 
>> I thought that I would get "Test bean1:I am bean1, test bean2:I am bean2"
>> 
>> What is my mistake?
>> 
>In order to use prototypes (or to get several instances), you have to
>use bc.getServiceObjects(getServiceReference(TempInterface.class)) and
>then get as many instances as you want from the ServiceObjects instance.
>(of course you should check for the methods returning null in case the
>service is currently not registered etc.)
>
>Carsten
>
>> 
>> 
>
>
>-- 
>Carsten Ziegeler
>Adobe Research Switzerland
>[email protected]
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail:  [email protected]
>For additional commands, e-mail:  [email protected]
>


-- 
Alex Sviridov

Reply via email to