美文网首页
Activiti流程引擎配置5-命令拦截器

Activiti流程引擎配置5-命令拦截器

作者: 安安汐而 | 来源:发表于2019-08-20 13:20 被阅读0次

Demo项目代码:
https://gitee.com/AnAnXiEr/activiti6-demo

1、命令模式与责任链模式
2、拦截器的配置方式
3、Activiti的拦截器

命令模式:

image.png

抽象的Command接口

ConcerteCommand命令实现
传入Receiver接受者参数
Receiver+ConcerteCommand创建一个命令,由Invoker执行器去执行(调用ConcerteCommand里的execute()方法,最终调用的是Receiver里面的action方法 )

时序图:

image.png

客户端
new Receiver()- > new ConcreteCommand(aReceiver) -> new Invoker(aCommand)
call() -> execute() ->action()

责任链模式:

ComandInterceptor

image.png

命令拦截器的配置

配置Interceptor

customPreCommandInterceptors:配置在默认拦截器之前
customPostCommandInterceptors:配置在默认拦截器之后
commandInvoker:配置在最后

Activiti的拦截器
image.png
cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean class="org.activiti.engine.impl.cfg.StandaloneInMemProcessEngineConfiguration"
          id="processEngineConfiguration">

        <property name="customPreCommandInterceptors">
            <list>
                <bean class="com.imooc.activiti.activitidemo.interceptor.DurationComandInterceptor"></bean>
            </list>
        </property>

        <property name="commandInvoker" ref="mdcCommandInvoker"/>

    </bean>


    <bean class="com.imooc.activiti.activitidemo.interceptor.MDCCommandInvoker" id="mdcCommandInvoker"/>

</beans>
DurationComandInterceptor
package com.imooc.activiti.activitidemo.interceptor;

import org.activiti.engine.impl.interceptor.AbstractCommandInterceptor;
import org.activiti.engine.impl.interceptor.Command;
import org.activiti.engine.impl.interceptor.CommandConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @Description 执行时间
 * @Author 胡浩
 * @Date 2019/8/20
 **/
public class DurationComandInterceptor extends AbstractCommandInterceptor {
    private static final Logger LOGGER = LoggerFactory.getLogger(DurationComandInterceptor.class);

    @Override
    public <T> T execute(CommandConfig commandConfig, Command<T> command) {
        long start = System.currentTimeMillis();
        try {
            return this.getNext().execute(commandConfig, command);
        } finally {
            long duration = System.currentTimeMillis() - start;
            LOGGER.info("{} 执行时长 {} 毫秒", command.getClass().getSimpleName(), duration);
        }

    }
}

相关文章

网友评论

      本文标题:Activiti流程引擎配置5-命令拦截器

      本文链接:https://www.haomeiwen.com/subject/uhxpsctx.html