You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@felix.apache.org by "Neil Bartlett (JIRA)" <ji...@apache.org> on 2016/03/24 18:09:25 UTC

[jira] [Comment Edited] (FELIX-5213) ComponentContext.getServiceReference returns null though service registered

    [ https://issues.apache.org/jira/browse/FELIX-5213?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15210567#comment-15210567 ] 

Neil Bartlett edited comment on FELIX-5213 at 3/24/16 5:08 PM:
---------------------------------------------------------------

There are two cases. If the component publishes its service before any consumer tries to fetch it, then we should get the ServiceRegistration during activate. This does seem to work correctly.

On the other hand if somebody was already listening for the service when we registered and they call getService synchronously from the serviceChanged event, then SCR doesn't yet have either the ServiceRegistration OR the ServiceReference because OSGi hasn't returned from registerService yet. So SCR cannot possibly see the ServiceRegistration yet.

So it's not a Felix/SCR bug, but certainly a gotcha in the spec.


was (Author: njbartlett):
There are two cases. If the component publishes its service before any consumer tries to fetch it, then we should get the ServiceRegistration during activate. This does seem to work correctly.

On the other hand if somebody was already listening for the service when we registered and they call getService synchronously from the serviceChanged event, then SCR doesn't yet have either the ServiceRegistration OR the ServiceReference because OSGi hasn't returned from registerService yet. So DS cannot possibly see the ServiceRegistration yet.

So it's not a Felix/SCR bug, but certainly a gotcha in the spec.

> ComponentContext.getServiceReference returns null though service registered
> ---------------------------------------------------------------------------
>
>                 Key: FELIX-5213
>                 URL: https://issues.apache.org/jira/browse/FELIX-5213
>             Project: Felix
>          Issue Type: Bug
>          Components: Declarative Services (SCR)
>    Affects Versions: scr-2.0.2
>            Reporter: Neil Bartlett
>
> In a component that provides a service and is not immediate, the service is registered before activation. Therefore the service reference of the registered service should be available to the component, from ComponentContext.getServiceReference(), during activation. However the following code sample throws NPE:
> {code}
> import org.osgi.framework.Constants;
> import org.osgi.framework.ServiceReference;
> import org.osgi.service.component.ComponentContext;
> import org.osgi.service.component.annotations.Activate;
> import org.osgi.service.component.annotations.Component;
> @Component(service = Object.class, property = "name=scrbug")
> public class Example {
> 	@Activate
> 	void activate(ComponentContext context) {
> 		ServiceReference<?> reference = context.getServiceReference();
> 		System.out.printf("My service ID is %d%n", reference.getProperty(Constants.SERVICE_ID));
> 	}
> }
> {code}
> Note that the service certainly exists, because I have injected it into a second component, in order to force activation of the above.



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)