I agree with Christian but would add an exception to his ruleā€¦ using 
ServiceTracker.waitForService() is useful when writing unit tests. The other 
options such as adding fixed-length sleeps are unreliable and seriously damage 
the performance of your test runs.

Neil


> On 23 Jan 2017, at 06:26, Christian Schneider <ch...@die-schneider.net> wrote:
> 
> This exactly the expected behaviour. The @Activate method allows the 
> component to configure itself. The service of the component will only be 
> published when the @Activate method has finished. If it would be published 
> earlier then the service might be in an invalid state.
> 
> Btw. You should avoid using waitForService. Instead override the addedService 
> method of ServiceTracker and continue your initialization only when the 
> service is present. Even better just inject the servcie into another 
> @Component.
> 
> Christian
> 
> 2017-01-22 21:14 GMT+01:00 Tim Jones <tim.jo...@mccarthy.co.nz 
> <mailto:tim.jo...@mccarthy.co.nz>>:
> Hi,
>  
> I am after some advice on the following.
> 
> In general terms the problem is if a new instance of a service is created via 
> a programatic call to ConfigurationAdmin.createFactoryConfiguration(), what 
> options do I have to determine that this new service instance has been 
> activated considering the call to  .createFactoryConfiguration() returns 
> immediately. 
> 
> I have tried setting up a service tracker with a filter for the specific 
> instance of the service but I am not sure I understand the expected behavior 
> of the .waitForService() method. For a small timeout period e.g. 1ms the call 
> to .waitForService() returns with a null service as I would expect. For a 
> longer timeout period e.g. 1000ms the call to .waitForService() returns with 
> non null service and from the logs I can see that it seems to return only 
> after the tracked service has returned from the @Activate method, is this the 
> expected behavior?
>  
> I also tried adding a sleep for 0.5 sec in the @Activate method of the 
> tracked service and the .waitForService() still only returned after the 
> @Activate method of that service had returned. Does the registration of a 
> service have any dependency on its activation in the context of Declarative 
> Services? Note the tracked service has @Component(immediate = true)
> 
> Although the behavior is what I am after ie .waitForService() only seems to 
> return after the tracked service has activated I am wondering if I am just 
> getting lucky in this case.
>  
>  
> Regards,
> Tim Jones
> 
> 
> 
> 
> _______________________________________________
> OSGi Developer Mail List
> osgi-dev@mail.osgi.org <mailto:osgi-dev@mail.osgi.org>
> https://mail.osgi.org/mailman/listinfo/osgi-dev 
> <https://mail.osgi.org/mailman/listinfo/osgi-dev>
> 
> 
> 
> -- 
> -- 
> Christian Schneider
> http://www.liquid-reality.de 
> <https://owa.talend.com/owa/redir.aspx?C=3aa4083e0c744ae1ba52bd062c5a7e46&URL=http%3a%2f%2fwww.liquid-reality.de>
> 
> Open Source Architect
> http://www.talend.com 
> <https://owa.talend.com/owa/redir.aspx?C=3aa4083e0c744ae1ba52bd062c5a7e46&URL=http%3a%2f%2fwww.talend.com>_______________________________________________
> OSGi Developer Mail List
> osgi-dev@mail.osgi.org
> https://mail.osgi.org/mailman/listinfo/osgi-dev

_______________________________________________
OSGi Developer Mail List
osgi-dev@mail.osgi.org
https://mail.osgi.org/mailman/listinfo/osgi-dev

Reply via email to