(三)Activiti之第一个程序以及Activiti插件的使用和Activiti表的解释
一、案例
1.1 建立Activiti Diagram图
- new -》 activiti -》Activiti Diagram,创建一个HelloWorld文件,后缀自动为bpmn,如下图定义好流程之后,保存该bpmn文件就会生成下图中的png图片
- 用xml打开该bmpn文件,有
<?xml version="1.0" encoding="UTF-8"?> <definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.activiti.org/test"> <process id="myFirstProcess" name="My First process" isExecutable="true"> <startEvent id="startevent1" name="Start"></startEvent> <userTask id="usertask1" name="用户节点" activiti:assignee="shyroke"></userTask> <endEvent id="endevent1" name="End"></endEvent> <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow> <sequenceFlow id="flow2" sourceRef="usertask1" targetRef="endevent1"></sequenceFlow> </process> <bpmndi:BPMNDiagram id="BPMNDiagram_myFirstProcess"> <bpmndi:BPMNPlane bpmnElement="myFirstProcess" id="BPMNPlane_myFirstProcess"> <bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1"> <omgdc:Bounds height="35.0" width="35.0" x="340.0" y="40.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1"> <omgdc:Bounds height="55.0" width="105.0" x="310.0" y="140.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1"> <omgdc:Bounds height="35.0" width="35.0" x="340.0" y="280.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1"> <omgdi:waypoint x="357.0" y="75.0"></omgdi:waypoint> <omgdi:waypoint x="362.0" y="140.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2"> <omgdi:waypoint x="362.0" y="195.0"></omgdi:waypoint> <omgdi:waypoint x="357.0" y="280.0"></omgdi:waypoint> </bpmndi:BPMNEdge> </bpmndi:BPMNPlane> </bpmndi:BPMNDiagram> </definitions>
1.2 部署流程定义
package com.shyroke.activiti.firstActiviti; import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngines; import org.activiti.engine.repository.Deployment; import org.junit.Test; /** * HelloWorld 工作流 * * @author 半颗柠檬、 * */ public class HelloWorldProcess { /** * 获取默认流程引擎实例,会自动读取activiti.cfg.xml文件 */ private ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); /** * 部署流程定义 */ @Test public void test_Deploy() { Deployment deployment=processEngine.getRepositoryService() // 获取部署相关Service .createDeployment() //// 创建部署 .addClasspathResource("diagrams/HelloWorld.bpmn") // 加载资源文件 .addClasspathResource("diagrams/HelloWorld.png") // 加载资源文件 .name("HelloWorld流程") //流程名称 .deploy(); //部署 System.out.println("流程部署ID:"+deployment.getId()); System.out.println("流程部署Name:"+deployment.getName()); } }
- 结果:
- 执行完这步之后,act_re_deployment表会新增一条部署记录
- 也会新增act_re_procdef流程定义表(processDefine)
- 新增资源文件表act_ge_bytearray
- 更新系统配置表 act_ge_property
1.3 启动流程实例
package com.shyroke.activiti.firstActiviti; import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngines; import org.activiti.engine.repository.Deployment; import org.activiti.engine.runtime.ProcessInstance; import org.junit.Test; /** * HelloWorld 工作流 * * @author 半颗柠檬、 * */ public class HelloWorldProcess { /** * 获取默认流程引擎实例,会自动读取activiti.cfg.xml文件 */ private ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); /** * 启动流程实例 */ @Test public void test_proceinst() { ProcessInstance instance=processEngine.getRuntimeService() // 运行时Service .startProcessInstanceByKey("myFirstProcess"); // 流程定义表act_re_procdef的KEY字段值 System.out.println("流程实例ID:"+instance.getId()); System.out.println("流程定义ID:"+instance.getProcessDefinitionId()); } }
- 执行用例结果:
这里的id是系统配置表 act_ge_property中id生成的。
- 执行完这步之后,流程实例运行时 执行对象表act_ru_execution
- 流程实例运行时 身份联系表act_ru_identitylink
- 流程实例运行时 用户任务表act_ru_task
- 活动节点历史表act_hi_actinst:
- 身份联系表 历史act_hi_identitylink
- 流程实例表 历史act_hi_procinst
- 历史任务表act_hi_taskinst
1.4 查看任务
package com.shyroke.activiti.firstActiviti; import java.util.List; import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngines; import org.activiti.engine.repository.Deployment; import org.activiti.engine.runtime.ProcessInstance; import org.activiti.engine.task.Task; import org.junit.Test; /** * HelloWorld 工作流 * * @author 半颗柠檬、 * */ public class HelloWorldProcess { /** * 获取默认流程引擎实例,会自动读取activiti.cfg.xml文件 */ private ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();/** * 查看任务 */ @Test public void find_task() { List<Task> taskList=processEngine.getTaskService().createTaskQuery().taskAssignee("shyroke").list(); for(Task task:taskList){ 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()); } } }
- 执行测试用例及结果
1.5 完成任务
package com.shyroke.activiti.firstActiviti; import java.util.List; import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngines; import org.activiti.engine.repository.Deployment; import org.activiti.engine.runtime.ProcessInstance; import org.activiti.engine.task.Task; import org.junit.Test; /** * HelloWorld 工作流 * * @author 半颗柠檬、 * */ public class HelloWorldProcess { /** * 获取默认流程引擎实例,会自动读取activiti.cfg.xml文件 */ private ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); /** * 完成任务 */ @Test public void test_completeTask() { processEngine.getTaskService().complete("2504"); } }
- 执行测试用例,结果
- #运行时的表以act_ru开头的表(如下图)的数据全部清空
- # 历史表以act_hi开头的表表数据修改 或者增加了数据