所有语言的第一个程序都叫helloworld,姑且也称这第一个activiti程序为helloworld。
一个工作流想要实现,必定有一个对应的部署文件,利用流程设计器设计一个简单的流程,请假->部门经理审批->总经理审批。生成的部署文件如下
<?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: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="helloworld" name="helloworldprocess" isExecutable="true"> <startEvent id="startevent1" name="Start"></startEvent> <endEvent id="endevent1" name="End"></endEvent> <userTask id="usertask1" name="申请请假" activiti:assignee="张三"></userTask> <userTask id="usertask2" name="部门审批" activiti:assignee="李四"></userTask> <userTask id="usertask3" name="经理审批" activiti:assignee="王五"></userTask> <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow> <sequenceFlow id="flow2" sourceRef="usertask1" targetRef="usertask2"></sequenceFlow> <sequenceFlow id="flow3" sourceRef="usertask2" targetRef="usertask3"></sequenceFlow> <sequenceFlow id="flow4" sourceRef="usertask3" targetRef="endevent1"></sequenceFlow> </process> <bpmndi:BPMNDiagram id="BPMNDiagram_helloworld"> <bpmndi:BPMNPlane bpmnElement="helloworld" id="BPMNPlane_helloworld"> <bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1"> <omgdc:Bounds height="35.0" width="35.0" x="400.0" y="20.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1"> <omgdc:Bounds height="35.0" width="35.0" x="400.0" y="440.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1"> <omgdc:Bounds height="55.0" width="105.0" x="365.0" y="100.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="usertask2" id="BPMNShape_usertask2"> <omgdc:Bounds height="55.0" width="105.0" x="365.0" y="210.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="usertask3" id="BPMNShape_usertask3"> <omgdc:Bounds height="55.0" width="105.0" x="365.0" y="340.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1"> <omgdi:waypoint x="417.0" y="55.0"></omgdi:waypoint> <omgdi:waypoint x="417.0" y="100.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2"> <omgdi:waypoint x="417.0" y="155.0"></omgdi:waypoint> <omgdi:waypoint x="417.0" y="210.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3"> <omgdi:waypoint x="417.0" y="265.0"></omgdi:waypoint> <omgdi:waypoint x="417.0" y="340.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="flow4" id="BPMNEdge_flow4"> <omgdi:waypoint x="417.0" y="395.0"></omgdi:waypoint> <omgdi:waypoint x="417.0" y="440.0"></omgdi:waypoint> </bpmndi:BPMNEdge> </bpmndi:BPMNPlane> </bpmndi:BPMNDiagram> </definitions>
在helloworld中,首先部署好该流程图,代码如下
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); /**部署流程定义*/ @Test public void deploymentProcessDefinition() { Deployment deployment = processEngine.getRepositoryService()//与流程定义和部署相关的service .createDeployment()//创建一个部署对象 .name("helloword入门")//添加部署名称 .addClasspathResource("diagrams/helloworld.bpmn")//从calsspath中加载,一次一个 .addClasspathResource("diagrams/helloworld.png") .deploy();//完成部署 System.out.println(deployment.getId()); System.out.println(deployment.getName()); }
这样在数据库act_ge_bytearray,act_re_procdef中可以看到有数据插入,表明部署已经完成。
接下来就是要启动流程,流程启动代码如下
/**启动流程实例*/ @Test public void startProcessInstance() { String processDefinitionKey = "helloworld"; ProcessInstance pi = processEngine.getRuntimeService()//正在执行的流程实例和执行对象相关的service .startProcessInstanceByKey(processDefinitionKey);//使用流程定义的key启动流程实例,key对应helloworld.bpnm文件中的key 默认按最新的启动 System.out.println(pi.getId());//流程实例id 101 System.out.println(pi.getProcessDefinitionId());//流程定义id helloworld:1:4 }
可以在act_ru_task中看到这条数据,表明流程已经启动
流程已经启动,必然要开始处理,因此要能查看个人任务,代码如下
/**查询个人任务*/ @Test public void findMypersonalTask() { String assignee = "张三"; List<Task> list = processEngine.getTaskService()//正在执行任务相关的service .createTaskQuery()//创建任务查询对象 .taskAssignee(assignee)//指定个人任务查询 办理人 .list();//返回列表 if(list!=null&&list.size()>-1) { 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()); System.out.println("########################################################"); } } }
此处查询所有签收人为张三的任务。
查询到个人代办业务后就要开始处理业务,代码如下
/**完成我的任务*/ @Test public void completeMyPersonalTask() { String taskId = "104"; processEngine.getTaskService()//正在进行任务 .complete(taskId); System.out.println("完成任务,任务id为"+taskId); }
完成任务后 act_ru_task中数据会产生变化,表明流程已经进入下个阶段。