Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Unable to reference the osgi service into a servlet

Avatar

Level 2

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) {

     --------

     --------

     -------

}

}

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Are you trying to reference SlingHttpServletRequest in your service ?

View solution in original post

12 Replies

Avatar

Community Advisor

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

Avatar

Level 10

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.

Avatar

Level 2

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

Avatar

Community Advisor

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

Avatar

Level 2

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..?

Avatar

Community Advisor

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

Avatar

Correct answer by
Community Advisor

Are you trying to reference SlingHttpServletRequest in your service ?

Avatar

Level 2

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.

Avatar

Level 2

Hi, Can you just give a quick check to all the dependencies in your parent pom and their versions. Can i know which version are you using for "maven-bundle-plugin" ?

Avatar

Community Advisor

If you add that policy , your service will be resolved only if you have all the required services available.