Custom workflow LockException issue
I have implemented a custom AEM Workflow Process Step in Java that updates a JCR node property in the workflow payload path. Here’s my code snippet:
@8220494(service = WorkflowProcess.class, property = { "process.label=Update Payload Deep" })
public class UpdatePayloadProcess implements WorkflowProcess {
@3214626
private ResourceResolverFactory resolverFactory;
@9944223
public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap args) throws WorkflowException {
String payloadPath = workItem.getWorkflowData().getPayload().toString();
Map<String, Object> serviceUserMap = Collections.singletonMap(ResourceResolverFactory.SUBSERVICE, "workflowServiceUser");
ResourceResolver resolver = null;
try {
resolver = resolverFactory.getServiceResourceResolver(serviceUserMap);
Session session = resolver.adaptTo(Session.class);
if (!session.nodeExists(payloadPath)) {
throw new WorkflowException("Payload path does not exist: " + payloadPath);
}
Node payloadNode = session.getNode(payloadPath);
// Lock the node before update to avoid conflicts in concurrent workflows
if (!payloadNode.isLocked()) {
session.getWorkspace().getLockManager().lock(payloadNode.getPath(), false, false, Long.MAX_VALUE, null);
}
payloadNode.setProperty("processedBy", "UpdatePayloadProcess");
session.save();
// Unlock the node after update
if (payloadNode.isLocked()) {
session.getWorkspace().getLockManager().unlock(payloadNode.getPath());
}
} catch (LoginException | RepositoryException e) {
throw new WorkflowException("Failed to update workflow payload", e);
} finally {
if (resolver != null && resolver.isLive()) {
resolver.close();
}
}
}
}
Despite implementing locking and proper service user access, sometimes the workflow fails with a LockException or concurrent update issues.
What could be the issue?