Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Equinox » Bug or Feature: Unexpected activation of Prototype service(Issue with SCR Prototype services)
Bug or Feature: Unexpected activation of Prototype service [message #1794503] Fri, 31 August 2018 14:29 Go to next message
Mike Rumpf is currently offline Mike RumpfFriend
Messages: 47
Registered: July 2009
Member
I encountered (at least for me) unexpected behaviour in SCR concerning prototype services. I thought it was only possible to create instances of the prototype service by calling getService() on the respective ComponentServiceObjects-reference.

However look at the following example:

I defined two service interfaces:

public interface MyProtoService {}
public interface AnotherService {}


Then I defined a service prototype implementing both interfaces and providing them as OSGi services.

@Component(service = { MyProtoService.class, AnotherService.class }, scope = ServiceScope.PROTOTYPE)
public class MyPrototype implements MyProtoService, AnotherService {}


Then I have another component collecting AnotherService-instances via the whiteboard pattern:

@Component
public class AnotherServiceCollector {

    @Reference(cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC,
            policyOption = ReferencePolicyOption.GREEDY)
    private volatile List<AnotherService> otherServices;

}


Though I don't have a single prototype_required reference in my application, a service instance instance of MyPrototype is still created and collected in AnotherServiceCollector when I start my application.

Is this expected behaviour?

Best Regards,
Mike





Re: Bug or Feature: Unexpected activation of Prototype service [message #1794509 is a reply to message #1794503] Fri, 31 August 2018 15:23 Go to previous messageGo to next message
Thomas Watson is currently offline Thomas WatsonFriend
Messages: 503
Registered: July 2009
Senior Member
See https://osgi.org/specification/osgi.cmpn/7.0.0/service.component.html#service.component-reference.scope

Yes that is expected. The reference scope tells SCR how it must obtain the service. The default (bundle scope) indicates that SCR is to use the BundleContext.getService methods to get the service. Providers of the service may be prototype services themselves but in this case the PrototypeServiceFactory representing your prototype service component gets treated as a normal ServiceFactory. What this means is that each service component within your bundle that references the service (AnotherService) using the default bundle scope will get injected with the same service instance object of type AnotherService.

HTH
Re: Bug or Feature: Unexpected activation of Prototype service [message #1794568 is a reply to message #1794509] Mon, 03 September 2018 07:20 Go to previous message
Mike Rumpf is currently offline Mike RumpfFriend
Messages: 47
Registered: July 2009
Member
Thank you. for your answer Did not know that.

Maybe a silly question: If every reference to a prototype service leads to a new instance of the proptotype service being created, is there any way to collect all existing instances of MyProtoService-instances?
Previous Topic:Accessing nested dirname inside NestedDirBundleFile
Next Topic:Equinox java 10 compatibility
Goto Forum:
  


Current Time: Sun Oct 13 07:35:18 GMT 2024

Powered by FUDForum. Page generated in 0.04400 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top