Custom task listener in Java

cancel
Showing results for 
Search instead for 
Did you mean: 
anakin59490
Established Member II

Custom task listener in Java

Hi,

I have the simple following Workflow :

<extensionElements>
<activiti:taskListener event="create" class="org.alfresco.repo.workflow.activiti.tasklistener.ScriptTaskListener">
<activiti:field name="script">
<activiti:string><![CDATA[if (typeof bpm_workflowDueDate != 'undefined') task.setVariableLocal('bpm_dueDate', bpm_workflowDueDate);
if (typeof bpm_workflowPriority != 'undefined') task.priority = bpm_workflowPriority;

// logger.system.out("task.getVariable(reviewAssignee) onCreate : "+task.getVariable("reviewAssignee"));]]></activiti:string>
</activiti:field>
</activiti:taskListener>
<activiti:taskListener event="complete" class="com.mc.huissier.workflow.WorkFlowTaskListener"></activiti:taskListener>
</extensionElements>

My WorkFlowTaskListener java class :

public class WorkFlowTaskListener implements TaskListener {

private static Logger logger = LoggerFactory.getLogger(WorkFlowTaskListener.class);

@Autowired
private WorkFlowServices workflowServices;

@Autowired
private ServiceRegistry serviceRegistry;

@Override
public void notify(DelegateTask task) {
      DelegateExecution execution = task.getExecution();
      String reviewAction = task.getVariable("wfvd_reviewAction").toString();
      String nomService = task.getVariable("wfvd_nomService").toString();
      String fileName = task.getVariable("bpm_workflowDescription").toString();

      ActivitiScriptNode scriptNode =   (ActivitiScriptNode)execution.getVariable(WorkflowNotificationUtils.PROP_PACKAGE);
      logger.info("scriptNode OK !" + scriptNode);
      NodeRef packagenode = scriptNode.getNodeRef();
      logger.info("packagenode OK !" + packagenode);

      if(reviewAction.equals("CLA")) {
              logger.info("WorkFlowTaskListener : Classement du document");
              try {
                 String client = task.getVariable("wfvd_client").toString();
                 String typologie = task.getVariable("wfvd_typologie").toString();
                 String docDate = task.getVariable("wfvd_docDate").toString();
                 String mandataire = task.getVariable("wfvd_mandataire").toString();
                  logger.info("client: " + client);
                  logger.info("typologie: " + typologie);
                  logger.info("date: " + docDate);
                  logger.info("mandataire: " + mandataire);
                  // Mise à jour des métadonnées
                  workflowServices.documentUpdate(packagenode, client, mandataire, typologie, docDate);
                  
               } catch (Exception e) {
              logger.error("WorkFlowTaskListener - Erreur lors du classement : " + ExceptionUtils.getStackTrace(e));
        }
}

I get an issue when documentUpdate method is called :

=>     WorkFlowTaskListener - Erreur lors du classement : java.lang.NullPointerException
at com.mc.huissier.workflow.WorkFlowTaskListener.notify(WorkFlowTaskListener.java:78)

It's like if my workflowServices customed Class was unknown

WorFlowServices.java :

@Service
public class WorkFlowServices {
    private static final Logger logger = LoggerFactory.getLogger(WorkFlowServices.class);
    @Autowired
     private CoreServices coreServices;
     @Autowired
      private FolderServices folderServices;
     @Autowired
      private FileFolderService fileFolderService;
    @Autowired
     private NodeService nodeService;

public void documentUpdate(final NodeRef nodeDocumed, final String client, final String mandataire,
final String typeDoc, final String docDateStr) {

      AuthenticationUtil.runAs(new AuthenticationUtil.RunAsWork<Object>() {
      public Object doWork() throws Exception {

             logger.debug("WorkFlowServices - documentUpdate - mise à jour des metadonnées");

             SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
             Date docDate = formatter.parse(docDateStr);

             nodeService.setProperty(nodeDocumed, CoreServices.PROP_TYPOLOGIE, typeDoc);
             nodeService.setProperty(nodeDocumed, CoreServices.PROP_DOCDATE, docDate);
             nodeService.setProperty(nodeDocumed, CoreServices.PROP_CLIENT, client);
             nodeService.setProperty(nodeDocumed, CoreServices.PROP_MANDATAIRE, mandataire);

            return null;
        }
     }, AuthenticationUtil.getSystemUserName());

}

The setence : "WorkFlowServices - documentUpdate - mise à jour des metadonnées"  doesn't appear in Log File

 

So I suppose the problem is about method or class definition

Thank you in advance

1 Reply
afaust
Master

Re: Custom task listener in Java

That exception message is likely a standard Java / Activiti exception message wrapped around the NullPointerException. The problem in your case is likely related to your use of annotations for configuration. Activiti inside of Alfresco (the embedded workflow engine) does not support configuration by annotation, and as such, all of your services that you specified via Autowired will not be set, causing NullPointerException upon access. You need to use Spring XML to define an instance of your listener, expose that bean to the Activiti engine, and then use a delegate expression instead of the class name to pick up the configured instance as a listener.