Activiti5第七弹,自己实现一个ProcessEngineConfiguration同时自定义拦截器

首先是我自己定义的MyProcessEngineConfiguration的activiti.cfg.xml文件的内容

<?xml version="1.0"?>
<beans default-lazy-init="false"xsi:schemaLocation=" http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jee="http://www.springframework.org/schema/jee"xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context"xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://www.springframework.org/schema/beans"><!-- DBCP数据库 --><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"><property name="driverClassName" value="com.mysql.jdbc.Driver"></property><property name="url" value="jdbc:mysql://localhost:3306/activi1"></property><property name="username" value="root"></property><property name="password"  value="root"></property></bean><!--processEngineConfiguration--><bean class="org.mpc.final_activiti.MyProcessEngineConfiguration"id="myProcessEngineConfiguration"><!-- 数据库配置方式一:直接使用jdbc的各种属性来配置,全部交给了activiti <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/activi1" /><property name="jdbcDriver" value="com.mysql.jdbc.Driver" /> <property name="jdbcUsername" value="root" /><property name="jdbcPassword" value="root" /> --><!-- 数据库配置方式二:将数据库的各种配置交给DBCP,然后activiti只配置DBCP数据库 --><property name="dataSource" ref="dataSource"></property><!-- 邮箱相关配置 START--><property name="mailServerHost" value="smtp.163.com" /><property name="mailServerPort" value="25" /><property name="mailServerUsername" value="15203437412"></property><property name="mailServerPassword" value="aa5524929"></property><property name="mailServerDefaultFrom" value="15203437412@163.com"></property><!-- 邮箱相关配置 END --><!-- 设置流程引擎启动和关闭时数据库执行的策略有一下三个值:false Activiti在启动时,会对比数据库中保存的版本,如果没有表或者版本不匹配,将在启动时抛出异常。true Activiti在启动时,会对数据库中所有表进行更新,如果表不存在,则Activiti会自动创建。create-drop Activiti在启动时会执行表的创建工作,在关闭时会执行表的删除工作。--><property name="databaseSchemaUpdate" value="true" /><!-- 制定要使用的数据库类型 --><property name="databaseType" value="mysql"></property><!-- 启动或关闭jobexecutor --><property name="jobExecutorActivate" value="true" /><!-- 保存流程相关的历史数据none 不保存任何历史数据,因此在流程执行中,这是最高效的。      																					    0activity 级别高于none,保存流程实例与流程行为,其他数据不保存。  																					    1audit 在前者的基础上,还会保存全部的流程任务极其属性。       																						2 full 保存历史数据的最高级别,除了会保存audit级别的数据外,还会保存其他全部流程相关的细节数据,包括一些流程参数等。   3--><property name="history" value="full"></property><!-- 这是我自定义的processEngineConfiguration中的一个属性,在这里进行注入 --><property name="name"    value="mpc_test"></property></bean>
</beans>

和默认的ProcessEngineConfiguration配置不同的是,这里多了一个属性,名为name。这个name就是我自己的ProcessEngineConfiguration中定义的属性,在这里进行注入



然后是XML文件中定义的BEAN所对应的org.mpc.final_activiti.MyProcessEngineConfiguration类

package org.mpc.final_activiti;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;
import org.activiti.engine.impl.interceptor.LogInterceptor;public class MyProcessEngineConfiguration extendsProcessEngineConfigurationImpl {// 要注入到自定义的ProcessEngineConfiguration中的属性private String name;public MyProcessEngineConfiguration() {}public String getName() {return name;}public void setName(String name) {this.name = name;}@Overrideprotected CommandInterceptor createTransactionInterceptor() {// 这个是Activiti5的ProcessEngineConfigurationImpl中定义的抽象方法,子类实现后可以向流程中添加一个自己定义的拦截器return new MyInterceptorA();}@Overrideprotected Collection<? extends CommandInterceptor> getDefaultCommandInterceptors() {// 重写父类的这个方法List<CommandInterceptor> interceptors = new ArrayList<CommandInterceptor>();interceptors.add(new LogInterceptor());
<span style="white-space:pre">		</span>//这里调用了上面的方法,把MyInterceptorA加入了责任链CommandInterceptor transactionInterceptor = createTransactionInterceptor();if (transactionInterceptor != null) {interceptors.add(transactionInterceptor);}// 在这里我们就可以添加任意多的拦截器了,比起Activiti5留给我们的只能添加一个拦截器的方法要实用interceptors.add(new MyInterceptorC());interceptors.add(new MyInterceptorB());// 默认的拦截器,是用来执行SQL脚本的interceptors.add(new CommandContextInterceptor(commandContextFactory,this));return interceptors;}}


下面是在MyProcessEngineConfiguration类中使用的MyInterceptorA的实现

package org.mpc.final_activiti;import org.activiti.engine.impl.interceptor.Command;
import org.activiti.engine.impl.interceptor.CommandConfig;
import org.activiti.engine.impl.interceptor.CommandContextInterceptor;/*** * * 继承CommandContextInterceptor,实现execute方法--使用的是责任链设计模式* * * */
public class MyInterceptorA extends CommandContextInterceptor {@Overridepublic <T> T execute(CommandConfig config, Command<T> command) {System.out.println("this is interceptor A "+ command.getClass().getName() + " "+ config.getClass().getName());return next.execute(config, command);}
}

MyInterceptorB,MyInterceptorC的实现和MyInterceptorA一样,只是在输出的时候改成了B,C。

最后是测试代码

package final_activiti.progress;import java.util.HashMap;
import java.util.Map;import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.junit.Assert;
import org.junit.Test;
import org.mpc.final_activiti.MyProcessEngineConfiguration;public class ConfigurationTest {@Testpublic void test() {MyProcessEngineConfiguration configuration = (MyProcessEngineConfiguration) ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("my_config.xml","myProcessEngineConfiguration");ProcessEngine p = configuration.buildProcessEngine();RepositoryService repositoryService = p.getRepositoryService();RuntimeService runtimeService = p.getRuntimeService();//这里我随便找了一个流程图部署repositoryService.createDeployment().addClasspathResource("final_activiti/progress/RecAndManulTask.bpmn").deploy();Map<String, Object> map = new HashMap<String, Object>();map.put("123", configuration.getName());Assert.assertTrue("mpc_test".equals(configuration.getName()));ProcessInstance pi = runtimeService.startProcessInstanceByKey("processRM", map);runtimeService.signal(pi.getId());}}

测试结果,在junit中是绿色的,没问题。在控制台的输出如下:

一月 23, 2015 9:50:17 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [my_config.xml]
this is interceptor A org.activiti.engine.impl.SchemaOperationsProcessEngineBuild org.activiti.engine.impl.interceptor.CommandConfig
this is interceptor C org.activiti.engine.impl.SchemaOperationsProcessEngineBuild org.activiti.engine.impl.interceptor.CommandConfig
this is interceptor B org.activiti.engine.impl.SchemaOperationsProcessEngineBuild org.activiti.engine.impl.interceptor.CommandConfig
一月 23, 2015 9:50:19 上午 org.activiti.engine.impl.ProcessEngineImpl <init>
信息: ProcessEngine default created
一月 23, 2015 9:50:19 上午 org.activiti.engine.impl.jobexecutor.JobExecutor start
信息: Starting up the JobExecutor[org.activiti.engine.impl.jobexecutor.DefaultJobExecutor].
一月 23, 2015 9:50:19 上午 org.activiti.engine.impl.jobexecutor.AcquireJobsRunnableImpl run
信息: JobExecutor[org.activiti.engine.impl.jobexecutor.DefaultJobExecutor] starting to acquire jobs
this is interceptor A org.activiti.engine.impl.cmd.AcquireJobsCmd org.activiti.engine.impl.interceptor.CommandConfig
this is interceptor C org.activiti.engine.impl.cmd.AcquireJobsCmd org.activiti.engine.impl.interceptor.CommandConfig
this is interceptor B org.activiti.engine.impl.cmd.AcquireJobsCmd org.activiti.engine.impl.interceptor.CommandConfig
this is interceptor A org.activiti.engine.impl.cmd.DeployCmd org.activiti.engine.impl.interceptor.CommandConfig
this is interceptor C org.activiti.engine.impl.cmd.DeployCmd org.activiti.engine.impl.interceptor.CommandConfig
this is interceptor B org.activiti.engine.impl.cmd.DeployCmd org.activiti.engine.impl.interceptor.CommandConfig
this is interceptor A org.activiti.engine.impl.cmd.GetNextIdBlockCmd org.activiti.engine.impl.interceptor.CommandConfig
this is interceptor C org.activiti.engine.impl.cmd.GetNextIdBlockCmd org.activiti.engine.impl.interceptor.CommandConfig
this is interceptor B org.activiti.engine.impl.cmd.GetNextIdBlockCmd org.activiti.engine.impl.interceptor.CommandConfig
一月 23, 2015 9:50:19 上午 org.activiti.engine.impl.bpmn.deployer.BpmnDeployer deploy
信息: Processing resource final_activiti/progress/RecAndManulTask.bpmn
this is interceptor A org.activiti.engine.impl.cmd.StartProcessInstanceCmd org.activiti.engine.impl.interceptor.CommandConfig
this is interceptor C org.activiti.engine.impl.cmd.StartProcessInstanceCmd org.activiti.engine.impl.interceptor.CommandConfig
this is interceptor B org.activiti.engine.impl.cmd.StartProcessInstanceCmd org.activiti.engine.impl.interceptor.CommandConfig
this is interceptor A org.activiti.engine.impl.cmd.SignalCmd org.activiti.engine.impl.interceptor.CommandConfig
this is interceptor C org.activiti.engine.impl.cmd.SignalCmd org.activiti.engine.impl.interceptor.CommandConfig
this is interceptor B org.activiti.engine.impl.cmd.SignalCmd org.activiti.engine.impl.interceptor.CommandConfig

可以看到,定义的拦截器,按照他们加入责任链的顺序执行了,对每一个命令都有执行

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/569336.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

SQL中and和or的区别是?

今天有这样得一个需求&#xff0c;如果登陆人是客服的话&#xff0c;会查询订单是’该客服’以及还没有匹配客服的&#xff0c;刚开始想的是直接在sql语句上拼写 or assigned_id is null 的&#xff0c;测试了一下发现这样的话&#xff0c;前面的其他条件都没有用了 这样的话…

Java编程设计---数组Arrays

数组的的定义 数组是存放在连续存储空间的元素集合 数组定义的格式&#xff1a; int[] arrnew int[5]; int&#xff1a;数组元素的数据类型&#xff0c;可以是基本数据类型&#xff0c;也可以是引用 arr&#xff1a;数组名称 5&#xff1a;数组中元素个数 第一步&#xff1a;定…

装饰器模式 decorator

所有的说明和解释都在代码中有注释来标明 package mode.decorator;/*** * 这里定义一个接口&#xff0c;在接口中定义我们要执行的操作。* * 以后所有的装饰器以及我们要装饰的对象都要实现这个接口。有了这样的大前提&#xff0c;我们就可以其用 Sourcable来定义我们的装饰器和…

bzoj1176: [Balkan2007]Mokia cdq

链接 bzoj 思路 cdq入门题&#xff0c;拆成4个矩阵&#xff0c;然后cdq。 代码 /**************************************************************Problem: 1176User: gryz2016Language: CResult: AcceptedTime:2652 msMemory:13012 kb *************************************…

桥接模式(Bridge)

桥接模式&#xff0c;就是把向多个方向发展的变化由继承的实现变为了耦合的实现。 package mode.bridge.test;/*** * 首先是一个抽象的咖啡类&#xff0c;有一个抽象的倒咖啡的方法* * 在这个类中有一个咖啡伴侣的属性&#xff0c;为什么会有这个属性。因为我们在冲咖啡的时候可…

python 中的if else 和in

python中if else 和in的用法php python a3 //python中‘:’是引入一个缩进的代码块 if a1:print(1) elif a3:print(3) else:print("查不到") python中的in 查看一个对象是否在另一个对象中 a[1,2,3,4,5,6] b3 if b in a:print("b在a中") else: 转载于:http…

15年1月的每天小程序

package everyworkdayprogramming._2015_1_04;public class Java_1_4 {/*** * * 打印出所有的水仙花数&#xff0c;所谓水仙花数是指一个三位数&#xff0c;其各位数字立方和等于该数本身。* * */public static void main(String[] args) {int bit 0, ten 0, hun 0;for (int…

SpringBoot整合升级Spring Security 报错 【The request was rejected because the URL was not normalized】...

前言 最近LZ给项目框架升级&#xff0c; 从Spring1.x升级到Spring2.x, 在这里就不多赘述两个版本之间的区别以及升级的原因。 关于升级过程中踩的坑&#xff0c;在其他博文中会做比较详细的记录&#xff0c;以便给读者参考&#xff0c;不要掉进同样的坑里。 这里我们讨论一个关…

个人测试作业

作业所属课程&#xff1a;https://edu.cnblogs.com/campus/xnsy/SoftwareEngineeringClass2 作业地址&#xff1a;https://edu.cnblogs.com/campus/xnsy/SoftwareEngineeringClass2/homework/3340 作业目标&#xff1a;测试其他同学项目 姓名&#xff1a;潘云峰201731062423 所…

安装Nvida 显示环境

查看是否能正确加载nvidia 驱动 在终端输入 &#xff08;glxinfo 需要安装mesa-utils&#xff09; 如果可以正确加载了nvidia驱动 那么在输入的内容中可以看到NVIDIA 字样 如果GPU是IntelGPU 正确加载类似 卸载 老版本 驱动 sudo apt-get purge nvidia* 把显卡驱动加入PPA sudo…

Activiti5第十一弹,流程监听器与任务监听器

首先创建流程监听器和任务监听器的实体类&#xff0c;个人比较喜欢使用Delegate Expression方式&#xff0c;其他两种方式也可以 流程监听器 package org.mpc.final_activiti;import java.io.Serializable;import org.activiti.engine.delegate.DelegateExecution; import org.…

06_go语言基础

// 07枚举 package main import ( "fmt" ) func main() { // 1.iota常量自动生成器&#xff0c;每个一行&#xff0c;自动累加1 // 2.iota给常量赋值使用 const ( a iota // 0 b iota // 1 c iota // 2 ) fmt.Printf("a %d,b %d,c %d\n", a, b, c) /…

通过继承来实现注解方式的属性注入

要使用注解来注入属性&#xff0c;首先就要定义一个注解&#xff0c;注解的定义如下&#xff1a; package everyworkdayprogramming._2015_1_23;import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Inherited; im…

ASCII码对照表

1、字母转换成ASCII码 1 string str "hello";2 byte[] array new byte[1]; 3 array System.Text.Encoding.ASCII.GetBytes(str); //把str的每个字符转换成ascii码4 5 int asciicode1 (short)(array[0]);//h 的…

2015-2月的小程序们

马上就要过年了&#xff0c;今天是二月最后一天上班了&#xff0c;心情有些激动&#xff0c;恨不得立马就到家去。再来公司的时候就是3.1号了&#xff0c;所以在离开之前把2月份的小程序们储存起来。虽然很多都是参考了网上的小程序练习&#xff0c;但是自己有自己的风格&#…

SHOI2009 会场预约

题目传送门 嗯&#xff0c;这道题的标签是STL&#xff0c;因为这个STL用的确实太妙了 这道题目要求维护一堆区间&#xff0c;而一个重要的操作是要删除所有与新区间冲突的区间 虽然可以用\(Splay\)来操作&#xff0c;但用STL里的set也绝对不虚 其中最精妙的当属这个重载运算符 …

创建者模式 builder

package mode.bulider;/*** * 首先是汽车&#xff0c;组成零件有&#xff1a;玻璃、轮胎、发动机&#xff1b;他们都是抽象的概念&#xff0c;所以用抽象类来创建* * */ public class Car {Glass glass;Wheel wheel;Engine engine; } package mode.bulider; /*** * 抽象的玻璃*…

平行四边形的特殊性质

定义平行四边形内角平分线围城的四边形为$\lambda$四边形$\lambda$四边形是矩形 $\lambda$四边形的中线为平行四边形中心$\lambda$四边形的对角线和平行四边形的边平行转载于:https://www.cnblogs.com/guoshaoyang/p/11011612.html

静态方法工厂模式

工厂模式有三种方法来实现&#xff0c;一种是通过传入参数的名称来决定创建哪一个产品&#xff0c;这种方法有很大的缺点&#xff0c;就是如果传入的产品名称如果不小心错误的话&#xff0c;就是无法来创建我们想得到的产品的。另一种是方法工厂模式&#xff0c;这种工厂模式中…

Elasticsearch 入门到高手的成长阶梯-索引的基本操作(1)

1. 创建索引 Elasticsearch中索引的名称&#xff0c;必须符合以下要求&#xff1a; 字母只能够是小写字母不能够包含特殊字符&#xff0c;如\, /, *, ?, ", <, >, |, , ,, #等名称不能够以英文的中划线“-”、下划线“_”以及加号“”开头名称不可以是“.”或“.…