Hi Team,
We have a custom process step, where based on logic we are starting the workflow/terminating the workflow.
We are terminating the workflow by using the below line.
wfsession.terminateWorkflow(item.getWorkflow());
This does terminate the workflow and safely completes whatever we need, but it throws an error in the backend
java.lang.IllegalStateException: Workflow is already finished. at com.adobe.granite.workflow.core.WorkflowSessionImpl.terminateWorkflow(WorkflowSessionImpl.java:546) [com.adobe.granite.workflow.core:2.0.240.CQ660-B0017] at com.adobe.granite.workflow.core.job.HandlerBase.executeProcess(HandlerBase.java:198) [com.adobe.granite.workflow.core:2.0.240.CQ660-B0017] at com.adobe.granite.workflow.core.job.JobHandler.process(JobHandler.java:271) [com.adobe.granite.workflow.core:2.0.240.CQ660-B0017] at org.apache.sling.event.impl.jobs.JobConsumerManager$JobConsumerWrapper.process(JobConsumerManager.java:502) [org.apache.sling.event:4.2.24] at org.apache.sling.event.impl.jobs.queues.JobQueueImpl.startJob(JobQueueImpl.java:351) [org.apache.sling.event:4.2.24] at org.apache.sling.event.impl.jobs.queues.JobQueueImpl.access$100(JobQueueImpl.java:60) [org.apache.sling.event:4.2.24] at org.apache.sling.event.impl.jobs.queues.JobQueueImpl$1.run(JobQueueImpl.java:287) [org.apache.sling.event:4.2.24] at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) at java.base/java.lang.Thread.run(Thread.java:829)
This seems like an product error. Can some please comment how to get around this?
Solved! Go to Solution.
Views
Replies
Total Likes
@vishwanath881 You can try checking the state of the workflow using item.getWorkflow().getState(). If this returns anything else than RUNNING, you can avoid calling the terminateWorkflow method.
@vishwanath881 You can try checking the state of the workflow using item.getWorkflow().getState(). If this returns anything else than RUNNING, you can avoid calling the terminateWorkflow method.
The error occurs because you’re attempting to terminate a workflow that is already in a finished state (e.g., COMPLETED, ABORTED, or SUSPENDED). To avoid this:
import com.adobe.granite.workflow.Workflow;
import com.adobe.granite.workflow.exec.WorkflowState;
Workflow workflow = item.getWorkflow();
if (workflow.getState() == WorkflowState.RUNNING) {
wfsession.terminateWorkflow(workflow);
}
try {
if (workflow.getState() == WorkflowState.RUNNING) {
wfsession.terminateWorkflow(workflow);
}
} catch (IllegalStateException e) {
// Log and ignore if the workflow is already finished
logger.warn("Workflow {} is already terminated/completed.", workflow.getId());
}
@vishwanath881 Did you find the suggestions helpful? Please let us know if you need more information. If a response worked, kindly mark it as correct for posterity; alternatively, if you found a solution yourself, we’d appreciate it if you could share it with the community. Thank you!
Views
Replies
Total Likes
Views
Likes
Replies
Views
Likes
Replies
Views
Likes
Replies
Views
Likes
Replies