Expand my Community achievements bar.

Event handler is not working in aem cloud dev server

Avatar

Level 1

Hi, 

I am trying to start a workflow on the basis of asset creation event in dam. It is working perfectly fine in AEM cloud local server but post deployment it is not working , though when i am starting the workfloww from workflow model , workflow is working fine . i am attching the code here, what can be the issue. Please guide me on this. What can be the alternative.

 

@AnkurAhlawat @arunpatidar  @Erika_Antkowiak 

 

package com.disney.core.listeners;

import com.day.cq.dam.api.DamEvent;
import com.day.cq.workflow.WorkflowException;
import com.day.cq.workflow.WorkflowService;
import com.day.cq.workflow.WorkflowSession;
import com.day.cq.workflow.exec.WorkflowData;
import com.day.cq.workflow.model.WorkflowModel;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventConstants;
import org.osgi.service.event.EventHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.jcr.Session;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Component(

service = EventHandler.class,
property = {
EventConstants.EVENT_TOPIC + "=" + DamEvent.EVENT_TOPIC,
EventConstants.EVENT_FILTER + "=(type=ASSET_CREATED)"
},
immediate = true
)
public class AssetVersionListener implements EventHandler {
private static final String DISNEY_WORKFLOW_ADMIN = "disney-workflow-admin";

@Reference
private ResourceResolverFactory resourceResolverFactory;

private final Logger log = LoggerFactory.getLogger(AssetVersionListener.class);
@Reference
private WorkflowService workflowService;

@Override
public void handleEvent(Event event) {
log.info("Inside Event Handler");
try {
Map<String, Object> authInfo = new HashMap<>();
authInfo.put(ResourceResolverFactory.SUBSERVICE, DISNEY_WORKFLOW_ADMIN);
ResourceResolver resolver = resourceResolverFactory.getServiceResourceResolver(authInfo);
String assetPath = (String) event.getProperty("assetPath");
Resource assetResource = resolver.getResource(assetPath);
if (Objects.nonNull(assetResource)) {
if (fileNameMatcher(assetResource.getName())) {
WorkflowSession wfSession = workflowService.getWorkflowSession(resolver.adaptTo(Session.class));
if (Objects.nonNull(wfSession)) {
WorkflowModel wfModel = wfSession.getModel("/var/workflow/models/asset-version-notificaion");
if (Objects.nonNull(wfModel)) {
WorkflowData workflowData = wfSession.newWorkflowData("JCR_PATH", assetPath);
if (Objects.nonNull(workflowData)) {
wfSession.startWorkflow(wfModel, workflowData);
}
}
}
}
}
} catch (WorkflowException | LoginException e) {
log.error("error in handle event method", e);
}
}

// function to match pattern in file name
public static boolean fileNameMatcher(String fileName) {
Pattern pattern = Pattern.compile("^(.*?)(?:_v_|_)(\\d+)\\.(\\w+)$"); //any filename following the _v_# or _# pattern
Matcher matcher = pattern.matcher(fileName);
return matcher.matches();
}
}

Topics

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

4 Replies

Avatar

Community Advisor

Hi @ShikhaSo3 

 

Have you tried using Workflow launcher ? You can refer this article - https://www.techiearchive.com/configure-launcher-with-aem-workflow-aemaacs/ for how to configure workflow launchers.

 

Hope this helps!

 

Thanks

Narendra

Avatar

Level 4

Please try using Post processing workflows for AEM as a Cloud Service

https://experience-aem.blogspot.com/2021/06/aem-cloud-service-asset-processing-profile-post-processi...

 

Avatar

Administrator

@ShikhaSo3 Did you find the suggestions helpful? Please let us know if you need more information. If a response worked, kindly mark it as correct for posterity; alternatively, if you found a solution yourself, we’d appreciate it if you could share it with the community. Thank you!



Kautuk Sahni

Avatar

Level 7

Hi @ShikhaSo3 

 

AEMaaCS has optimized event handling, and DamEvent.EVENT_TOPIC may not behave consistently, switch to ResourceChangeListener for better AEMaaCS event handling also ensure the model exists at this path in Cloud.

try WorkflowModel wfModel = wfSession.getModel("/conf/global/settings/workflow/models/asset-version-notification");

 

The service user (disney-workflow-admin) might not have sufficient permissions in the Cloud environment, check the service user is mapped in the system/console/confMgr under Apache Sling Service User Mapper Service Amendment. 

 

last thing check assetPath is not null

 

Hope this helps!