Unable to reference the osgi service into a servlet | Community
Skip to main content
Parusharam
Level 2
October 4, 2018
Solved

Unable to reference the osgi service into a servlet

  • October 4, 2018
  • 12 replies
  • 6573 views

I am trying to reference a service into a servlet,but when I use @Refernce annotation i am getting 404 not found exception even though my service is registered properly.

here is my code snippet

Note: I am using OSGI R6 Annotations in AEM 6.2

Servlet:

--------

@Component(immediate = true,

        service = Servlet.class,

        property = {

                "sling.servlet.paths=/express/bin/getDynamicData",

                "sling.servlet.extensions=json",

                "sling.servlet.methods=GET"

        }

)

@Designate(ocd=DynamicDataServlet.Configuration.class)

public class DynamicDataServlet extends SlingSafeMethodsServlet {

     @Reference

     private DynamicDataService dynamicService;

@Override

    protected void doGet(final SlingHttpServletRequest request,

    final SlingHttpServletResponse response) throws ServletException, IOException {

          ------------

          -------------

          dynamicService.getData(resourceResolver, region) ;

}

}

Service interface:

-------------------

public interface DynamicDataService {

  JSONObject getDynamicData(ResourceResolver resourceResolver, String region);

}

ServiceImpl:

----------------

@Component

(service = DynamicDataService.class, name = "TNT Dynamic Data Service", immediate =true)

public class DynamicDataServiceImpl implements DynamicDataService {

@Override

  public JSONObject getDynamicData(ResourceResolver resourceResolver, String region) {

     --------

     --------

     -------

}

}

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 VeenaVikraman

Are you trying to reference SlingHttpServletRequest in your service ?

12 replies

Peter_Puzanovs
Community Advisor
Community Advisor
October 4, 2018

Dear Parush,

Your example looks fine to me. Just a small check, are you using: import org.osgi.service.component.annotations.Reference; for your reference?

Also, have a look at recent tutorial by Scott where he uses Reference and creates Services and Servlets[0] wit R6 OSGi annotations.

Anyhow, I thought that OSGi R6 were recommended from AEM 6.3 onwards, not AEM 6.2.

[0] Adobe Experience Manager Help | Integrating SOLR with Adobe Experience Manager 6.4

Regards,

Peter

smacdonald2008
Level 10
October 4, 2018

We just released new SOLR article that shows how to reference an AEM service from a Servlet where R6 annotations are used.

Servlet:

@Component(service=Servlet.class,

        property={

                Constants.SERVICE_DESCRIPTION + "=Solr Index Servlet",

                "sling.servlet.methods=" + HttpConstants.METHOD_GET,

                "sling.servlet.paths="+ "/bin/solr/push/pages"

           })

public class IndexContentToSolr extends SlingAllMethodsServlet {

private static final long serialVersionUID = 1L;

private static final Logger LOG = LoggerFactory

.getLogger(IndexContentToSolr.class);

@Reference

SolrServerConfiguration solrConfigurationService;

@Reference

SolrSearchService solrSearchService;

SolrSearchService Interface

package com.adobe.aem.core;

import java.io.IOException;

import javax.jcr.RepositoryException;

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

import org.apache.sling.commons.json.JSONArray;

import org.apache.sling.commons.json.JSONException;

import org.apache.sling.commons.json.JSONObject;

import org.apache.solr.client.solrj.SolrServerException;

import org.apache.solr.client.solrj.impl.HttpSolrClient;

import com.day.cq.search.result.SearchResult;

public interface SolrSearchService {

JSONArray crawlContent(String resourcePath, String resourceType);

JSONArray createPageMetadataArray(SearchResult results)

throws RepositoryException;

JSONObject createPageMetadataObject(Resource pageContent);

boolean indexPageToSolr(JSONObject indexPageData, HttpSolrClient server)

throws JSONException, SolrServerException, IOException;

boolean indexPagesToSolr(JSONArray indexPageData, HttpSolrClient server)

throws JSONException, SolrServerException, IOException;

}

SolrSearchServiceImpl  class

@Component

public class SolrSearchServiceImpl implements SolrSearchService {

private static final Logger LOG = LoggerFactory

.getLogger(SolrSearchServiceImpl.class);

@Reference

private QueryBuilder queryBuilder;

That works.

However - for AEM 6.2 - I have only ever used Felix SRC Annotations - not R6. Not sure if that is the issue.

For AEM 6.2 - we had Maven 10 - which used Felix SRC Annotations.

Parusharam
Level 2
October 4, 2018

Hi, Yes, I am using import org.osgi.service.component.annotations.Reference annotation only.

VeenaVikraman
Community Advisor
Community Advisor
October 4, 2018

Agree with PuzanovsP​ and smacdonald2008​ . + Could you please check your system/console/components and make sure your service is active .

Parusharam
Level 2
October 4, 2018

Yes looks like issue with the service as it is showing unsatisfied (reference). When I tried to make it active it is going to disabled state.

Do I need create ocd class for service as well..?

VeenaVikraman
Community Advisor
Community Advisor
October 4, 2018

Can you share the screen shot of the service from the system console. There should be some reference which will help us what is causing the service not to resolve

Parusharam
Level 2
October 4, 2018

VeenaVikraman
Community Advisor
Community Advisor
October 4, 2018

This is your service or servlet ?

VeenaVikraman
Community Advisor
VeenaVikramanCommunity AdvisorAccepted solution
Community Advisor
October 4, 2018

Are you trying to reference SlingHttpServletRequest in your service ?

Parusharam
Level 2
October 4, 2018

Yes, i removed it and it's working fine. But If I add Configuration-policy = require then My service is missing in /system/console/components.