【工作流前进之路】Activiti 研究之拦截器

来源:互联网 发布:淘宝图片添加热区 编辑:程序博客网 时间:2024/06/05 00:35

         Activiti提供了拦截器功能,外界对Activiti流程中各个实例的操作,可以看作是对数据进行操作.Aciviti使用了命令模式和职责链模式来完成这一系列的操作.在介绍Activiti的拦截器之前,需要先来介绍两个GOF中的设计模式:命令模式和职责链模式

.命令模式

        GOF的设计模式中,命令模式属于行为型模式,命令模式的官方定义为:将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化;请求排队或记录请求日志,以及支持可撤销操作.其类图如下:


具体代码不贴了.

.职责链模式

        职责链模式同命令模式一样,也是属于行为型模式,它的官方定义是:使用多个对象都有机会处理请求,从而避免请求的发送者和接受者之间的耦合关系.将这个对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止.


 

具体的代码就不贴了.

 

.自定义拦截器

        在前面,对命令模式和职责链模式进行解释,Activiti的拦截器,就是结合这两个设计模式完成的,在理解Activiti的拦截器之前,我们来自己定义一个拦截器.

 拦截器代码:

/** * 拦截器实现A */import org.activiti.engine.impl.interceptor.Command;import org.activiti.engine.impl.interceptor.CommandInterceptor;public class InterceptorA extends CommandInterceptor {@Overridepublic <T> T execute(Command<T> command) {//输入字符串和命令System.out.println("this is InterceptorA----->"+command.getClass().getName());//然后让职责链中的下一个请求处理者处理命令return next.execute(command);}}

/** * 拦截器实现B */import org.activiti.engine.impl.interceptor.Command;import org.activiti.engine.impl.interceptor.CommandInterceptor;public class InterceptorB extends CommandInterceptor {@Overridepublic <T> T execute(Command<T> command) {//输入字符串和命令System.out.println("this is InterceptorB----->"+command.getClass().getName());//然后让职责链中的下一个请求处理者处理命令return next.execute(command);}}

配置类:

/** * 自定义配置类 */import java.util.ArrayList;import java.util.Collection;import java.util.List;import org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl;import org.activiti.engine.impl.interceptor.CommandContextInterceptor;import org.activiti.engine.impl.interceptor.CommandInterceptor;public class TestConfiguration extends ProcessEngineConfigurationImpl {@Overrideprotected Collection<? extends CommandInterceptor> getDefaultCommandInterceptorsTxRequired() {//创建一个拦截器结合List<CommandInterceptor> defaultCommandInterceptorsTxRequired=new ArrayList<CommandInterceptor>();//添加自定义拦截器AdefaultCommandInterceptorsTxRequired.add(new InterceptorA());//添加系统的拦截器defaultCommandInterceptorsTxRequired.add(new CommandContextInterceptor(commandContextFactory,this)); return defaultCommandInterceptorsTxRequired;}@Overrideprotected Collection<? extends CommandInterceptor> getDefaultCommandInterceptorsTxRequiresNew() {//创建一个拦截器集合List<CommandInterceptor> defaultCommandInterceptorsTxRequired=new ArrayList<CommandInterceptor>();//添加自定义拦截器defaultCommandInterceptorsTxRequired.add(new InterceptorB());return defaultCommandInterceptorsTxRequired;}}


测试类:

// 创建流程引擎 ProcessEngines.getDefaultProcessEngine();//创建Activiti配置对象ProcessEngineConfiguration config=ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("my-config.xml");ProcessEngine engine =config.buildProcessEngine();// 得到流程存储服务组件RepositoryService repositoryService = engine.getRepositoryService();// 部署流程文件repositoryService.createDeployment().addClasspathResource("bpmn/First.bpmn").deploy();//开始流程engine.getRuntimeService().startProcessInstanceByKey("myProcess");


 

执行结果:


从截图中可以看到拦截器输出的相关命令,在流程引擎启动时,执行SchemaOperationsProcessEngineBuild,该类也是一个Command的实现,进行部署,将会执行DeployCmd,获取ID块时,获取GetNextIdblockCmd,开始流程时,执行StartProcessInstanceCmd.

 

自定拦截器的类图:


 

这是我对activiti中拦截器的理解,如果您有不同的理解,欢迎交流.

0 0
原创粉丝点击