2 Replies Latest reply on Jul 19, 2013 9:57 AM by aditya p

    How to trigger an email when workflow instance fails??

    aditya p

      Hello,

       

      We have a requirement where in a mail should be triggered to the system administrator in case a workflow instance fails.

       

      I tried setting up a launcher (to trigger a Test Workflow that sends mail) with following configuration but it doesn't get triggered.

       

      Launcher_WF.jpg

      The same launcher on cq:workflow node modification under /etc/workflow/instances/....... triggers my Test workflow if I change the Launcher Glob Path to /etc/workflow(/.*)

       

      So, I have a couple of questions:

      • Is this feature is available out of the box in CQ 5.6?
      • If not, then how should this requirement be implemented?

       

      Thanks!!

        • 2. Re: How to trigger an email when workflow instance fails??
          aditya p Level 1

          CQ provides a way to listen to workflow events and lets us handle events specifically. We have to implement org.osgi.service.event.EventHandler for this purpose.

           

          Here is a sample implementation for reference:

           

          package com.mycompany.test;

           

          import javax.jcr.RepositoryException;

          import javax.jcr.Session;

           

          import org.apache.sling.jcr.api.SlingRepository;

          import org.osgi.service.event.Event;

          import org.osgi.service.event.EventHandler;

          import org.slf4j.Logger;

          import org.slf4j.LoggerFactory;

           

          import com.day.cq.workflow.WorkflowException;

          import com.day.cq.workflow.WorkflowService;

          import com.day.cq.workflow.WorkflowSession;

          import com.day.cq.workflow.event.WorkflowEvent;

          import com.day.cq.workflow.exec.WorkItem;

          import com.day.cq.workflow.exec.Workflow;

           

          /**

          * WorkflowEventCatcher is listening to workflow events

          * /

            @Component( immediate=true)

            @Service

            @Property( name="event.topics", value=com.day.cq.workflow.event.WorkflowEvent.EVENT_TOPIC )

           

            public class WorkflowEventCatcher implements EventHandler {

           

            private static final Logger log = LoggerFactory.getLogger(WorkflowEventCatcher.class

           

            @Reference

            private WorkflowService workflowService;

           

            @Reference

            private SlingRepository repository;

           

            public void handleEvent(Event event) {

                  log.debug("Received event of topic: " + event.getTopic());

                  String topic = event.getTopic();

                  WorkflowSession wfSession = getWorkflowSession();

                  try {

                      if (topic.equals(WorkflowEvent.EVENT_TOPIC)) {

                          Object eventType = event.getProperty(WorkflowEvent.EVENT_TYPE);

                          String instanceId =

                                  (String) event.getProperty(WorkflowEvent.WORKFLOW_INSTANCE_ID);

                          Workflow instance = null;

                          try {

                              instance = (instanceId != null) ? wfSession.getWorkflow(instanceId) : null;

                          } catch (WorkflowException we) {

                              log.warn("Unable to load workflow instance", we);

                          }

                          if (instance != null) {

                              // workflow instance events

                              if (eventType.equals(WorkflowEvent.WORKFLOW_STARTED_EVENT) ||

                                      eventType.equals(WorkflowEvent.WORKFLOW_RESUMED_EVENT) ||

                                      eventType.equals(WorkflowEvent.WORKFLOW_SUSPENDED_EVENT)) {

                                   // your code comes here...

                              } else if (

                                      eventType.equals(WorkflowEvent.WORKFLOW_ABORTED_EVENT) ||

                                      eventType.equals(WorkflowEvent.WORKFLOW_COMPLETED_EVENT)) {

                                   // your code comes here...

                              }

                              // workflow node event

                              if (eventType.equals(WorkflowEvent.NODE_TRANSITION_EVENT)) {

                                  WorkItem currentItem =

                                          (WorkItem) event.getProperty(WorkflowEvent.WORK_ITEM);

                                   // your code comes here...

                              }

                          }

                      }

                  } finally {

                        if (wfSession != null) {

                           wfSession.logout();

                       }

                  }

                  return true;

              }

           

              //----------< helper >-----------------------------------------------------

              private WorkflowSession getWorkflowSession() {

                  try {

                      Session adminSession = repository.loginAdministrative(null);

                      return workflowService.getWorkflowSession(adminSession);

                  } catch (RepositoryException re) {

                            log.error("RepositoryException: " + re);

                  }

                  return null;

              }

          }

           

           

          Thanks,

          Aditya