Activiti7系列课程8-流程和任务事件监听器

摘要:本文重点讲解一下Activiti7中的流程(ProcessRuntimeEventListener)和任务事件监听器(TaskRuntimeEventListener)。

流程事件监听器

流程事件监听器和任务事件监听器是在Activiti5/6两个版本扩展实现的。 这些扩展还意味着监听器相关代码将与流程执行可以同步运行。 在云Cloud 中已经抛弃了原有的事件监听器。 在Activiti 7中,流程引擎会以异步方式发出我们可以监听和订阅的事件。

通过实现org.activiti.api.process.runtime.events.listener.ProcessRuntimeEventListener接口创建流程事件监听器。

@Service
public class MyProcessEventListener implements ProcessRuntimeEventListener {
    private Logger logger = LoggerFactory.getLogger(MyProcessEventListener.class);
    @Override
    public void onEvent(RuntimeEvent runtimeEvent) {
        if (runtimeEvent instanceof ProcessStartedEvent)
            logger.info("Do something, process is started: " + runtimeEvent.toString());
        else if (runtimeEvent instanceof ProcessCompletedEvent)
            logger.info("Do something, process is completed: " + runtimeEvent.toString());
        else if (runtimeEvent instanceof ProcessCancelledEvent)
            logger.info("Do something, process is cancelled: " + runtimeEvent.toString());
        else if (runtimeEvent instanceof ProcessSuspendedEvent)
            logger.info("Do something, process is suspended: " + runtimeEvent.toString());
        else if (runtimeEvent instanceof ProcessResumedEvent)
            logger.info("Do something, process is resumed: " + runtimeEvent.toString());
        else if (runtimeEvent instanceof ProcessCreatedEvent)
            logger.info("Do something, process is created: " + runtimeEvent.toString());
        else if (runtimeEvent instanceof ActivitiSequenceFlowTakenEvent)
            logger.info("Do something, sequence flow is taken: " + runtimeEvent.toString());
        else if (runtimeEvent instanceof VariableCreatedEvent)
            logger.info("Do something, variable was created: " + runtimeEvent.toString());
        else
            logger.info("Unknown event: " + runtimeEvent.toString());

    }
}

通过实现ProcessRuntimeEventListener接口,程序进程侦听器自动连接到ProcessRuntime。 监听器收到一个RuntimeEvent,其中包含有关该事件的所有信息。 我们可以查看子类来确定并处理的事件内容,例如ProcessCompletedEvent。

任务事件监听器

任务事件监听器需要实现TaskRuntimeEventListener接口,代码如下:

@Service
public class MyTaskEventListener implements TaskRuntimeEventListener {
    private Logger logger = LoggerFactory.getLogger(MyTaskEventListener.class);

    @Override
    public void onEvent(RuntimeEvent runtimeEvent) {
        if (runtimeEvent instanceof TaskActivatedEvent)
            logger.info("Do something, task is activated: " + runtimeEvent.toString());
        else if (runtimeEvent instanceof TaskAssignedEvent) {
            TaskAssignedEvent taskEvent = (TaskAssignedEvent)runtimeEvent;
            Task task = taskEvent.getEntity();
            logger.info("Do something, task is assigned: " + task.toString());
        } else if (runtimeEvent instanceof TaskCancelledEvent)
            logger.info("Do something, task is cancelled: " + runtimeEvent.toString());
        else if (runtimeEvent instanceof TaskCompletedEvent)
            logger.info("Do something, task is completed: " + runtimeEvent.toString());
        else if (runtimeEvent instanceof TaskCreatedEvent)
            logger.info("Do something, task is created: " + runtimeEvent.toString());
        else if (runtimeEvent instanceof TaskSuspendedEvent)
            logger.info("Do something, task is suspended: " + runtimeEvent.toString());
        else
            logger.info("Unknown event: " + runtimeEvent.toString());

    }
}

流程和任务事件监听器开箱即用,无需额外配置。

博客相关资料获取

博客相关的资料以及代码获取可以直接添加qq:3152981878(请务必备注,activiti7学习)

你可能感兴趣的:(activiti工作流)