Expand my Community achievements bar.

Radically easy to access on brand approved content for distribution and omnichannel performant delivery. AEM Assets Content Hub and Dynamic Media with OpenAPI capabilities is now GA.
SOLVED

AEM 6.5 how to get workflow acting node by java code?

Avatar

Level 4

I am new to AEM, how can I get workflow current node?

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Level 10

Hi @Johann_Lu ,

To get the current acting node of a workflow in AEM 6.5 using Java code, you can use the Workflow API provided by AEM. Here is a step-by-step guide on how to achieve this:

Step-by-Step Guide

  1. Add Dependencies: Ensure you have the necessary dependencies in your Maven project. Add the following dependencies to your pom.xml:

 

<dependency>
    <groupId>com.adobe.granite</groupId>
    <artifactId>com.adobe.granite.workflow.api</artifactId>
    <version>2.0.28</version>
</dependency>
<dependency>
    <groupId>org.apache.sling</groupId>
    <artifactId>org.apache.sling.api</artifactId>
    <version>2.20.0</version>
</dependency>

 

Create a Java Class to Get the Current Workflow Node: Create a Java class that interacts with the Workflow API to get the current node of a workflow.

 

package com.example.aem.core.workflow;

import com.adobe.granite.workflow.WorkflowSession;
import com.adobe.granite.workflow.exec.Route;
import com.adobe.granite.workflow.exec.WorkItem;
import com.adobe.granite.workflow.exec.WorkflowData;
import com.adobe.granite.workflow.exec.WorkflowInstance;
import com.adobe.granite.workflow.exec.WorkflowModel;
import com.adobe.granite.workflow.exec.WorkflowNode;
import com.adobe.granite.workflow.model.WorkflowNodeType;
import org.apache.sling.api.resource.ResourceResolver;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.jcr.Session;

@Component(service = WorkflowNodeService.class)
public class WorkflowNodeService {

    private static final Logger LOG = LoggerFactory.getLogger(WorkflowNodeService.class);

    @Reference
    private com.adobe.granite.workflow.WorkflowSession workflowSession;

    public WorkflowNode getCurrentWorkflowNode(String workflowInstanceId, ResourceResolver resourceResolver) {
        try {
            Session session = resourceResolver.adaptTo(Session.class);
            WorkflowSession wfSession = workflowSession.adaptTo(WorkflowSession.class);
            WorkflowInstance workflowInstance = wfSession.getWorkflow(workflowInstanceId);

            for (WorkItem workItem : workflowInstance.getWorkItems()) {
                WorkflowNode node = workItem.getNode();
                if (node.getType().equals(WorkflowNodeType.PARTICIPANT)) {
                    return node;
                }
            }
        } catch (Exception e) {
            LOG.error("Error getting current workflow node: ", e);
        }
        return null;
    }
}

 

Use the Service in Your Code: Inject the WorkflowNodeService into your servlet or another OSGi component to use it.

 

@Component(service = MyWorkflowServlet.class,
           immediate = true,
           property = {
                   "sling.servlet.paths=/bin/myworkflow",
                   "sling.servlet.methods=GET"
           })
public class MyWorkflowServlet extends SlingAllMethodsServlet {

    @Reference
    private WorkflowNodeService workflowNodeService;

    @Override
    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
            throws ServletException, IOException {
        String workflowInstanceId = request.getParameter("workflowInstanceId");
        ResourceResolver resourceResolver = request.getResourceResolver();
        WorkflowNode currentNode = workflowNodeService.getCurrentWorkflowNode(workflowInstanceId, resourceResolver);

        if (currentNode != null) {
            response.getWriter().write("Current Node: " + currentNode.getTitle());
        } else {
            response.getWriter().write("Workflow node not found.");
        }
    }
}

 

Explanation:

  1. WorkflowNodeService Class:

    • getCurrentWorkflowNode: This method takes the workflow instance ID and a ResourceResolver as parameters. It retrieves the current node of the workflow instance.
    • workflowSession: This is a reference to the Workflow Session, which is used to interact with the workflow engine.
  2. MyWorkflowServlet Class:

    • doGet: This method handles GET requests. It retrieves the workflow instance ID from the request, uses the WorkflowNodeService to get the current node, and writes the node's title to the response.

Notes:

  • Error Handling: Proper error handling is implemented to catch any exceptions that might occur during the workflow node retrieval.
  • Service Registration: The WorkflowNodeService is registered as an OSGi service, making it available for injection into other components.

By following these steps, you can retrieve the current node of a workflow instance in AEM using Java code. This example demonstrates how to set up the necessary dependencies, create a service to interact with the Workflow API, and use this service in a servlet to respond with the current workflow node information.

 

View solution in original post

5 Replies

Avatar

Community Advisor

@Johann_Lu 

 

The details of current WorkItem are in workItem object of execute method of Workflow Process Step.

    public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaDataMap) throws WorkflowException {
        ResourceResolver resourceResolver = null;

 

If you are looking for current payload(Page/Asset etc), on which workflow is running, the use

String payloadPath = (String) workItem.getWorkflowData().getPayload();

 


Aanchal Sikka

Avatar

Level 4

No, I need the current node through workflowID, is it possible?

Avatar

Level 2

Could you please provide more details/screenshot for better understanding?

Is it, 

  • Workflow step node?
  • payload resource/node?

Avatar

Correct answer by
Level 10

Hi @Johann_Lu ,

To get the current acting node of a workflow in AEM 6.5 using Java code, you can use the Workflow API provided by AEM. Here is a step-by-step guide on how to achieve this:

Step-by-Step Guide

  1. Add Dependencies: Ensure you have the necessary dependencies in your Maven project. Add the following dependencies to your pom.xml:

 

<dependency>
    <groupId>com.adobe.granite</groupId>
    <artifactId>com.adobe.granite.workflow.api</artifactId>
    <version>2.0.28</version>
</dependency>
<dependency>
    <groupId>org.apache.sling</groupId>
    <artifactId>org.apache.sling.api</artifactId>
    <version>2.20.0</version>
</dependency>

 

Create a Java Class to Get the Current Workflow Node: Create a Java class that interacts with the Workflow API to get the current node of a workflow.

 

package com.example.aem.core.workflow;

import com.adobe.granite.workflow.WorkflowSession;
import com.adobe.granite.workflow.exec.Route;
import com.adobe.granite.workflow.exec.WorkItem;
import com.adobe.granite.workflow.exec.WorkflowData;
import com.adobe.granite.workflow.exec.WorkflowInstance;
import com.adobe.granite.workflow.exec.WorkflowModel;
import com.adobe.granite.workflow.exec.WorkflowNode;
import com.adobe.granite.workflow.model.WorkflowNodeType;
import org.apache.sling.api.resource.ResourceResolver;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.jcr.Session;

@Component(service = WorkflowNodeService.class)
public class WorkflowNodeService {

    private static final Logger LOG = LoggerFactory.getLogger(WorkflowNodeService.class);

    @Reference
    private com.adobe.granite.workflow.WorkflowSession workflowSession;

    public WorkflowNode getCurrentWorkflowNode(String workflowInstanceId, ResourceResolver resourceResolver) {
        try {
            Session session = resourceResolver.adaptTo(Session.class);
            WorkflowSession wfSession = workflowSession.adaptTo(WorkflowSession.class);
            WorkflowInstance workflowInstance = wfSession.getWorkflow(workflowInstanceId);

            for (WorkItem workItem : workflowInstance.getWorkItems()) {
                WorkflowNode node = workItem.getNode();
                if (node.getType().equals(WorkflowNodeType.PARTICIPANT)) {
                    return node;
                }
            }
        } catch (Exception e) {
            LOG.error("Error getting current workflow node: ", e);
        }
        return null;
    }
}

 

Use the Service in Your Code: Inject the WorkflowNodeService into your servlet or another OSGi component to use it.

 

@Component(service = MyWorkflowServlet.class,
           immediate = true,
           property = {
                   "sling.servlet.paths=/bin/myworkflow",
                   "sling.servlet.methods=GET"
           })
public class MyWorkflowServlet extends SlingAllMethodsServlet {

    @Reference
    private WorkflowNodeService workflowNodeService;

    @Override
    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
            throws ServletException, IOException {
        String workflowInstanceId = request.getParameter("workflowInstanceId");
        ResourceResolver resourceResolver = request.getResourceResolver();
        WorkflowNode currentNode = workflowNodeService.getCurrentWorkflowNode(workflowInstanceId, resourceResolver);

        if (currentNode != null) {
            response.getWriter().write("Current Node: " + currentNode.getTitle());
        } else {
            response.getWriter().write("Workflow node not found.");
        }
    }
}

 

Explanation:

  1. WorkflowNodeService Class:

    • getCurrentWorkflowNode: This method takes the workflow instance ID and a ResourceResolver as parameters. It retrieves the current node of the workflow instance.
    • workflowSession: This is a reference to the Workflow Session, which is used to interact with the workflow engine.
  2. MyWorkflowServlet Class:

    • doGet: This method handles GET requests. It retrieves the workflow instance ID from the request, uses the WorkflowNodeService to get the current node, and writes the node's title to the response.

Notes:

  • Error Handling: Proper error handling is implemented to catch any exceptions that might occur during the workflow node retrieval.
  • Service Registration: The WorkflowNodeService is registered as an OSGi service, making it available for injection into other components.

By following these steps, you can retrieve the current node of a workflow instance in AEM using Java code. This example demonstrates how to set up the necessary dependencies, create a service to interact with the Workflow API, and use this service in a servlet to respond with the current workflow node information.