Replicator is null | Community
Skip to main content
Level 2
February 6, 2024
Solved

Replicator is null

  • February 6, 2024
  • 3 replies
  • 1601 views

Hi 

I am trying to create a servlet in AEM to unplish some pages. The servlet keeps throwing the nullpointer at:

replicator.replicate(session, ReplicationActionType.DEACTIVE, path); 

 

I found the root cause is the replictor is null, here's how I set it:

@3214626
Replicator replicator;


I tried to do maven install and restarted the server multiple times but still not working, please advise.

 

Thank you

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 SureshDhulipudi

can you change Reference also to OSGi instead of Apache

 

import org.osgi.service.component.annotations.Reference;

3 replies

SureshDhulipudi
Community Advisor
Community Advisor
February 6, 2024

can you paste complete service class code here:

 

== example ==

import com.day.cq.replication.Replicator;
import com.day.cq.replication.ReplicationActionType;
import com.day.cq.replication.ReplicationException;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

import javax.jcr.Session;

@8220494(service = MyPageDeactivationService.class)
public class MyPageDeactivationService {

@3214626
private Replicator replicator;

@3214626
private ResourceResolverFactory resolverFactory;

public void deactivatePage(String path) {
try {
ResourceResolver resolver = resolverFactory.getServiceResourceResolver(null);
PageManager pageManager = resolver.adaptTo(PageManager.class);
Page page = pageManager.getPage(path);

if (page != null) {
Session session = resolver.adaptTo(Session.class);
replicator.replicate(session, ReplicationActionType.DEACTIVATE, path);
}
} catch (Exception e) {
// handle exception
}
}
}

 

 

 

Raja_Reddy
Community Advisor
Community Advisor
February 7, 2024

Hi @hanl 
Please try this

@Override protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) { try { String queryString = "SELECT [jcr:path] FROM [cq:Page] AS s WHERE ISDESCENDANTNODE([/content/test])"; ResourceResolver resourceResolver = request.getResourceResolver(); Session session = resourceResolver.adaptTo(Session.class); QueryManager queryManager = session.getWorkspace().getQueryManager(); Query query = queryManager.createQuery(queryString, Query.JCR_SQL2); QueryResult queryResult = query.execute(); RowIterator rowIterator = queryResult.getRows(); while (rowIterator.hasNext()) { String path = rowIterator.nextRow().getPath(); Resource jcrContent = resourceResolver.getResource(path + "/jcr:content"); if (jcrContent != null) { ValueMap properties = jcrContent.getValueMap(); Boolean autoUnpublishAtExpiry = properties.get("autoUnpublishAtExpiry", Boolean.class); Date expiryDate = properties.get("expiry", Date.class); Date currentDate = new Date(); if (autoUnpublishAtExpiry != null && autoUnpublishAtExpiry && expiryDate != null && currentDate.after(expiryDate)) { replicator.replicate(session, ReplicationActionType.DEACTIVATE, path); } } } session.save(); response.getWriter().write("Expired Page unpublished successfully"); } catch (Exception e) { log.error("Error occurred while unpublishing pages: {}", e.getMessage(), e); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); response.getWriter().write("Error occurred while unpublishing pages: " + e.getMessage()); } }
SureshDhulipudi
Community Advisor
Community Advisor
February 6, 2024

Check your bundle is up and running and also validate this service 

Dipti_Chauhan
Community Advisor
Community Advisor
February 7, 2024

Please check if your service is up and running. Otherwise I don't see any issue with the code snippet you shared.

 

Sharing working example which is also using osgi annotation :

import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.SlingHttpServletResponse; import org.apache.sling.api.servlets.SlingAllMethodsServlet; import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Reference; import com.day.cq.replication.ReplicationActionType; import com.day.cq.replication.Replicator; @8220494(service = SlingAllMethodsServlet.class, property = { "sling.servlet.methods=POST", "sling.servlet.paths=/bin/publishPage" }) public class PublishPageServlet extends SlingAllMethodsServlet { @3214626 private Replicator replicator; @9944223 protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws Exception { String path = request.getParameter("path"); // Get the path of the page to publish if (path != null) { replicator.replicate(request.getResourceResolver().adaptTo(javax.jcr.Session.class), ReplicationActionType.ACTIVATE, path); response.getWriter().write("Page published successfully"); } else { response.setStatus(400); response.getWriter().write("Missing 'path' parameter"); } } }