AEM 6.3 || How to call OSGi Service in Sling Model with Service wrote by OSGi R6 Annotation | Community
Skip to main content
Level 2
October 3, 2017
Solved

AEM 6.3 || How to call OSGi Service in Sling Model with Service wrote by OSGi R6 Annotation

  • October 3, 2017
  • 16 replies
  • 46308 views

[Thread Edited By Adobe]

/*Don’t forget to meet and greet your fellow peers virtually by telling them about yourself here

Go ahead and to it now: https://adobe.ly/3eDnB4v */

 

Actual Question:

Dear AEM Community,

 

 

Currently, I'm working on AEM 6.3 OSGi R6 Official OSGi Declarative Services Annotations in AEM - Adobe Experience Manager | AEM/CQ | Apache Sling

But we can't call it in Sling model, we can't use annotation @3214626 (import org.osgi.service.component.annotations.Reference) or @586265 ( from Sling Model) or @586265 @3200416("osgi-services").

 

Any help would be greatly appreciated.

 

Thanks,

Thomas

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by smacdonald2008

THe getSlingScriptHelper().getService();

only works from WCMUsePojo.

I just tested using @inject and it works to inject a running AEM Service into a Sling Model .

Given these classes:

KeyService interface:

package com.community.aem.core;

public interface KeyService {

 

    public void setKey(int val);

    public String getKey();

}

KeyServiceImpl class

package com.community.aem.core;

import org.apache.felix.scr.annotations.Component;

import org.apache.felix.scr.annotations.Service;

//This is a component so it can provide or consume services

@Component

@Service

public class KeyServiceImpl implements KeyService {

  

    //Define a class member named key

    private int key = 0 ;

  

    @Override

    //A basic setter method that sets key

    public void setKey(int val)

    {

        //Set the key class member

        this.key = val ;

       

    }

    @Override

    //A basic getter that gets key

    public String getKey()

    {

        //return the value of the key class member

       

        //Convert the int to a String to display it within an AEM web page

        String strI = Integer.toString(this.key);

        return strI;

    }

}

.

We can inject KeyService into HelloWorldModel - see:

package com.community.aem.core.models;

import javax.annotation.PostConstruct;

import javax.inject.Inject;

import javax.inject.Named;

import com.community.aem.core.KeyService;

import org.apache.sling.api.resource.Resource;

import org.apache.sling.models.annotations.Default;

import org.apache.sling.models.annotations.Model;

import org.apache.sling.settings.SlingSettingsService;

@Model(adaptables=Resource.class)

public class HelloWorldModel {

    @Inject

    private KeyService keyService;

 

    @Inject

    private SlingSettingsService settings;

    @Inject @Named("sling:resourceType") @Default(values="No resourceType")

    protected String resourceType;

    private String message;

    @PostConstruct

    protected void init() {

     

       

        keyService.setKey(80) ;

     

        message = "\tHello World! - the keyservice is " +keyService.getKey() +" \n";

        message += "\tThis is instance: " + settings.getSlingId() + "\n";

        message += "\tResource type is: " + resourceType + "\n";

    }

    public String getMessage() {

        return message;

    }

}

This works -- you can see the successful output here. I hope this clears up how to reference a running AEM Service from WCMUsePojo and Sling models.

You can try this too by following this article to get the default class:

Creating an Adobe Experience Manager 6.3 Project using Adobe Maven Archetype 11

Then add the KeyService and Impl class to the com.community.aem.core package. You will get the same result.  BE  sure to add the new code (bolded code above) to the Sling Model class too!

16 replies

VeenaVikraman
Community Advisor
Community Advisor
February 22, 2019

is your Sling Model getting Injected properly ?

srikanthp689160
Level 4
February 22, 2019

Hi Veena,

Sling model is injected correctly. I was able to resolve by using annotation @OSGiService instead of @inject for service invocation to work in AEM 6.4.

Thanks for the response.

Thanks & Regards,

Srikanth

Level 2
April 23, 2019

Can anyone explain why? Why do we have to have a interface for every service?smacdonald2008

VeenaVikraman
Community Advisor
Community Advisor
April 25, 2019

vishalsaini

As you are aware that we work with OSGI Services. So the basic documentations says "An OSGi service is a java object instance, registered into an OSGi framework with a set of properties. Any java object can be registered as a service, but typically it implements a well-known interface." 

Some references that might help you https://www.osgi.org/developer/architecture/

https://www.osgi.org/developer/architecture/

https://www.knopflerfish.org/osgi_service_tutorial.html

https://www.vogella.com/tutorials/OSGiServices/article.html

Level 2
August 29, 2019

I tried both @OSGiService and @Inject to call service in Sling model. It's Not working in both cases.

I  am using AEM 6.4 with SP6.4.5

i am getting this error:

Caused by: org.apache.sling.scripting.sightly.SightlyException: Identifier com.arya.mypackage.core.models.HelloWorldModel cannot be correctly instantiated by the Use API

at org.apache.sling.scripting.sightly.impl.engine.extension.use.UseRuntimeExtension.call(UseRuntimeExtension.java:78) [org.apache.sling.scripting.sightly:1.0.48.1_3_1]

at org.apache.sling.scripting.sightly.impl.engine.runtime.RenderContextImpl.call(RenderContextImpl.java:69) [org.apache.sling.scripting.sightly:1.0.48.1_3_1]

at org.apache.sling.scripting.sightly.apps.aryaproject.components.content.helloworld.helloworld_html.render(helloworld_html.java:55)

at org.apache.sling.scripting.sightly.java.compiler.RenderUnit.render(RenderUnit.java:48) [org.apache.sling.scripting.sightly.compiler.java:1.0.22.1_3_1]

at org.apache.sling.scripting.sightly.impl.engine.SightlyCompiledScript.eval(SightlyCompiledScript.java:61) [org.apache.sling.scripting.sightly:1.0.48.1_3_1]

at org.apache.sling.scripting.core.impl.DefaultSlingScript.call(DefaultSlingScript.java:386) [org.apache.sling.scripting.core:2.0.54]

at org.apache.sling.scripting.core.impl.DefaultSlingScript.eval(DefaultSlingScript.java:184) [org.apache.sling.scripting.core:2.0.54]

at org.apache.sling.scripting.core.impl.DefaultSlingScript.service(DefaultSlingScript.java:491) [org.apache.sling.scripting.core:2.0.54]

... 250 common frames omitted

Caused by: org.apache.sling.models.factory.MissingElementsException: Could not inject all required fields into class com.arya.mypackage.core.models.HelloWorldModel

at org.apache.sling.models.impl.ModelAdapterFactory.createObject(ModelAdapterFactory.java:679) [org.apache.sling.models.impl:1.4.7.T20180205124646-b0647a3]

at org.apache.sling.models.impl.ModelAdapterFactory.internalCreateModel(ModelAdapterFactory.java:394) [org.apache.sling.models.impl:1.4.7.T20180205124646-b0647a3]

at org.apache.sling.models.impl.ModelAdapterFactory.createModel(ModelAdapterFactory.java:261) [org.apache.sling.models.impl:1.4.7.T20180205124646-b0647a3]

at org.apache.sling.scripting.sightly.models.impl.SlingModelsUseProvider.provide(SlingModelsUseProvider.java:135) [org.apache.sling.scripting.sightly.models.provider:1.0.6]

at org.apache.sling.scripting.sightly.impl.engine.extension.use.UseRuntimeExtension.call(UseRuntimeExtension.java:73) [org.apache.sling.scripting.sightly:1.0.48.1_3_1]

... 257 common frames omitted

Suppressed: org.apache.sling.models.factory.MissingElementException: Could not inject private com.arya.mypackage.core.interfaces.KeyService com.arya.mypackage.core.models.HelloWorldModel.keyService

at org.apache.sling.models.impl.ModelAdapterFactory.createObject(ModelAdapterFactory.java:684) [org.apache.sling.models.impl:1.4.7.T20180205124646-b0647a3]

... 261 common frames omitted

Caused by: org.apache.sling.models.factory.ModelClassException: No injector returned a non-null value!

at org.apache.sling.models.impl.ModelAdapterFactory.injectElement(ModelAdapterFactory.java:581) [org.apache.sling.models.impl:1.4.7.T20180205124646-b0647a3]

at org.apache.sling.models.impl.ModelAdapterFactory.createObject(ModelAdapterFactory.java:682) [org.apache.sling.models.impl:1.4.7.T20180205124646-b0647a3]

... 261 common frames omitted

srikanthp689160
Level 4
August 29, 2019

Hi,

Can you check if your sling model is adaptable to both resource and SlingHttpServletRequest i.e. @Model(adaptables = { Resource.class, SlingHttpServletRequest.class })