Skip to main content

Workflows in AEM 02 - Process Step

Hi everyone, it's been a long time since I wrote the first part of this blog. In the previous part, we discussed the fundamentals of workflows, their types and applications.

Today, we are going to discuss the most widely used Workflow step - the process step. This is generally used when we want our application to execute a certain logic.

It executes an ECMA script or an OSGi service to perform automatic processing. A process can be implemented using the following steps -
  1. Create an OSGi service implementing the interface com.adobe.granite.workflow.exec.WorkflowProcess.
  2. Set the property process.label. This is the String value by which our workflow needs to be listed.
  3. Implement the execute(WorkItem, WorkflowSession, MetaDataMap) method with the implementation code.

The execute() method has three parameters -
  1. WorkItem - It is the unit that is passed through a Workflow instance of a WorkflowModel. It contains the WorkflowData. The instances act on and a reference to the WorkflowNode that describes the underlying workflow step.
  2. WorkflowSession - This class provides all functionality (depending on the users' rights) for managing WorkflowModels, Workflow instances and their execution.
  3. MetaDataMap - A value map for generic access to meta data values.
Following is the sample implementation of a process step - 
package org.redquark.demo.core.workflows;

import java.util.Arrays;

import org.apache.commons.lang3.StringUtils;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.adobe.granite.workflow.WorkflowException;
import com.adobe.granite.workflow.WorkflowSession;
import com.adobe.granite.workflow.exec.WorkItem;
import com.adobe.granite.workflow.exec.WorkflowProcess;
import com.adobe.granite.workflow.metadata.MetaDataMap;

/**
 * @author Anirudh Sharma
 */
@Component(service = WorkflowProcess.class, property = { "process.label=" + "Process Step Example" })
public class ProcessStepExample implements WorkflowProcess {

 private final Logger log = LoggerFactory.getLogger(this.getClass());

 @Override
 public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaDataMap)
   throws WorkflowException {

  // Getting payload from Workflow - workItem -> workflowData -> payload
  String payloadType = workItem.getWorkflowData().getPayloadType();

  // Check type of payload; there are two - JCR_PATH and JCR_UUID
  if (StringUtils.equals(payloadType, "JCR_PATH")) {
   log.info("Payload type: {}", payloadType);
   // Get the JCR path from the payload
   String path = workItem.getWorkflowData().getPayload().toString();
   log.info("Payload path: {}", path);
  }

  // Get workflow process arguments
  String[] processArguments = metaDataMap.get("PROCESS_ARGS", "Default").split(",");
  log.info("Process args: {}", Arrays.toString(processArguments));
 }

}
Here, we are implementing WorkflowProcess interface and our business logic is written in the execute() method. Here we are getting the type of payload from the WorkflowData object and then also getting the process arguments that a user might pass while using this process step.

Please make note of the process.label property that defines the identifier by which this process step will be listed among many process steps (this is explained in the below section).

Configuring the workflow

Workflow models
  • Now click on "Create Model" and enter the workflow details and "Done".
Create workflow model
  • Select workflow model created and click on "Edit"
Edit workflow model
  • Click on drag components here and select process step from the list. Before that, delete step 1 which is created automatically.
Select process step
  • Edit the process step using the following configuration. Note that we selected the process which is similar to the value we have given in the process.label property.
Configure process step
  • After saving the dialog, click on "Sync" in top right corner to save the workflow. Thus our workflow model is ready. Now, we can run it for any operation we want. The simplest example is to run this workflow model for a page.
  • When you run it, you will find the following messages in the logs -
2019-09-24 22:40:35.268 INFO [org.redquark.demo.core.workflows.ProcessStepExample] Payload type: JCR_PATH
2019-09-24 22:40:35.268 INFO [org.redquark.demo.core.workflows.ProcessStepExample] Payload path: /content/we-retail/language-masters/en/men
2019-09-24 22:40:35.269 INFO [org.redquark.demo.core.workflows.ProcessStepExample] Process args: [a=1, b=2, c=3]

Conclusion

In this post, we looked into creating a custom workflow process steps and how to configure it. You can find the code to this in my GitHub repository.

I hope you enjoyed this post. Your suggestions and feedback are always welcome. Feel free to befriend me on FacebookTwitter or Linked In or say Hi by email.

Comments

Popular posts from this blog

Day 00: AEM Developer Series

image source:  https://solutionpartners.adobe.com/ Hello everyone! Welcome to this AEM development series. We can all see the revolution of Digital Marketing today. Companies are dying to be a part of it and they have made this a war for the Digital Marketing tools. Adobe is way ahead in this war and has gained a lot of market capture. They are leaders in the Digital Marketing platforms since 2014-15. One of the flagship product in Adobe's Digital Marketing suite is Adobe Experience Manager (AEM) . Since AEM is in huge demand, the people who know how to develop on AEM are also in huge demand. But developing on AEM is not easy as it is made up of various open-source technologies such as Apache Felix (OSGi), Apache Sling, Apache Oak and Adobe's own technologies like Granite, HTL etc. Learning all these technologies in conjunction can sometimes become confusing and frustrating 😫. When I first started learning AEM in 2016, I was dumbfounded to see there is

Day 05: Working with Sling Servlets in AEM

AEM Developer Series Day 00: AEM Developer Series Day 01: Introduction to AEM Day 02: AEM Architecture Day 03: Setting up AEM Development Environment Day 04: Developing First OSGi Bundle Day 05: Working with Sling Servlets in AEM Day 06: Playing with Sling Post Servlet Day 07: Creating your first component in AEM Day 08: Dueling with JavaScript Use API Day 09: Dueling with Java User API Day 10: Getting to know Sling Models Day 11: Client Libraries in Action Day 12: Creating your custom OSGi Configuration Day 13: Schedulers in AEM Day 14: Eventing in AEM Day 15: Custom Workflows in AEM Day 16: Creating JMX Beans in AEM Day 17: Working with QueryBuilder API Day 18: Working with Granite Datasources in AEM Day 19: Replication API in Action Day 20: Working with Users and Groups in AEM A Servlet is a class used to extend the capabilities of servers that host applications accessed by means of a request-response programming model. For such applications, Servlet tec

Day 03: Setting up AEM Development Environment

AEM Developer Series Day 00: AEM Developer Series Day 01: Introduction to AEM Day 02: AEM Architecture Day 03: Setting up AEM Development Environment Day 04: Developing First OSGi Bundle Day 05: Working with Sling Servlets in AEM Day 06: Playing with Sling Post Servlet Day 07: Creating your first component in AEM Day 08: Dueling with JavaScript Use API Day 09: Dueling with Java User API Day 10: Getting to know Sling Models Day 11: Client Libraries in Action Day 12: Creating your custom OSGi Configuration Day 13: Schedulers in AEM Day 14: Eventing in AEM Day 15: Custom Workflows in AEM Day 16: Creating JMX Beans in AEM Day 17: Working with QueryBuilder API Day 18: Working with Granite Datasources in AEM Day 19: Replication API in Action Day 20: Working with Users and Groups in AEM Welcome to the third day of learning AEM development. In previous posts, we discussed the AEM architecture and in this post, we are going to see how to set up an AEM development e