Custom task listener in Java

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

Custom task listener in Java


I have the simple following Workflow :

<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:taskListener event="complete" class=""></activiti:taskListener>

My WorkFlowTaskListener java class :

public class WorkFlowTaskListener implements TaskListener {

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

private WorkFlowServices workflowServices;

private ServiceRegistry serviceRegistry;

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);"scriptNode OK !" + scriptNode);
      NodeRef packagenode = scriptNode.getNodeRef();"packagenode OK !" + packagenode);

      if(reviewAction.equals("CLA")) {
    "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();
        "client: " + client);
        "typologie: " + typologie);
        "date: " + docDate);
        "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

It's like if my workflowServices customed Class was unknown :

public class WorkFlowServices {
    private static final Logger logger = LoggerFactory.getLogger(WorkFlowServices.class);
     private CoreServices coreServices;
      private FolderServices folderServices;
      private FileFolderService fileFolderService;
     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

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.