一、新建activiti工程
选择工具栏File->New->Activiti->Activiti Project , 选择Next,填工程名如:act_demo_1
二、添加依赖类库
1、解压下载的activiti-5.19.0.2.zip,至任意目录,用${activiti}表示根目录
2、找到${activiti}/wars/activiti-rest.war,复制进tomcat目录下的webapps,运行tomcat,出现目录activiti-rest,用${activiti-rest}表示
3、右键工程->New->Folder,新建lib文件夹以保存依赖jar包,将${activiti-rest}\WEB-INF\lib下的所有包复制进入工程lib文件夹
(由工程结构看出,依赖于Maven)
(由依赖包可看出,activiti是由mybatis进行数据库操作,spring进行管理)
4、在工程里全选添加的jar包,右键Build Path->Add to Build Path,将依赖jar包添加至工程依赖中,完成后可看到工程下的Referenced
Libraries 出现所添加的包
5、添加java连接Mysql数据包,mysql-connector-java-5.1.22-bin.jar
6、右键工程,Build Path->Add Libraries...->JUnit->Finish,添加JUnit测试类包
三、初始化数据库
找到工程src/test/java右键,New->other->java->JUnit->JUnit Test Case,类名填写:TestActiviti
PS.下载的activiti下的libs包中,以-sources.jar结尾的为源码包,可通过eclipse链接查看源码
具体操作
1、通过ctrl+shift+o,引用对应的包,如import org.activiti.engine.ProcessEngineConfiguration;
2 、按住Ctrl点击以下 ProcessEngineConfiguration,会出现无法找到源码页,点击Attach Source....
3、 选择 External location,点击External file,找到libs包下的jar,如本例为activiti-engine-5.19.0.2-sources.jar
1、使用代码初始化
<pre name="code" class="java"> @Test public void testInit() { ProcessEngineConfiguration processEngineConfiguration = ProcessEngineConfiguration.createStandaloneProcessEngineConfiguration(); processEngineConfiguration.setJdbcDriver("com.mysql.jdbc.Driver"); processEngineConfiguration.setJdbcUrl("jdbc:mysql://localhost:3306/activiti?useUnicode=true&characterEncoding=utf8"); processEngineConfiguration.setJdbcUsername("scott"); processEngineConfiguration.setJdbcPassword("tiger"); /** public static final String DB_SCHEMA_UPDATE_FALSE = "false";不能自动创建表,需要表存在 public static final String DB_SCHEMA_UPDATE_CREATE_DROP = "create-drop";先删除表再创建表 public static final String DB_SCHEMA_UPDATE_TRUE = "true";如果表不存在,自动创建表 */ processEngineConfiguration.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);//设置初始化数据库方式
ProcessEngine processEngine = processEngineConfiguration.buildProcessEngine();System.out.println("processEngine:"+processEngine);
}
运行后控制台输出ProcessEngine的包名+类名@hashCode,同时发现数据库中出现25张新表,初始化完成。
2、通过配置文件初始化
(1)查看源码可知,调用createProcessEngineConfigurationFromResourceDefault(),会寻找根目录下的activiti.cfg.xml作为配置文件,实例化一个名为
processEngineConfiguration的对象,该对象为org.activiti.engine.ProcessEngineConfiguration类
(2)所以,在工程src/main/resources资源文件夹下,创建activiti.cfg.xml文件如下,头部引用可参考${rest}/WEB-INF/classes/activiti-custom-context.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" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration"> <property name="jdbcDriver" value="com.mysql.jdbc.Driver"/> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/activiti?useUnicode=true&characterEncoding=utf8"/> <property name="jdbcUsername" value="scott"/> <property name="jdbcPassword" value="tiger"/> <!-- 没有表创建表 --> <property name="databaseSchemaUpdate" value="true"/> </bean> </beans>
测试函数,效果同上。
@Test public void testInit2() { ProcessEngineConfiguration processEngineConfiguration = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml"); ProcessEngine processEngine = processEngineConfiguration.buildProcessEngine(); System.out.println("processEngine:"+processEngine); }
(3)若出现下图所示警告,表示未配置日志文件
将${rest}\WEB-INF\classes\log4j.properties,复制进工程src/main/resources资源文件夹即可。
三、新建helloworld流程
1、新建流程定义文件
点击src/main/resources/diagrams,右键New->Other->Activiti->Activiti Diagram,File Name填写HelloWorld.bpmn, 直接到finish
2、打开HelloWorld.bpmn流程定义文件,添加流程开始标志StartEvent,添加流程结束标志EndEvent,中间顺序添加三个UserTask用户任务,
用SequenceFlow连接流程任务。
3、修改流程定义,如下
点击Properties(与console同位置,若无则选择Window->Show View->Properties),
(1)首先选择流程图中空白处,点击Properties中的Process,修改Id为helloWorld,修改Name为helloWorldProcess
(2)选择第一个UserTask,点击Properties中的General,修改Name为提交申请;点击Main config,修改Assignee(流程指派人)为A;
同理,修改第二个UserTask的Name为部门经理审批,Assignee为B;修改第三个UserTask的Name为总经理审批,Assignee为C;
(3)保存后,src/main/resources/diagrams下出现同名.png
PS.可通过右键.bpmn,Open With->XML Editor,查看流程定义详细。
4.新建测试类TestHelloWorld.java
(1) 默认实例化ProcessEngine
public static ProcessEngine processEngine; //加载类时加载流程引擎 @BeforeClass public static void beforeClass(){ processEngine = ProcessEngines.getDefaultProcessEngine(); }
(2)部署流程
常用方式.
//部署流程定义 @Test public void deploymentProcessDefinition() { RepositoryService repositoryService = processEngine.getRepositoryService();//持久化Service,用于部署流程定义相关 DeploymentBuilder deploymentBuilder = repositoryService.createDeployment();//创建一个部署对象 deploymentBuilder.name("helloWorld流程定义");//可有可无 deploymentBuilder.addClasspathResource("diagrams/HelloWorld.bpmn");//表示从classpath的资源中加载,由于resources目录下的文件打包后 deploymentBuilder.addClasspathResource("diagrams/HelloWorld.png");//会出现在WEB-INF/classes中,也即classpath下,所以路径如此 Deployment deployment = deploymentBuilder.deploy(); System.out.println("部署ID:"+deployment.getId()); System.out.println("部署名称:"+deployment.getName()); System.out.println("部署时间:"+deployment.getDeploymentTime()); }
zip部署方式.
将.bpmn以及.png文件打包为zip文件(直接打包两个文件,不要放入文件夹后打包),如HelloWorld.zip,然后放入resources下的diagrams目录。
//用zip格式部署流程定义 @Test public void deployZipProcessDefinition() { RepositoryService repositoryService = processEngine.getRepositoryService();//持久化Service,用于部署流程定义相关 DeploymentBuilder deploymentBuilder = repositoryService.createDeployment();//创建一个部署对象 InputStream in = ClassLoader.getSystemResourceAsStream("diagrams/HelloWorld.zip"); ZipInputStream zipInputStream = new ZipInputStream(in); deploymentBuilder.name("helloWorld流程定义");//可有可无 deploymentBuilder.addZipInputStream(zipInputStream); Deployment deployment = deploymentBuilder.deploy(); System.out.println("部署ID:"+deployment.getId()); System.out.println("部署名称:"+deployment.getName()); System.out.println("部署时间:"+deployment.getDeploymentTime());
InputStream部署方式:
public void deploymentProcessDefinition_InputStream(){ InputStream inputStream_bpmn = ClassLoader.getSystemResourceAsStream("diagrams/HelloWorld.bpmn"); InputStream inputStream_png = ClassLoader.getSystemResourceAsStream("diagrams/HelloWorld.png"); RepositoryService repositoryService = processEngine.getRepositoryService(); DeploymentBuilder deploymentBuilder = repositoryService.createDeployment(); deploymentBuilder.addInputStream("HelloWorld.bpmn", inputStream_bpmn); deploymentBuilder.addInputStream("HelloWorld.png", inputStream_png); deploymentBuilder.deploy(); }
PS、Maven结构,对于src/main/java下的文件,默认只编译其中的.java文件,所以如果将流程图放在此文件夹下,则由于未包含而无法找到。
解决方法:右键工程->Build Path..->Configurate Build Path,选择Source视图,可看到其中src/main/java的Included值为**/*.java,表名只编译Java文件
选择后Edit,Remove其中的**/*.java,保存后变为all,则会编译所有文件。
(可通过Navigator导航视图查看工程下的target/classes文件夹,看编译后包含哪些文件)
查看数据库act_re_deployment(部署对象表),添加一行记录,act_re_procdef(流程定义表)也添加一行属性,后者通过DEPLOYMENT_ID_关联前者ID_.
(3)启动流程
//启动流程实例 @Test public void startProcessInstance(){ RuntimeService runtimeService = processEngine.getRuntimeService();//与正在执行的流程实例和执行对象相关的Service //通过流程key启动实例,优势在于存在同key值的流程,以最新版本优先,符合常规业务需求 ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(key);//key对应.bpmn文件中的流程Id属性值,也即act_re_procdef表中 的KEY_ System.out.println("流程实例ID:"+processInstance.getId());//5001 System.out.println("流程定义ID:"+processInstance.getProcessDefinitionId());//helloWorld:1:4 }查看act_ru_execution,添加了一条ID_为5001的数据,即为流程实例ID。
act_ru_task,添加了一条ID_为5004的数据,即为任务ID
(4)查询用户任务
//查询用户的当前个人任务 @Test public void getUserTaskList(){ String userId = "A"; TaskService taskService = processEngine.getTaskService();//任务管理相关 TaskQuery taskQuery = taskService.createTaskQuery();//创建任务查询对象 List<Task> list = taskQuery.taskAssignee(userId).list();//通过用户ID查询持有任务 if(null!=list && list.size()>0){ for(Task task:list){ System.out.println("任务ID:"+task.getId()); System.out.println("任务名称:"+task.getName()); System.out.println("任务创建时间:"+task.getCreateTime()); System.out.println("任务办理人:"+task.getAssignee()); System.out.println("流程实例ID:"+task.getProcessInstanceId()); System.out.println("执行对象ID:"+task.getExecutionId()); System.out.println("流程定义ID:"+task.getProcessDefinitionId()); } } }
输出如图:
(5)完成任务
//完成任务 @Test public void completeTask(){ String taskId = "5004"; TaskService taskService = processEngine.getTaskService();//任务管理相关 taskService.complete(taskId);//完成任务 System.out.println("完成任务,任务ID:"+taskId); }完成流程的一个环节,其他同理。当执行到流程结束时,act_ru_task不再有记录。
PS.act_hi_taskinst表有过往任务记录(包括任务开始时间,结束时间),act_hi_procinst有流程实例记录。