Skip to main content
Level 2
Oct 3, 2017 3:28 PM UTC
Solved

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

[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

smacdonald2008
smacdonald2008Accepted solution
Level 10
Oct 3, 2017 7:06 PM UTC

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

srikanthp689160
Level 4
Aug 29, 2019 8:12 PM UTC

Hi,

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

Translate
Level 2
Aug 29, 2019 7:58 PM UTC

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

Translate
VeenaVikraman
New Participant
Apr 25, 2019 6:31 AM UTC

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

Translate
Level 2
Apr 23, 2019 4:01 AM UTC

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

Translate
srikanthp689160
Level 4
Feb 22, 2019 8:22 AM UTC

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

Translate
VeenaVikraman
New Participant
Feb 22, 2019 6:34 AM UTC

is your Sling Model getting Injected properly ?

Translate
srikanthp689160
Level 4
Feb 21, 2019 2:36 AM UTC

Hi Scot,

Even i am facing the same problem. OSGi services injected in a sling model is null.

Service is active, i even deleted and re-deployed bundle but it does not work. Same service when invoked from WCMUsePojo with the help of getSlingScriptHelper().getService(); works.

Can you please help me what can be done to fix this issue.

Even @Inject of SlingSettingsService is null.

Thanks & Regards,

Srikanth

Translate
VeenaVikraman
New Participant
Dec 17, 2018 7:44 AM UTC

Services should always have an interface to Implement .

Translate
New Participant
Oct 3, 2018 4:16 PM UTC

if i don't define the interface can't we inject the services.

As one of my service don't have any interface . initially it is not working and only working when i added a interface to it.

But it is existing code, i should not change that code

Translate
smacdonald2008
Level 10
Oct 3, 2017 7:24 PM UTC

I am glad that its working for you. I also added this to our AEM tip blog.

Translate