上一章我们activiti的命令、拦截器等进行了剖析。我们已经很熟悉其执行的原理,本章我们自己动手写拦截器和命令类。
首先我们明确一下拦截器调用链的执行先后顺序。上一章代码剖析,已经能看出来,每个拦截器,都在自己的execute方法中执行下一个拦截器的execute方法,是嵌套调用,因此执行的先后顺序大致如下图:
我们直接看代码效果,先自定义我们的命令MyCommand.java:
public class MyCommand implements Command<String>, Serializable{
public String execute(CommandContext commandContext) {
System.out.println("Hello world");
return null;
}
}
命令类必须实现Command<T>接口,并且实现其execute方法。
接着我们分别自定义前置拦截器MyPreIntercepter.java和后置拦截器MyPostIntercepter.java
public class MyPreIntercepter extends AbstractCommandInterceptor{
public <T> T execute(CommandConfig config, Command<T> command) {
System.out.println("MyPreIntercepter: execute start");
next.execute(config, command);
System.out.println("MyPreIntercepter: execute end");
return null;
}
}
public class MyPostIntercepter extends AbstractCommandInterceptor{
public <T> T execute(CommandConfig config, Command<T> command) {
System.out.println("MyPostIntercepter: execute start");
next.execute(config, command);
System.out.println("MyPostIntercepter: execute end");
return null;
}
}
自定义的拦截器需要在流程引擎初始化的时候进行设置,因此我们需要在流程引擎的配置文件中添加,我们新建一个activitiIntercepter.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 id="processEngineConfiguration"
class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
<property name="jdbcUrl"
value="jdbc:mysql://localhost:3306/db_activiti?useUnicode=true&&characterEncoding=utf8&serverTimezone=UTC" />
<property name="jdbcDriver" value="com.mysql.jdbc.Driver" />
<property name="jdbcUsername" value="root" />
<property name="jdbcPassword" value="" />
<property name="databaseSchemaUpdate" value="true" />
<property name="customPreCommandInterceptors">
<list>
<bean class="commandAndIntercepter.MyPreIntercepter"></bean>
</list>
</property>
<property name="customPostCommandInterceptors">
<list>
<bean class="commandAndIntercepter.MyPostIntercepter"></bean>
</list>
</property>
</bean>
</beans>
17-26行配置自定义的前置与后置命令拦截器。其他配置与以往情况相同。
接着我们编写调用命令类的代码,App.java
public class App {
private ProcessEngine pe;
public void getFromProcessEngineConfiguration() {
ProcessEngineConfiguration pec = ProcessEngineConfiguration
.createProcessEngineConfigurationFromResource("activitiInterceper.cfg.xml");
pe = pec.buildProcessEngine();
}
public void test() {
MyCommand myCommand = new MyCommand();
ServiceImpl service = (ServiceImpl)pe.getRepositoryService();
CommandExecutor commandExecutor = service.getCommandExecutor();
commandExecutor.execute(myCommand);
}
public static void main(String[] args) {
App app = new App();
app.getFromProcessEngineConfiguration();
app.test();
}
}
运行程序,查看控制台输出:
MyPreIntercepter: execute start
MyPostIntercepter: execute start
MyPostIntercepter: execute end
MyPreIntercepter: execute end
MyPreIntercepter: execute start
MyPostIntercepter: execute start
Hello world
MyPostIntercepter: execute end
MyPreIntercepter: execute end
很奇怪我们的拦截器执行了两次,而我们只调用了一次命令类!其实在第8行pec.buildProcessEngine()构造流程引擎时,流程引擎就会调用一次内置的命令类SchemaOperationsProcessEngineBuild构造数据库,所以会被拦截器拦截到,拦截器第一次执行便是拦截这个命令。