ActivitiEventListener的onEvent方法无法正常工作

时间:2016-03-21 10:35:46

标签: activiti

import org.activiti.engine.RuntimeService;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.ExecutionListener;
import org.activiti.engine.delegate.TaskListener;
import org.activiti.engine.delegate.event.ActivitiEvent;
import org.activiti.engine.delegate.event.ActivitiEventListener;


@SuppressWarnings("serial")
public class MyEventListener implements ActivitiEventListener,ExecutionListener,TaskListener {

  @Override
  public void onEvent(ActivitiEvent event) {

      System.out.println("Excecution id"+event.getExecutionId()+"ProcessDefinition id"+event.getProcessDefinitionId());
      System.out.println("ProcessInstance Id"+event.getProcessInstanceId()+"Type"+event.getType());

    switch (event.getType()) {

      case ACTIVITY_STARTED:
          System.out.println("Activi started ");
          break;
      case ACTIVITY_COMPLETED:
          System.out.println("Activi completed ");
          break;

      case TASK_CREATED : 
          System.out.println("Task Created ");
          break;

      case TASK_COMPLETED:
          System.out.println("Task completed ");
          break;

      case JOB_EXECUTION_SUCCESS:
        System.out.println("A job well done!");
        break;

      case JOB_EXECUTION_FAILURE:
        System.out.println("A job has failed...");
        break;

      default:
        System.out.println("Event received: " + event.getType());
    }
  }

  @Override
  public boolean isFailOnException() {
    // The logic in the onEvent method of this listener is not critical, exceptions
    // can be ignored if logging fails...
    return false;
  }

@Override
public void notify(DelegateExecution arg0) throws Exception {

    System.out.println("Notify Execution called");
    // TODO Auto-generated method stub

}

@Override
public void notify(DelegateTask arg0) {
    System.out.println("Notify Delegate task called");
    // TODO Auto-generated method stub      
}
}

我已为进程指定了此类名。 我能够看到来自notify方法的消息,但onEvent方法没有调用。

我已为进程指定了此类名。 我能够看到来自notify方法的消息,但onEvent方法没有调用。

BPMN代码:

<process id="myProcess" name="myProcess" isExecutable="true">
    <extensionElements>
<activiti:executionListener event="start" class="MyEventListener"></activiti:executionListener>
    </extensionElements>
    <startEvent id="startevent1" name="Start"></startEvent>
    <userTask id="usertask1" name="Login" activiti:assignee="Darshan">
      <extensionElements>
        <activiti:taskListener event="create" class="MyEventListener"></activiti:taskListener>
      </extensionElements>
    </userTask>
    <userTask id="usertask2" name="Welcome Page" activiti:assignee="Darshan"></userTask>
    <endEvent id="endevent1" name="End"></endEvent>
    <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow>
    <sequenceFlow id="flow2" sourceRef="usertask1" targetRef="usertask2"></sequenceFlow>
    <sequenceFlow id="flow3" sourceRef="usertask2" targetRef="endevent1"></sequenceFlow>
  </process>

1 个答案:

答案 0 :(得分:0)

您可以看到来自for item in metarArray[2...remarkIndex] 方法的消息,因为notify类已注册为任务侦听器,因此Activiti会调用MyEventListener接口提供的notify(DelegateTask arg0)
要调用TaskListener,您需要将您的类注册为事件监听器。 BPMN中的任务应如下所示:

onEvent(ActivitiEvent event)

您可以在Activiti用户指南Adding listeners to process definitions部分

中找到更多详细信息