Demo项目代码:
https://gitee.com/AnAnXiEr/activiti6-demo
1、命令模式与责任链模式
2、拦截器的配置方式
3、Activiti的拦截器
命令模式:
![](https://img.haomeiwen.com/i6929381/a65b7582738acd6e.png)
抽象的Command接口
ConcerteCommand命令实现
传入Receiver接受者参数
Receiver+ConcerteCommand创建一个命令,由Invoker执行器去执行(调用ConcerteCommand里的execute()方法,最终调用的是Receiver里面的action方法 )
时序图:
![](https://img.haomeiwen.com/i6929381/3b313e0093e0ab18.png)
客户端
new Receiver()- > new ConcreteCommand(aReceiver) -> new Invoker(aCommand)
call() -> execute() ->action()
责任链模式:
ComandInterceptor
![](https://img.haomeiwen.com/i6929381/0c11513e1b2fe292.png)
命令拦截器的配置
配置Interceptor
customPreCommandInterceptors:配置在默认拦截器之前
customPostCommandInterceptors:配置在默认拦截器之后
commandInvoker:配置在最后
Activiti的拦截器
![](https://img.haomeiwen.com/i6929381/2e3124e9c482e14a.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);
}
}
}
网友评论