1 // VacationRequest.java
2
3 /**
4 * author : 冯孟活 ^_^
5 * dates : 2015年9月1日 下午10:32:58
6 * class : 演示简单的公司请假流程
7 *
8 * 一个简单的流程分三个步骤:
9 * 1、发布流程(部署流程定义)
10 * 2、启动流程实例
11 * 3、完成任务(先查询任务,后完成任务)
12 * 4、挂起、激活一个流程实例(可选)
13 */
14 public class VacationRequest {
15 public static void main(String[] args) {
16
17 /**
18 * 第一步:发布流程
19 */
20 ProcessEngine processEngine = ProcessEngineConfiguration // 通过流程引擎配置类来创建流程引擎
21 .createProcessEngineConfigurationFromResource("activiti.cfg.xml").buildProcessEngine();
22 RepositoryService repositoryService = processEngine.getRepositoryService(); // 通过流程引擎来得到知识库服务
23 repositoryService.createDeployment().addClasspathResource("VacationRequest.bpmn").deploy(); // 通过只是库部署流程定义
24 System.out.println("流程定义的个数="+repositoryService.createDeploymentQuery().count()); // 查询所有发布的流程定义的个数
25
26 /**
27 * 第二步:启动一个流程实例
28 */
29 /*定义Map来存放流程变量:流程变量经常会被用到,因为他们赋予来自同一个流程定义的不同流程实例
30 的特别含义,简单来说,流程变量是区分流程实例的关键
31 */
32 Map<String, Object> variables = new HashMap<>(); // 定义一个Map来存放流程变量
33 variables.put("employeeName","Kermit");
34 variables.put("numberOfDays",new Integer(4));
35 variables.put("vacationMotivation","I'm really tired!");
36 RuntimeService runtimeService = processEngine.getRuntimeService(); // 获取运行服务
37 runtimeService.startProcessInstanceByKey("vacationRequest",variables); // 通过运行服务来启动流程实例,并且设置流程变量(通过key 或者 id 部署都可以)
38 System.out.println("流程实例的个数="+runtimeService.createProcessInstanceQuery().count()); // 通过运行服务来查询所有的流程实例的个数
39
40 /**
41 * 第三部:完成任务
42 */
43 TaskService taskService = processEngine.getTaskService(); // 通过流程引擎获取任务服务
44 List<Task> tasks = taskService.createTaskQuery().taskCandidateGroup("management").list(); // 通过任务服务来查询任务候选组(这是通过组来分区)
45 for (Task task : tasks) { // 遍历打印任务
46 System.err.println("能找到的任务="+task.getName());
47 }
48 Task task = tasks.get(0); // 获取第一个任务
49 Map<String,Object> taskVariables = new HashMap<>(); // 定义一个Map来存放任务变量
50 taskVariables.put("vacationApproved","false");
51 taskVariables.put("managerMotivation","We have a tight deadline!");
52 taskService.complete(task.getId(),taskVariables); // 根据Id来完成任务
53
54 /**
55 * 挂起,激活一个流程
56 */
57 /*
58 * 我们可以挂起一个流程定义。当挂起流程定义时, 就不能创建新流程了(会抛出一个异常)。
59 * 可以通过RepositoryService挂起一个流程:
60 */
61 //repositoryService.suspendProcessDefinitionByKey("vacationRequest"); // 挂起一个流程定义
62 //try{
63 //runtimeService.startProcessInstanceByKey("vacationRequest"); // 启动一个流程实例
64 //}catch(ActivitiException e){ // 这里会抛出一个Activiti自定义异常
65 //e.printStackTrace();
66 //}
67
68 /*
69 * 备注下:
70 * 也可以挂起一个流程实例。挂起时,流程不能继续执行(比如,完成任务会抛出异常),
71 * 异步操作(比如定时器)也不会执行。 挂起流程实例可以调用 runtimeService.suspendProcessInstance方法。
72 * 激活流程实例可以调用runtimeService.activateProcessInstanceXXX方法。
73 */
74 }
75 }
1 <!-- activiti.cfg.xml -->
2
3 <?xml version="1.0" encoding="UTF-8"?>
4 <beans xmlns="http://www.springframework.org/schema/beans"
5 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6 xsi:schemaLocation="http://www.springframework.org/schema/beans
7 http://www.springframework.org/schema/beans/spring-beans.xsd">
8
9 <bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
10 <property name="databaseSchemaUpdate" value="update"/>
11 <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/db_activiti?useUnicode=true&characterEncoding=utf-8"/>
12 <property name="jdbcDriver" value="com.mysql.jdbc.Driver"/>
13 <property name="jdbcUsername" value="root"/>
14 <property name="jdbcPassword" value="root"/>
15 <property name="jobExecutorActivate" value="true"/>
16 </bean>
17
18 </beans>
1 <!-- VacationRequest.bpmn -->
2 <?xml version="1.0" encoding="UTF-8" ?>
3 <definitions id="definitions"
4 targetNamespace="http://activiti.org/bpmn20"
5 xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"
6 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
7 xmlns:activiti="http://activiti.org/bpmn">
8
9 <process id="vacationRequest" name="Vacation request">
10
11 <startEvent id="request" activiti:initiator="employeeName">
12 <extensionElements>
13 <activiti:formProperty id="numberOfDays" name="Number of days" type="long" value="1" required="true"/>
14 <activiti:formProperty id="startDate" name="First day of holiday (dd-MM-yyy)" datePattern="dd-MM-yyyy hh:mm" type="date" required="true" />
15 <activiti:formProperty id="vacationMotivation" name="Motivation" type="string" />
16 </extensionElements>
17 </startEvent>
18 <sequenceFlow id="flow1" sourceRef="request" targetRef="handleRequest" />
19
20 <userTask id="handleRequest" name="Handle vacation request" >
21 <documentation>
22 ${employeeName} would like to take ${numberOfDays} day(s) of vacation (Motivation: ${vacationMotivation}).
23 </documentation>
24 <extensionElements>
25 <activiti:formProperty id="vacationApproved" name="Do you approve this vacation" type="enum" required="true">
26 <activiti:value id="true" name="Approve" />
27 <activiti:value id="false" name="Reject" />
28 </activiti:formProperty>
29 <activiti:formProperty id="managerMotivation" name="Motivation" type="string" />
30 </extensionElements>
31 <potentialOwner>
32 <resourceAssignmentExpression>
33 <formalExpression>management</formalExpression>
34 </resourceAssignmentExpression>
35 </potentialOwner>
36 </userTask>
37 <sequenceFlow id="flow2" sourceRef="handleRequest" targetRef="requestApprovedDecision" />
38
39 <exclusiveGateway id="requestApprovedDecision" name="Request approved?" />
40 <sequenceFlow id="flow3" sourceRef="requestApprovedDecision" targetRef="sendApprovalMail">
41 <conditionExpression xsi:type="tFormalExpression">${vacationApproved == 'true'}</conditionExpression>
42 </sequenceFlow>
43
44 <task id="sendApprovalMail" name="Send confirmation e-mail" />
45 <sequenceFlow id="flow4" sourceRef="sendApprovalMail" targetRef="theEnd1" />
46 <endEvent id="theEnd1" />
47
48 <sequenceFlow id="flow5" sourceRef="requestApprovedDecision" targetRef="adjustVacationRequestTask">
49 <conditionExpression xsi:type="tFormalExpression">${vacationApproved == 'false'}</conditionExpression>
50 </sequenceFlow>
51
52 <userTask id="adjustVacationRequestTask" name="Adjust vacation request">
53 <documentation>
54 Your manager has disapproved your vacation request for ${numberOfDays} days.
55 Reason: ${managerMotivation}
56 </documentation>
57 <extensionElements>
58 <activiti:formProperty id="numberOfDays" name="Number of days" value="${numberOfDays}" type="long" required="true"/>
59 <activiti:formProperty id="startDate" name="First day of holiday (dd-MM-yyy)" value="${startDate}" datePattern="dd-MM-yyyy hh:mm" type="date" required="true" />
60 <activiti:formProperty id="vacationMotivation" name="Motivation" value="${vacationMotivation}" type="string" />
61 <activiti:formProperty id="resendRequest" name="Resend vacation request to manager?" type="enum" required="true">
62 <activiti:value id="true" name="Yes" />
63 <activiti:value id="false" name="No" />
64 </activiti:formProperty>
65 </extensionElements>
66 <humanPerformer>
67 <resourceAssignmentExpression>
68 <formalExpression>${employeeName}</formalExpression>
69 </resourceAssignmentExpression>
70 </humanPerformer>
71 </userTask>
72 <sequenceFlow id="flow6" sourceRef="adjustVacationRequestTask" targetRef="resendRequestDecision" />
73
74 <exclusiveGateway id="resendRequestDecision" name="Resend request?" />
75 <sequenceFlow id="flow7" sourceRef="resendRequestDecision" targetRef="handleRequest">
76 <conditionExpression xsi:type="tFormalExpression">${resendRequest == 'true'}</conditionExpression>
77 </sequenceFlow>
78
79 <sequenceFlow id="flow8" sourceRef="resendRequestDecision" targetRef="theEnd2">
80 <conditionExpression xsi:type="tFormalExpression">${resendRequest == 'false'}</conditionExpression>
81 </sequenceFlow>
82 <endEvent id="theEnd2" />
83
84 </process>
85
86 </definitions>
没有最好,也没有最坏,一切习惯就好~_^!
JAVA交流群^_^:487566461
Activiti5交流群~_^:470165731
JBPM6交流群^_^:470118196
你的加入是我的荣耀,来吧,朋友:让我们一起交流和探讨!