Access OSGI ser­vice from the WCMUse-class in Sightly

 

OSGI service are very helpful once its comes to the development of a module. A Service can be used to perform small task like string operations to big like processing shopping cart. For developers who are shifting to Sightly for better development practices and taking advantage of AEM 6.x features, it might be a troublesome that how a OSGI Service can be accessed in Sightly module.

Zoom out a bit and you will be able to see things more clear. Here’s all that needs to be done,

  1. You have to create a OSGI service as usual by creating a interface and implementing it.
  2. Create a class extending WCMUse and get the instance of your OSGI service. 
  3. Class created in step #2, use this in Sightly component to get the values / output

Let’s get started, 

1. Create an interface name SightlyServiceInterface.java that will be implemented by our service

2. Create a service class name SightlyService.java, define the method’s implementations 

3. Write class which extends WCMUse name Developer.java

  • Line 15 :- Getting our service instance 

4. Access values in Sightly component

5. Once component is used, output can be seen

Sightly component

Let me know if you need any help in implementation

7 thoughts on “Access OSGI ser­vice from the WCMUse-class in Sightly

  1. Hi,

    Thanks a lot for your post,it’s really helpful…

    Just one question … What happens if we have several classes(services) implementing the same interface (Ex. SightlySerivceInterface) what will be the return of getSlingScriptHelper().getService(SightlySerivceInterface.class) ??

    We should use the class name “SightlySerivce” instead ????

     
  2. Hi,

    I have service class in one bundle, model class in another bundle.. Both are deployed as OSGI bundles in AEM.

    Now my problem is, I am trying to access the service in model class.. But seems like service is not registered. Do I need to any extra configuration for this.. Could you please suggest me..

     

Leave a Reply

Your email address will not be published. Required fields are marked *