Expand my Community achievements bar.

SOLVED

service null with immediate=true?

Avatar

Level 4

Hi,

I have an osgi bundle with a reference of a interface: @Reference private MyServiceInterface myserviceInterface; The bundle is built correctly and i can see the service under both system/console/services and system/console/configMgr. The MyServiceImplementation (the class that implements the interface MyServiceInterface) has the property immediate=true and his activator method runs with the build of the bundle but the variable myserviceInterface that i want to access in another class is null Have you any idea?

 

Thanks

1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

Hi,

the class containing this @Reference to MyServiceInterface has to be a component or service to make this SCR magic work.

kind regards,
Jörg

View solution in original post

9 Replies

Avatar

Level 10

By default a component providing a service is delayed and injection works properly. Looks like because you are using immediate=true- its not delayed and its affecting dependency injection. This is explained here: 

http://dev.day.com/content/ddc/blog/2010/01/dsdelayedcomponent.html

Avatar

Correct answer by
Employee Advisor

Hi,

the class containing this @Reference to MyServiceInterface has to be a component or service to make this SCR magic work.

kind regards,
Jörg

Avatar

Level 4

This is the header of the class with the reference:

@Component(metatype=true,label="MyClass")
@Service(Runnable.class)

This is the header of the class that implements the interface:

@Component(metatype = true, label = "MyService", immediate=true)
@Service(MyServiceInterface.class)

Any idea?

Avatar

Level 9

Hi samuel786431,

I don't think there is any issue with annotation attributes. May I have answers of following queries?.

  • Is class registered as OSGI service which implements "Runnable" interface?.
  • Is class registered as OSGI service which implements "MyServiceInterface"?.
  • Also, verify if services are in active state.

If above things are fine, then would like to see if there is any exception in the log. Also, would like to see code of the class which implements the Runnable interface. Can you provide that?.

Jitendra

Avatar

Level 4
The bundle is active, i don't know how to verify if a specific service is active (the service is listed inside the system console) I'm trying to simulate the problem inside a little use case:import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.apache.felix.scr.annotations.*; import org.apache.felix.scr.annotations.Component; import org.apache.felix.scr.annotations.Reference; import org.apache.felix.scr.annotations.Service; import FakePath.PabAuthService; @Component(metatype=true,label="MyTest",description="MyTest") @Service(Runnable.class) @Properties({ @Property(name="scheduler.runOn", value="LEADER"), @Property(name="scheduler.expression", value="0 * * * * ?"), @Property(name="scheduler.concurrent",boolValue=false) }) public class MyTest implements Runnable{ protected final Logger log = LoggerFactory.getLogger(this.getClass()); public void run() { } @Reference private PabAuthService PabAuthService; public String test(){ return ""+PabAuthService; }and<%@page import="com.day.cq.wcm.api.WCMMode"%> <%@page import="FakePath.service.*"%> <%@page import="FakePath.service.impl.*"%> <%MyTest test = new MyTest();%> <h2><%=test.test()%></h2>The h2 tag contains null...

Avatar

Level 9

Hi @samuel786431,

This is the problem. You can't get the reference of a service by creating MyTest Object here. MyTest is a service and reference of this service already exist.

Try below code. sling is a default object available in component jsp.jsp.

MyTest myTest = sling.getService(MyTest.class).

And then call myTest.test() inside h2 tag.

FYI :  @Reference annotation works the way dependency injection works. Here is the doc which explains all about use of Sling API.

https://helpx.adobe.com/experience-manager/using/using-sling-apis.html

---

Jitendra

Avatar

Level 4

I have another jsp with your code and it works fine but the class inside the getService is an interface

MyTest isn't an interface.

If i use <%MyTest test = sling.getService(MyTest.class);%> test will be null.

How can i assign an interface to my service?  @Service(Runnable.class, MyInterface.class) doesn't work

Avatar

Level 9

Oh yes. Sorry My bad. Let me know re-think.

Avatar

Level 9

Hi @samuel786431,

In my view, it can't be solved like this. We might have to restructure our classes. CRON job & Service separately.  We shouldn't be mixing both things together.

//Cron job with run method and a separate service with test method. In case you want to run test() method from cron job , you could do so by getting reference of the service.

--

Jitendra