{"id":4940,"date":"2018-08-20T12:43:48","date_gmt":"2018-08-20T07:13:48","guid":{"rendered":"\/?p=4940"},"modified":"2020-08-17T11:44:57","modified_gmt":"2020-08-17T06:14:57","slug":"triggering-a-workflow-using-event-listeners-in-aem","status":"publish","type":"post","link":"https:\/\/www.argildx.us\/technology\/triggering-a-workflow-using-event-listeners-in-aem\/","title":{"rendered":"How to Trigger a Workflow in AEM using Event Listeners"},"content":{"rendered":"
We can trigger a workflow in AEM 6.2 when a DAM Asset is created, modified, or deleted within a given path. In this article, we will explore triggering workflows from our code based on events in the JCR.<\/p>\n
Suppose you have a workflow that creates custom renditions of assets in addition to the default AEM renditions, when the asset is under \u201c\/content\/dam\/ProjectName\/images\/\u201d. You would have set up two workflow launchers for triggering this workflow: one with event type as \u201cNode Create\u201d and one with \u201cNode Modified\u201d. We can also achieve the same functionality through our code, without touching the GUI.<\/p>\n
When assets are moved into a certain folder structure in DAM, trigger a workflow that creates a 100px X 100px thumbnail of our image.<\/p>\n
Fig 1: Before Moving the asset, no custom thumbnail.\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 Fig 2: Desired result after moving the asset, the new\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0thumbnail.<\/p>\n
The intuitive thought is that when an asset is moved, a new node is created in the new location and the old one is deleted. However, experience shows that AEM does not create a new node in the destination folder on Node Move. We know this because the \u2018jcr:Created\u2019 property does not change. AEM does not even change the last modified date.<\/p>\n
Fig 3: Creation Timestamp Before Moving the Asset.\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 Fig 4: Creation Timestamp is the same after moving.<\/p>\n
Fig 5: Modification Timestamp Before Moving the Asset.\u00a0 \u00a0 \u00a0 Fig 6: Modification Timestamp is the same after moving.<\/p>\n
On copying the asset, a new version of the same is created. This triggers the Node Creation launcher.<\/p>\n
<\/p>\n
<\/p>\n<\/div>\n
Fig. 7: No versions before copying the asset.\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0Fig. 8: Version created after copy-pasting the asset.<\/p>\n
Event Listeners<\/strong><\/p>\n AEM supports observation, which enables us to receive notifications of persistent changes to the workspace. A persisted change to the workspace is represented by a set of one or more events. Each event reports a single simple change to the structure of the persistent workspace in terms of an item added, changed, moved or removed. There are thus 7 possible events at the JCR level, viz:<\/p>\n We connect with the observation mechanism by registering an event listener with the workspace. An event listener is a class implementing the EventListener interface, that responds to the stream of events to which it has been subscribed. An event listener is added to a workspace with:<\/p>\n (A detailed explanation of each parameter is given with the code example in the package as well as at the end of this article) As defined by the EventListener interface, listener must provide an implementation of the onEvent method:<\/p>\n When an event occurs that falls within the scope of the listener, the repository calls the onEvent method invoking our logic which processes\/responds to the event. In our case, we will register an event listener to listen for \u201cNode Moved\u201d events under \u201c\/content\/dam\/images\u201d so that when an asset is moved to that folder, our workflow can be triggered.<\/p>\n When the component is activated, the activate(\u2026) method is called. It contains a call to ObservationManager.addEventListener(\u2026) for registering the event listener. The deactivate(\u2026) method contains logic for deregistering the event listener, and is triggered when the bundle is being stopped.<\/p>\n When the relevant event occurs, the onEvent(\u2026) method is called, which contains logic for processing the event. In our case, we trigger a workflow in AEM.<\/p>\n The following is the relevant code from ThumbnailNodeMovedListener.java:<\/strong><\/p>\n Download <\/a> this code (including the workflow):<\/p>\n Build it using<\/p>\n N.B: Creating a workflow is not part of this tutorial, and therefore a ready workflow has been provided in the code package. However, if you want to learn how to create custom workflows, here is an excellent resource.<\/a><\/p>\n Adobe Consulting Services. (2018, March 20). acs-aem-samples\/SampleJcrEventListener.java at master \u00b7 Adobe-Consulting-Services\/acs-aem-samples. Retrieved from Github: https:\/\/github.com\/Adobe-Consulting-Services\/acs-aem-samples\/blob\/master\/bundle\/src\/main\/java\/com\/adobe\/acs\/samples\/events\/impl\/SampleJcrEventListener.java<\/a><\/p>\n Day Software AG. (2018, March 20). JCR 2.0: 12 Observation (Content Repository for Java Technology API v2.0). Retrieved from Adobe Docs: https:\/\/docs.adobe.com\/docs\/en\/spec\/jcr\/2.0\/12_Observation.html<\/a><\/p>\n FAQs<\/strong><\/p>\n Trigger a Workflow in AEM We can trigger a workflow in AEM 6.2 when a DAM Asset is created, modified, or deleted within a given path. In this article, we will explore triggering workflows from our code based on events in the JCR. Suppose you have a workflow that creates custom renditions of assets in … Read more<\/a><\/p>\n","protected":false},"author":29,"featured_media":6662,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","content-type":"","footnotes":""},"categories":[66],"tags":[81,83,67,69,32,34],"yst_prominent_words":[1084,1086,1088,1803,1085,1081,1806,1843,1844,1804,1082,1800,1802,1805,1083,1811,1801,1700,1845,1087],"acf":[],"yoast_head":"\n\n
void ObservationManager. \r\naddEventListener(EventListener listener, \r\nint eventTypes, \r\nString absPath,\r\nboolean isDeep, \r\nString[] uuid, \r\nString[] nodeTypeName, \r\nboolean noLocal)<\/pre>\n
void EventListener.onEvent(EventIterator events)<\/pre>\n
Implementation<\/strong><\/h5>\n
\r\nprotected void activate(ComponentContext ctx) { \r\n\r\ntry { \r\n. \r\n. \r\n. \r\n\/\/ Building the parameters for adding the event listener \r\n\r\n\r\n\/\/ Whether the subfolders of the given path should also be watched \r\n\r\nboolean isDeep = true; \r\n\r\n\/\/ Only events whose associated node has one of the UUIDs in this list will be \r\n\r\n\/\/ received. If this parameter is null then no UUID-related restriction is \r\n\r\n\/\/ placed on events received. \r\n\r\nString[] uuid = null; \r\n\r\n\/\/ Only events whose associated node has one of the node types (or a subtype of \r\n\r\n\/\/ one of the node types) in this list will be received. If this parameter is \r\n\r\n\/\/ null then no node type-related restriction is placed on events received. \r\n\r\nString[] nodeTypeName = null; \r\n \r\n\r\n\/\/ If noLocal is true, then events generated by the session through which the \r\n\r\n\/\/ listener was registered are ignored. Otherwise, they are not ignored. \r\n\r\nboolean noLocal = true; \r\n\r\n\/\/ Registering the event listener \r\n\r\nobservationManager.addEventListener(this, Event.NODE_MOVED, ASSET_UPDATE_PATH, isDeep, uuid, nodeTypeName, \r\n\r\nnoLocal); \r\n\r\n} \r\n\r\n} \r\n\r\n \r\n\r\npublic void onEvent(EventIterator itr) { \r\n\r\nwhile (itr.hasNext()) { \r\n\r\nEvent currentEvent = itr.nextEvent(); \r\n\r\ntry { \r\n. \r\n. \r\n. \r\n\r\n\/\/ Create a workflow session \r\n\r\nWorkflowSession wfSession = workflowService.getWorkflowSession(localSession); \r\n\r\n\/\/ Get the workflow model \r\n\r\nWorkflowModel wfModel = wfSession.getModel(THUMBNAIL_WORKFLOW_PATH); \r\n\r\n\r\n\/\/ Get the Workflow data. The first parameter in the newWorkflowData method is \r\n\r\n\/\/ the payloadType. Just a fancy name to let it know what type of workflow it is \r\n\r\n\/\/ working with. \r\n\r\nWorkflowData wfData = wfSession.newWorkflowData(JCR_PATH, currentEvent.getPath()+ORIGINAL_RENDITION_RELATIVE_PATH); \r\n\r\n\/\/ Start the Workflow. \r\n\r\nwfSession.startWorkflow(wfModel, wfData); \r\n\r\n} \r\n. \r\n. \r\n.<\/pre>\n
mvn clean install -PautoInstallPackage<\/pre>\n
References<\/strong><\/h5>\n
\n
\nAn\u00a0event listener<\/b> is a procedure or function in a program that waits for an event<\/b> to occur.<\/li>\n
\nSome simple examples of an event<\/b> are users clicking or moving the mouse, pressing a key on the keyboard, or network activity.<\/li>\n
\nA workflow launcher<\/strong> is a standard way to invoke or trigger a workflow based on conditions. The workflow launcher monitors changes in the content repository to launch workflows dependent on the location and resource type of the changed node.<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"