Expand my Community achievements bar.

Workflow to be triggered based on xpath query results aem6.5

Avatar

Level 8

Hello,

 

In the cronjob , based on the xpath query results .I will need to triggered workflow

 

Could you please provide inputs with any samples as how could be this be achieved.

 

Regards,

Srinivas

8 Replies

Avatar

Community Advisor

This should work in your case

try {
Map<String, String> map = new HashMap<>();
//update query as per requirement
map.put("path", "path");
map.put("type", "dam:Asset");
QueryBuilder queryBuilder = resolver.adaptTo(QueryBuilder.class);
Session session = resolver.adaptTo(Session.class);
final WorkflowSession workflowSession = resolver.adaptTo(WorkflowSession.class);
Query query = queryBuilder.createQuery(PredicateGroup.create(map), session); SearchResult searchResult = query.getResult(); for (Hit hit : searchResult.getHits()) { finalString payloadPath = hit.getPath(); final String model = "/var/workflow/models/metadata_map_example"; final WorkflowModel workflowModel = workflowSession.getModel(model); final WorkflowData workflowData = workflowSession.newWorkflowData("JCR_PATH", payloadPath); // Start the workflow! workflowSession.startWorkflow(workflowModel, workflowData, workflowMetadata); log.info("Workflow: {} started", model); } } catch (WorkflowException | IOException e) { log.error(e.getMessage(), e); }

Links :

 

Avatar

Level 8

Hello @Sachin_Arora_ 

It is throwing java.lang.IllegalStateException: Resource resolver is already closed.

In the  workflows methods when they  are getting triggered .How could I resolve this??

 

 try {
           for (Hit hit : searchResult.getHits()) {
           }
} catch (WorkflowException | IOException e) {
              log.error(e.getMessage(), e);

} finally{
       if(resolver!=null){
            resolver.close();
        }
}

Avatar

Level 8

@Sachin_Arora_ 

Thanks for the quick  response , below is code I am performing .

 

While the workflows are getting executed it throws the Exception

1>

I noticed that on removing  the finally from  WorkflowTriggerCronJob ,then it does not throw 

throwing java.lang.IllegalStateException: Resource resolver is already closed.

 

2>Is it correct to remove the finally from WorkflowTriggerCronJob

3> If there is any workflow does not execute successfully and throws an exception can I capture the workflow  path In array list , I tried doing that in WorkflowTriggerCronJob, but it completes the cronjob without waiting for workflows to complete the execution.

 

 

public class WorkflowTriggerCronJob implements Runnable {

@Reference
private ResourceResolverFactory resolverFactory;

try {
Map<String, Object> subService = new HashMap<>();
subService.put(ResourceResolverFactory.SUBSERVICE,"WRITE");
resourceResolver = resolverFactory.getServiceResourceResolver(subService);
final WorkflowSession workflowSession=resourceResolver.adaptTo(WorkflowSession.class);


for (Hit hit : searchResult.getHits()) {
finalString payloadPath = hit.getPath();
final String model = "/var/workflow/models/metadata_map_example";
final WorkflowModel workflowModel = workflowSession.getModel(model);
final WorkflowData workflowData = workflowSession.newWorkflowData("JCR_PATH", payloadPath);
// Start the workflow!
workflowSession.startWorkflow(workflowModel, workflowData, workflowMetadata);
log.info("Workflow: {} started", model);
}

......some operations like modifying a asset metadata node... and saving it

}catch (Exception runException) {
log.error("An exception occurred while executing workflow process trigger cronjob."
, runException);
} finally {
if (session != null && session.isLive()) {
session.logout();
}

if (resourceResolver != null && resourceResolver.isLive()) {
log.debug("inside final resourceResolver");
resourceResolver.close();
}
}
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


public class ObjWorkflowProcess extends Util implements WorkflowProcess, Serializable {

 

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

try {

Map<String, Object> subService = new HashMap<>();
subService.put(ResourceResolverFactory.SUBSERVICE,"WRITE");
resourceResolver = resourceResolverFactory.getServiceResourceResolver(subService);

// get session and workflow-data
session = resourceResolver.adaptTo(Session.class);

......some operations like modifying a asset metadata node... and saving it
}catch (Exception runException) {
log.error("An exception occurred while executing  workflow process trigger cronjob."
, runException);
} finally {
if (session != null && session.isLive()) {
session.logout();
}

if (resourceResolver != null && resourceResolver.isLive()) {
log.debug("inside final resourceResolver");
resourceResolver.close();
}
}
}

}

 

 

Avatar

Level 8

@Sachin_Arora_ 

is it possible to provide inputs for the below query I had??

3> If there is any workflow does not execute successfully and throws an exception can I capture the workflow  path In array list , I tried doing that in WorkflowTriggerCronJob, but it completes the cronjob without waiting for workflows to complete the execution.

Avatar

Community Advisor

When you start the workflow using workflowSession, it starts the workflow in a different thread. So your Scheduler wont know whether it will be getting failed or not. You should refactor your workflow logic/process to avoid getting failed. There is a thread based approach to validate the status of workflow, but I wont recommend using that.

 

Also you can use a property as an identifier which can be added on payload once workflow is completed successfully. If that property does not exist on the payload, it means your scheduler should cover that in next run else you can use Sling Jobs which re-runs the jobs in case of an failure.

Avatar

Community Advisor

Execute querybuilder api to fetch results in scheduler. Refer below code and modify as per your requirements

Map<String, String> queryMap = new HashMap<>();
queryMap.put("path", "queryPath..");
 queryMap.put("type", "cq:Page");
//add query criteria in queryMap
 QueryBuilder queryBuilder = resourceResolver.adaptTo(QueryBuilder.class);
Session session = resourceResolver.adaptTo(Session.class);
Query query = queryBuilder.createQuery(PredicateGroup.create(queryMap), session);
SearchResult result = query.getResult();
List<Hit> hits = result.getHits();
for (Hit hit : hits) {
try{
finalString payloadPath = hit.getPath();
final ResourceResolver resolver = request.getResourceResolver();
final WorkflowSession workflowSession = resolver.adaptTo(WorkflowSession.class);
String model = "/var/workflow/models/yourworkflowModel";
final WorkflowModel workflowModel = workflowSession.getModel(model);
		final WorkflowData workflowData = workflowSession.newWorkflowData("JCR_PATH", payloadPath);
workflowSession.startWorkflow(workflowModel, workflowData, workflowMetadata);
}
catch (WorkflowException | IOException e) {
   log.error(e.getMessage(), e);
  }

Refer https://aem.redquark.org/2019/12/workflows-in-aem-05-trigger-workflow.html

 

Avatar

Level 8

Hello ,

 

Could any help me on this.

 

In the xpath query results after triggering the workflows based on asset path , If there is any workflow does not execute successfully and throws an exception can I capture the workflow  path In array list in the cronjob , I tried doing , but it completes the cronjob without waiting for workflows to complete the execution.

 

How to solve this

 

Regards,

Srinivas