activiti流程--监听器

监听器是activiti中经常使用的,一般用在启动或者关闭,以及流程扭转的时候。


1:流程引擎的启动与监听


之前的配置文件中,有这么个配置:


/**
 * 用于监听流程引擎的启动与关闭
 * 
 * @author wsylp
 *
 */
@Service("myProcessEngineLifecycleListener")
public class MyProcessEngineLifecycleListener implements ProcessEngineLifecycleListener   {
    private static final Logger LOGGER = LoggerFactory.getLogger(ActivitiServiceImpl.class);

    @Override
    public void onProcessEngineBuilt(ProcessEngine processEngine) {
        LOGGER.debug("processEngine:onProcessEngineBuilt{}开始启动", processEngine);
        LOGGER.info("processEngine:onProcessEngineBuilt:{}开始启动" + processEngine);
    }

    @Override
    public void onProcessEngineClosed(ProcessEngine processEngine) {
        LOGGER.debug("processEngine:onProcessEngineClosed{}结束流程", processEngine);
        LOGGER.info("processEngine:onProcessEngineBuilt:{}结束流程" + processEngine);
    }


}

此时我们进行查询任务的单元测试,进行观察日志


[09:15:38:575] [DEBUG] - wsylp.service.impl.MyProcessEngineLifecycleListener.onProcessEngineBuilt(MyProcessEngineLifecycleListener.java:22) - processEngine:onProcessEngineBuiltorg.activiti.engine.impl.ProcessEngineImpl@4417af13开始启动 [09:15:38:575] [INFO] - wsylp.service.impl.MyProcessEngineLifecycleListener.onProcessEngineBuilt(MyProcessEngineLifecycleListener.java:23) - processEngine:onProcessEngineBuilt:{}开始启动
org.activiti.engine.impl.ProcessEngineImpl@4417af13
[09:15:38:576] [INFO] - wsylp.log.MyLog.logAfter(MyLog.java:29) - @AfterReturning:org.activiti.engine.ProcessEngineLifecycleListener.onProcessEngineBuilt end.
[09:15:38:656] [INFO] - wsylp.log.MyLog.logBegin(MyLog.java:22) - @Before:wsylp.service.ActivitiService.getTasks begin.
[09:15:38:657] [DEBUG] - org.activiti.engine.impl.interceptor.LogInterceptor.execute(LogInterceptor.java:33) - 

[09:15:38:658] [DEBUG] - org.activiti.engine.impl.interceptor.LogInterceptor.execute(LogInterceptor.java:34) - --- starting TaskQueryImpl --------------------------------------------------------
[09:15:38:658] [DEBUG] - org.activiti.spring.SpringTransactionInterceptor.execute(SpringTransactionInterceptor.java:40) - Running command with propagation REQUIRED
[09:15:38:718] [DEBUG] - org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(BaseJdbcLogger.java:159) - ==>  Preparing: select distinct RES.* from ACT_RU_TASK RES order by RES.ID_ asc LIMIT ? OFFSET ? 
[09:15:38:720] [DEBUG] - org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(BaseJdbcLogger.java:159) - ==> Parameters: 2147483647(Integer), 0(Integer)
[09:15:38:756] [DEBUG] - org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(BaseJdbcLogger.java:159) - <==      Total: 3
[09:15:38:756] [DEBUG] - org.activiti.engine.impl.db.DbSqlSession.flush(DbSqlSession.java:611) - flush summary: 0 insert, 0 update, 0 delete.
[09:15:38:757] [DEBUG] - org.activiti.engine.impl.db.DbSqlSession.flush(DbSqlSession.java:612) - now executing flush...
[09:15:38:763] [DEBUG] - org.activiti.engine.impl.interceptor.LogInterceptor.execute(LogInterceptor.java:40) - --- TaskQueryImpl finished --------------------------------------------------------
[09:15:38:763] [DEBUG] - org.activiti.engine.impl.interceptor.LogInterceptor.execute(LogInterceptor.java:41) - 

[09:15:38:764] [INFO] - wsylp.service.impl.ActivitiServiceImpl.getTasks(ActivitiServiceImpl.java:166) - 任务处理人【zjl】,任务id【22504】,任务名称【总经理[审批]】,任务流程定义id【sequenceFlow:1:7504】, 流程定义key【zjlsp】,任务拥有者【null】 
[09:15:38:764] [INFO] - wsylp.service.impl.ActivitiServiceImpl.getTasks(ActivitiServiceImpl.java:166) - 任务处理人【zjl】,任务id【30004】,任务名称【总经理[审批]】,任务流程定义id【sequenceFlow:1:7504】, 流程定义key【zjlsp】,任务拥有者【null】 
[09:15:38:765] [INFO] - wsylp.service.impl.ActivitiServiceImpl.getTasks(ActivitiServiceImpl.java:166) - 任务处理人【gy】,任务id【35014】,任务名称【普通[处理]】,任务流程定义id【gateway:1:32504】, 流程定义key【generalApprove】,任务拥有者【null】 
[09:15:38:765] [INFO] - wsylp.log.MyLog.logAfter(MyLog.java:29) - @AfterReturning:wsylp.service.ActivitiService.getTasks end.
[09:15:38:778] [DEBUG] - org.activiti.engine.impl.interceptor.LogInterceptor.execute(LogInterceptor.java:33) - 

[09:15:38:779] [DEBUG] - org.activiti.engine.impl.interceptor.LogInterceptor.execute(LogInterceptor.java:34) - --- starting SchemaOperationProcessEngineClose --------------------------------------------------------
[09:15:38:779] [DEBUG] - org.activiti.spring.SpringTransactionInterceptor.execute(SpringTransactionInterceptor.java:40) - Running command with propagation NOT_SUPPORTED
[09:15:38:779] [DEBUG] - org.activiti.engine.impl.db.DbSqlSession.flush(DbSqlSession.java:611) - flush summary: 0 insert, 0 update, 0 delete.
[09:15:38:779] [DEBUG] - org.activiti.engine.impl.db.DbSqlSession.flush(DbSqlSession.java:612) - now executing flush...
[09:15:38:780] [DEBUG] - org.activiti.engine.impl.interceptor.LogInterceptor.execute(LogInterceptor.java:40) - --- SchemaOperationProcessEngineClose finished --------------------------------------------------------
[09:15:38:780] [DEBUG] - org.activiti.engine.impl.interceptor.LogInterceptor.execute(LogInterceptor.java:41) - 

[09:15:38:780] [INFO] - wsylp.log.MyLog.logBegin(MyLog.java:22) - @Before:org.activiti.engine.ProcessEngineLifecycleListener.onProcessEngineClosed begin.
[***09:15:38:781] [DEBUG] - wsylp.service.impl.MyProcessEngineLifecycleListener.onProcessEngineClosed(MyProcessEngineLifecycleListener.java:28) - processEngine:onProcessEngineClosedorg.activiti.engine.impl.ProcessEngineImpl@4417af13结束流程
[09:15:38:781] [INFO] - wsylp.service.impl.MyProcessEngineLifecycleListener.onProcessEngineClosed(MyProcessEngineLifecycleListener.java:29) - processEngine:onProcessEngineBuilt:{}结束流程***org.activiti.engine.impl.ProcessEngineImpl@4417af13
[09:15:38:781] [INFO] - wsylp.log.MyLog.logAfter(MyLog.java:29) - @AfterReturning:org.activiti.engine.ProcessEngineLifecycleListener.onProcessEngineClosed end.

从上面的日志中可以看到开始于结束的日志,表示流程正常启动与关闭。


2:开始与结束事件监听器


正常的开启流程需要进行设置变量,指定代理人等信息,或者发送短信提醒,都可以使用事件监听器。

activiti流程--监听器_第1张图片

activiti流程--监听器_第2张图片


日志


[09:38:36:461] [INFO] - wsylp.service.impl.ActivitiListener.notify(ActivitiListener.java:35) - start=========

也可以使用spring管理,写入表达式,其中表达式为spring管理的bean的id
activiti流程--监听器_第3张图片


3:任务监听器


任务监听与开始的事件监听差不多
activiti流程--监听器_第4张图片

package wsylp.service.impl;

import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.Expression;
import org.activiti.engine.delegate.TaskListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 节点监听器,任务监听器,连线监听器
 * 
 * @author wsylp
 *
 */
public class MyTaskListener implements TaskListener {
    private static final Logger LOGGER = LoggerFactory.getLogger(ActivitiServiceImpl.class);

    private static final long serialVersionUID = 6200534335483960408L;

    private Expression arg;

    public Expression getArg() {
        return arg;
    }

    public void setArg(Expression arg) {
        this.arg = arg;
    }

    @Override
    public void notify(DelegateTask delegateTask) {

        // 实现TaskListener中的方法
        String eventName = delegateTask.getEventName();
        LOGGER.info("任务监听器:{}", arg.getValue(delegateTask));
        if ("create".endsWith(eventName)) {
            LOGGER.info("create=========");
        } else if ("assignment".endsWith(eventName)) {
            LOGGER.info("assignment========");
        } else if ("complete".endsWith(eventName)) {
            LOGGER.info("complete===========");
        } else if ("delete".endsWith(eventName)) {
            LOGGER.info("delete=============");
        }

    }

}

日志


[10:36:53:622] [INFO] - wsylp.service.impl.MyTaskListener.notify(MyTaskListener.java:35) - 任务监听器:张飒
[10:36:53:626] [INFO] - wsylp.service.impl.MyTaskListener.notify(MyTaskListener.java:41) - complete===========

github代码地址:https://github.com/wsylp/gms.git

你可能感兴趣的:(activiti)