上一章我們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構造數據庫,所以會被攔截器攔截到,攔截器第一次執行便是攔截這個命令。