Wednesday 7 August 2019

AEM - Retry Failed Workflow Item using Curl and Java

In AEM, workflows instances can be managed (complete, send back, terminate etc) either from AEM inbox or from workflow console.

AEM Inbox
http://localhost:4502/aem/inbox

AEM Workflow - Instance Page
http://localhost:4502/libs/cq/workflow/admin/console/content/instances.html

Sometimes there are cases when workflows need to manage programmatically. This can be done using various AEM workflow JAVA API, available within below packages.

Granite API

com.adobe.granite.workflow
com.adobe.granite.workflow.model
com.adobe.granite.workflow.exec

CQ API (old)

com.day.cq.workflow
com.day.cq.workflow.model
com.day.cq.workflow.exec

But None of the API have option to retry failed workflow.

AEM inbox


The alternative is to create a custom solution to retry workflow


Retry Workflow using Curl

WORKFLOW_ITEM_ENCODED="$(uriencode ${WORKFLOW_ITEM})"

curl -u admin:admin \
-H 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8' \
--data "cmd=advance" \
--data "item=${WORKFLOW_ITEM_ENCODED}" \
--data "route-${WORKFLOW_ITEM_ENCODED}=retry-current-step" \
--data "comment-${WORKFLOW_ITEM_ENCODED}=retry by curl"


where ${WORKFLOW_ITEM} variable contains the current workflow item path.
e.g.
/var/workflow/instances/server0/2019-07-28/request_copy_2/workItems/node1_var_workflow_instances_server0_2019-07-28_request_copy_2





Complete Curl code at
https://github.com/arunpatidar02/aem63app-repo/blob/master/java/workflow/retry-wf.sh


Retry Workflow using JAVA

In code, you may need to retry workflow from a servlet or any other services or components.
I created an OSGi component, which provides methods to retry a failed workflow item.
You need to add below code files available from GitHub
https://github.com/arunpatidar02/aem63app-repo/tree/master/java/workflow/com/acc/aem64/core/services


This WorkflowRetryService interface contains 2 methods

public void retryWorkflow(WorkflowSession wfSession, WorkItem workflowItem, Map<String, String> param);

public void retryWorkflow(String workflowItemPath, Map<String, String> param);

Where,
wfSession: WorkflowSession from the current session
workflowItemPath: Current item path as shown in the above screenshot
workflowItem: Current workflow item of the above path
param: Map, contains workflow item's metadata properties in key-value pair like comment etc.


The second method uses the sub-service session, so make sure service user has rights to perform read/write and other operation in the repository.


Example Servlet

This servlet is used to retry workflow using Java

String workflowItemPath = "/var/workflow/instances/server0/2019-07-28/request_copy_2/workItems/node1_var_workflow_instances_server0_2019-07-28_request_copy_2";
Map<String, String> params = new HashMap<String, String>();
params.put("comment", "comment - retrying");
WorkflowSession wfSession = (WorkflowSession)req.getResourceResolver().adaptTo(WorkflowSession.class);
WorkItem workflowItem;
try {
  workflowItem = wfSession.getWorkItem(workflowItemPath);
  wrs.retryWorkflow(wfSession, workflowItem, params);
//  wrs.retryWorkflow(workflowItemPath, params);

catch (WorkflowException e) {
  e.printStackTrace();
}

https://github.com/arunpatidar02/aem63app-repo/blob/master/java/WorkflowRetryDemoServlet.java


No comments:

Post a Comment

AEMaaCS - Core Component's Children Editor

If you are using core components, you should be familiar with the children-editor for Tabs, Accordion, and Carousel. These components are th...

About Me

My photo
https://www.linkedin.com/in/arunpatidar26/ https://experienceleaguecommunities.adobe.com/t5/user/viewprofilepage/user-id/6786635 https://community.adobe.com/t5/user/viewprofilepage/user-id/12372253 https://forums.adobe.com/people/Arun+Patidar