Skip to main content

Workflows in AEM 04 - MetaDataMap

Sometimes we are faced with a situation where we need to pass data from one workflow step to the another. AEM's workflow API provides an easy way to achieve this using MetaDataMap.

Please note that only primitive data types like Integer, String etc. can be passed from one step to the another. If you have a requirement to pass non-primitive data then use the byte[] array.

Caution: If the data to be passed is too large, refrain passing InputStream. Instead, a better approach is to save the data in a JCR node in the step and retrieve it from the JCR node in a later step.

A MetaDataMap is the data structure which acts as a value map and allows users to set and get data among the steps.

Code example

Let's see a code example to demonstrate this concept. Here we will be using two process steps. In the first, we will be passing data and in the second, we will be retrieving it.

FirstProcessStep.java

package org.redquark.demo.core.workflows;

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=" + "First Process Step" })
public class FirstProcessStep implements WorkflowProcess {

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

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

  log.info("Executing first workflow process step");

  // Getting the metadata map
  MetaDataMap map = workItem.getWorkflow().getWorkflowData().getMetaDataMap();

  // Putting some values in the map
  map.put("Data", "Data from the first step");
 }

}

Here, you can see that we are getting the instance of MetaDataMap from the WorkItem and then we are saving a key-value pair in it. The key we are using here is "Data" and using this only, we will retrieve the values in the later steps.

SecondProcessStep.java

package org.redquark.demo.core.workflows;

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=" + "Second Process Step" })
public class SecondProcessStep implements WorkflowProcess {

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

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

  log.info("Executing second workflow process step");

  // Getting the metadata map
  MetaDataMap map = workItem.getWorkflow().getWorkflowData().getMetaDataMap();

  // Getting the data stored
  String data = (String) map.get("Data");

  log.info("Data from the first step: {}", data);
 }

}

In the second process step, we are getting the value from the same key "Data" and then we can use as per out requirement.

Conclusion

So in this post, we looked into how to work with MetaDataMap API and saw how can we pass data from one workflow step to another.

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

  1. Hi Anirudh,
    Could you please let me know a way to get data from Dialog participant step to the Process step.
    Thanks,
    Mahesh

    ReplyDelete
    Replies
    1. Hey Mahesh, when you say getting data from Dialog Participant Step to the Process step, are they part of same workflow model? If so, you can try MetaDataMap object to pass data among steps.

      Delete

Post a Comment

Popular posts from this blog

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 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 12: Creating your Custom OSGi configuration

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 We all know that AEM works on Apache Felix  which is an implementation of OSGi . OSGi provides a way to manage bundles and configurations. We saw one example of an OSGi configurati