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 {
@reference
private Replicator replicator;
@Override
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 @inject, but it didn’t help.
Has anyone faced this before? Do I need to handle service injection differently inside workflow steps?
Thanks in advance!
Solved! Go to Solution.
Views
Replies
Total Likes
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:
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);
}
}
}
Ensure the bundle is active, and check OSGi console at /system/console/components
to confirm your workflow step is registered and active.
Official Adobe Documentation:
Replicator API:
The Replicator
interface is part of AEM's API for content replication. You can find its documentation here:
Replicator Interface - AEM SDK API
Implementing Custom Workflow Process Steps:
For guidance on creating custom workflow steps, refer to Adobe's tutorial:
Implementing Custom Process Step | Adobe Experience Manager
Hope that helps!
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:
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);
}
}
}
Ensure the bundle is active, and check OSGi console at /system/console/components
to confirm your workflow step is registered and active.
Official Adobe Documentation:
Replicator API:
The Replicator
interface is part of AEM's API for content replication. You can find its documentation here:
Replicator Interface - AEM SDK API
Implementing Custom Workflow Process Steps:
For guidance on creating custom workflow steps, refer to Adobe's tutorial:
Implementing Custom Process Step | Adobe Experience Manager
Hope that helps!
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.
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
It seems your session is not having replication permission for the particular resource you are trying to replicate.
Views
Replies
Total Likes
Views
Likes
Replies
Views
Likes
Replies