Unable to trigger replication from custom workflow step | Community
Skip to main content
Level 2
May 28, 2025
Solved

Unable to trigger replication from custom workflow step

  • May 28, 2025
  • 4 replies
  • 714 views

Hey everyone,
I am working on an AEM 6.5.17 and trying to trigger content replication to the publish instance from a custom workflow step. I have injected the Replicator service in my workflow process implementation, but when the step is executed, I am getting a NullPointerException when calling replicator.replicate(...).

Here is a simple code:

public class CustomReplicationStep implements WorkflowProcess {
@3214626
private Replicator replicator;

@9944223
public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap args) {
String payloadPath = workItem.getWorkflowData().getPayload().toString();
try {
Session session = workflowSession.adaptTo(Session.class);
replicator.replicate(session, ReplicationActionType.ACTIVATE, payloadPath);
} catch (Exception e) {
log.error("Replication failed", e);
}
}
}

 

I registered this workflow properly in OSGI, and it appears in the workflow model, but the error suggests the replicator is null. I even tried switching to @586265, but it didn’t help.

Has anyone faced this before? Do I need to handle service injection differently inside workflow steps?

Thanks in advance!

Best answer by SantoshSai

Hi @vishal_kagde,

The problem is that OSGi service injection (like @Reference) does not work if you're instantiating your class manually or it's not registered properly as an OSGi component.

Try the following approach to see if it resolves the issue:

  1. Make sure your class is declared as an OSGi component with proper annotations:

    @Component(service = WorkflowProcess.class, property = {"process.label=Custom Replication Step"}) public class CustomReplicationStep implements WorkflowProcess { @Reference private Replicator replicator; @Override public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap args) throws WorkflowException { try { String path = workItem.getWorkflowData().getPayload().toString(); Session session = workflowSession.adaptTo(Session.class); replicator.replicate(session, ReplicationActionType.ACTIVATE, path); } catch (Exception e) { throw new WorkflowException("Replication failed", e); } } }

  1. Ensure the bundle is active, and check OSGi console at /system/console/components to confirm your workflow step is registered and active.

  2. One last thing worth to check: The session used for replication must have the necessary permissions. Ensure that the service user associated with the workflow has replication rights.

Official Adobe Documentation:

Hope that helps!

4 replies

SantoshSai
Community Advisor
SantoshSaiCommunity AdvisorAccepted solution
Community Advisor
May 28, 2025

Hi @vishal_kagde,

The problem is that OSGi service injection (like @Reference) does not work if you're instantiating your class manually or it's not registered properly as an OSGi component.

Try the following approach to see if it resolves the issue:

  1. Make sure your class is declared as an OSGi component with proper annotations:

    @Component(service = WorkflowProcess.class, property = {"process.label=Custom Replication Step"}) public class CustomReplicationStep implements WorkflowProcess { @Reference private Replicator replicator; @Override public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap args) throws WorkflowException { try { String path = workItem.getWorkflowData().getPayload().toString(); Session session = workflowSession.adaptTo(Session.class); replicator.replicate(session, ReplicationActionType.ACTIVATE, path); } catch (Exception e) { throw new WorkflowException("Replication failed", e); } } }

  1. Ensure the bundle is active, and check OSGi console at /system/console/components to confirm your workflow step is registered and active.

  2. One last thing worth to check: The session used for replication must have the necessary permissions. Ensure that the service user associated with the workflow has replication rights.

Official Adobe Documentation:

Hope that helps!

Santosh Sai
kapil_rajoria
Community Advisor
Community Advisor
May 28, 2025

Hi @vishal_kagde ,
For @reference to work, I hope your class is annotated as an OSGI component by using the @component annotation.

@Component(service = WorkflowProcess.class, property = {"process.label=Custom Replication Step"}) public class CustomReplicationStep implements WorkflowProcess {

 
After this, If this doesn't work, check if your bundle (http://localhost:4502/system/console/bundles) is active or not and share the snapshot here, if there's any issue.

AmitVishwakarma
Community Advisor
Community Advisor
May 28, 2025

Hi @vishal_kagde ,

Please follow below steps:

Step 1: Use Correct OSGi Annotations and Service Injection

package com.yourcompany.core.workflow; import com.adobe.granite.workflow.WorkflowException; import com.adobe.granite.workflow.exec.WorkItem; import com.adobe.granite.workflow.exec.WorkflowProcess; import com.adobe.granite.workflow.metadata.MetaDataMap; import com.day.cq.replication.ReplicationActionType; import com.day.cq.replication.Replicator; 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; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.HashMap; import java.util.Map; @Component(service = WorkflowProcess.class, property = {"process.label=Custom Page Replication Step"}) public class CustomReplicationStep implements WorkflowProcess { private static final Logger log = LoggerFactory.getLogger(CustomReplicationStep.class); @Reference private Replicator replicator; @Reference private ResourceResolverFactory resolverFactory; private static final String SERVICE_USER = "workflow-service-user"; @Override public void execute(WorkItem workItem, com.adobe.granite.workflow.exec.WorkflowSession workflowSession, MetaDataMap args) throws WorkflowException { String payloadPath = workItem.getWorkflowData().getPayload().toString(); log.info("Replicating path: {}", payloadPath); Map<String, Object> authMap = new HashMap<>(); authMap.put(ResourceResolverFactory.SUBSERVICE, SERVICE_USER); try (ResourceResolver resolver = resolverFactory.getServiceResourceResolver(authMap)) { Session session = resolver.adaptTo(Session.class); replicator.replicate(session, ReplicationActionType.ACTIVATE, payloadPath); log.info("Replication succeeded for path: {}", payloadPath); } catch (Exception e) { log.error("Replication failed", e); throw new WorkflowException("Replication failed for: " + payloadPath, e); } } }

Step 2: Create a Service User Mapping

Go to /system/console/configMgr

Search: Apache Sling Service User Mapper Service Amendment

Add entry:

com.yourcompany.core.workflow:workflow-service-user=replication-service

 

Step 3: Create Service User and Assign Permissions

Go to Useradmin (/useradmin)

Create system user: replication-service

Assign /content and replication rights:

  - jcr:read

  - replicate

  - replicate:Activate

 

Look in error.log for:

Replicating path: /content/yourpage Replication succeeded for path: /content/yourpage

 

Regards,
Amit

rahulp76079001
Level 2
June 17, 2025

It seems your session is not having replication permission for the particular resource you are trying to replicate.