How do I consume a DS? (Take 2) [message #666957] |
Mon, 25 April 2011 06:02 |
|
I posted a question last week but got no response so I will try rephrasing the question. How does one consume a service declared using Declarative Services? The current practice seems to be to inject it rather than using a ServiceTracker. I have to assume this means declaring the service consumer itself as a component under Declarative Services, not specifying a service but instead referencing the required service. For example:
Component registering service to be consumed:
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" activate="activate" deactivate="deactivate" immediate="false" name="ca.footeware.hello.service">
<implementation class="ca.footeware.hello.service.internal.HelloService"/>
<service>
<provide interface="ca.footeware.hello.service.IHelloService"/>
</service>
</scr:component>
Component consuming above service:
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" activate="activate" deactivate="deactivate" name="ca.footeware.hello.service.consumer">
<implementation class="ca.footeware.hello.service.consumer.HelloConsumer"/>
<reference bind="setHelloService" cardinality="1..1" interface="ca.footeware.hello.service.IHelloService" name="helloService" policy="static" unbind="unsetHelloService"/>
</scr:component>
This works fine but in practice I often have need for the service in a class whose lifecycle I cannot place under the control of Declarative Services, e.g. a ViewPart. Where once I would call out from the ViewPart to its containing bundle's Activator where I had a ServiceTracker to provide me the service, I'm trying to refactor to remove the ServiceTracker and somehow inject the service instead. I presume it should be injected where I need it, i.e. the ViewPart.
Using the above component structure, with my ViewPart as the consuming component, things don't work quite right. DS creates an instance of my ViewPart on application start, injects the service and calls its activate(). Then my RCP application creates another ViewPart instance when its Window > Show View menu is invoked. This new instance never gets injected or activated! How would you consume a DS from a ViewPart - I'm obviously doing it wrong
What am I missing? Is there some combination of settings in the component.xml's that will make this work? Or should I componentize the Activator and again reach out to it from the ViewPart? Come on people, someone must have some ideas. I've been stuck here for weeks and I'm worried the mark on my forehead will become permanent!
Craig
|
|
|
|
Re: How do I consume a DS? (Take 2) [message #667133 is a reply to message #667001] |
Wed, 27 April 2011 00:18 |
|
Thanks Paul. That clears things up for me.
My service is not now dynamic but I was hoping to code its consumption to work if it later became so. I'll try injecting it into a utility class as a static variable and consume it in my ViewPart, that should work. I'm just glad I'm not chasing that wild goose anymore!
I'm a little disappointed that DS has to immediately load a pojo to inject it with a referenced service. I was expecting immediate=true to specify that. Without it, I expected the pojo to be injected with its reference and activated only when it was instantiated, much as when a declared service is created only when needed.
Am I correct that in Eclipse 4.x, the @Inject annotation will inject a service into a ViewPart (or any platform managed class) only when it's instantiated by the platform?
Thanks again,
Craig
|
|
|
|
Powered by
FUDForum. Page generated in 0.03626 seconds