Java_Activiti5_菜鸟也来学Activiti5工作流_之入门简单例子(一)

 // VacationRequest.java

 /**
* author : 冯孟活 ^_^
* dates : 2015年9月1日 下午10:32:58
* class : 演示简单的公司请假流程
*
* 一个简单的流程分三个步骤:
* 1、发布流程(部署流程定义)
* 2、启动流程实例
* 3、完成任务(先查询任务,后完成任务)
* 4、挂起、激活一个流程实例(可选)
*/
public class VacationRequest {
public static void main(String[] args) { /**
* 第一步:发布流程
*/
ProcessEngine processEngine = ProcessEngineConfiguration // 通过流程引擎配置类来创建流程引擎
.createProcessEngineConfigurationFromResource("activiti.cfg.xml").buildProcessEngine();
RepositoryService repositoryService = processEngine.getRepositoryService(); // 通过流程引擎来得到知识库服务
repositoryService.createDeployment().addClasspathResource("VacationRequest.bpmn").deploy(); // 通过只是库部署流程定义
System.out.println("流程定义的个数="+repositoryService.createDeploymentQuery().count()); // 查询所有发布的流程定义的个数 /**
* 第二步:启动一个流程实例
*/
/*定义Map来存放流程变量:流程变量经常会被用到,因为他们赋予来自同一个流程定义的不同流程实例
的特别含义,简单来说,流程变量是区分流程实例的关键
*/
Map<String, Object> variables = new HashMap<>(); // 定义一个Map来存放流程变量
variables.put("employeeName","Kermit");
variables.put("numberOfDays",new Integer(4));
variables.put("vacationMotivation","I'm really tired!");
RuntimeService runtimeService = processEngine.getRuntimeService(); // 获取运行服务
runtimeService.startProcessInstanceByKey("vacationRequest",variables); // 通过运行服务来启动流程实例,并且设置流程变量(通过key 或者 id 部署都可以)
System.out.println("流程实例的个数="+runtimeService.createProcessInstanceQuery().count()); // 通过运行服务来查询所有的流程实例的个数 /**
* 第三部:完成任务
*/
TaskService taskService = processEngine.getTaskService(); // 通过流程引擎获取任务服务
List<Task> tasks = taskService.createTaskQuery().taskCandidateGroup("management").list(); // 通过任务服务来查询任务候选组(这是通过组来分区)
for (Task task : tasks) { // 遍历打印任务
System.err.println("能找到的任务="+task.getName());
}
Task task = tasks.get(0); // 获取第一个任务
Map<String,Object> taskVariables = new HashMap<>(); // 定义一个Map来存放任务变量
taskVariables.put("vacationApproved","false");
taskVariables.put("managerMotivation","We have a tight deadline!");
taskService.complete(task.getId(),taskVariables); // 根据Id来完成任务 /**
* 挂起,激活一个流程
*/
/*
* 我们可以挂起一个流程定义。当挂起流程定义时, 就不能创建新流程了(会抛出一个异常)。
* 可以通过RepositoryService挂起一个流程:
*/
//repositoryService.suspendProcessDefinitionByKey("vacationRequest"); // 挂起一个流程定义
//try{
//runtimeService.startProcessInstanceByKey("vacationRequest"); // 启动一个流程实例
//}catch(ActivitiException e){ // 这里会抛出一个Activiti自定义异常
//e.printStackTrace();
//} /*
* 备注下:
* 也可以挂起一个流程实例。挂起时,流程不能继续执行(比如,完成任务会抛出异常),
* 异步操作(比如定时器)也不会执行。 挂起流程实例可以调用 runtimeService.suspendProcessInstance方法。
* 激活流程实例可以调用runtimeService.activateProcessInstanceXXX方法。
*/
}
}
 <!-- activiti.cfg.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
<property name="databaseSchemaUpdate" value="update"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/db_activiti?useUnicode=true&amp;characterEncoding=utf-8"/>
<property name="jdbcDriver" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUsername" value="root"/>
<property name="jdbcPassword" value="root"/>
<property name="jobExecutorActivate" value="true"/>
</bean> </beans>
 <!-- VacationRequest.bpmn -->
<?xml version="1.0" encoding="UTF-8" ?>
<definitions id="definitions"
targetNamespace="http://activiti.org/bpmn20"
xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:activiti="http://activiti.org/bpmn"> <process id="vacationRequest" name="Vacation request"> <startEvent id="request" activiti:initiator="employeeName">
<extensionElements>
<activiti:formProperty id="numberOfDays" name="Number of days" type="long" value="1" required="true"/>
<activiti:formProperty id="startDate" name="First day of holiday (dd-MM-yyy)" datePattern="dd-MM-yyyy hh:mm" type="date" required="true" />
<activiti:formProperty id="vacationMotivation" name="Motivation" type="string" />
</extensionElements>
</startEvent>
<sequenceFlow id="flow1" sourceRef="request" targetRef="handleRequest" /> <userTask id="handleRequest" name="Handle vacation request" >
<documentation>
${employeeName} would like to take ${numberOfDays} day(s) of vacation (Motivation: ${vacationMotivation}).
</documentation>
<extensionElements>
<activiti:formProperty id="vacationApproved" name="Do you approve this vacation" type="enum" required="true">
<activiti:value id="true" name="Approve" />
<activiti:value id="false" name="Reject" />
</activiti:formProperty>
<activiti:formProperty id="managerMotivation" name="Motivation" type="string" />
</extensionElements>
<potentialOwner>
<resourceAssignmentExpression>
<formalExpression>management</formalExpression>
</resourceAssignmentExpression>
</potentialOwner>
</userTask>
<sequenceFlow id="flow2" sourceRef="handleRequest" targetRef="requestApprovedDecision" /> <exclusiveGateway id="requestApprovedDecision" name="Request approved?" />
<sequenceFlow id="flow3" sourceRef="requestApprovedDecision" targetRef="sendApprovalMail">
<conditionExpression xsi:type="tFormalExpression">${vacationApproved == 'true'}</conditionExpression>
</sequenceFlow> <task id="sendApprovalMail" name="Send confirmation e-mail" />
<sequenceFlow id="flow4" sourceRef="sendApprovalMail" targetRef="theEnd1" />
<endEvent id="theEnd1" /> <sequenceFlow id="flow5" sourceRef="requestApprovedDecision" targetRef="adjustVacationRequestTask">
<conditionExpression xsi:type="tFormalExpression">${vacationApproved == 'false'}</conditionExpression>
</sequenceFlow> <userTask id="adjustVacationRequestTask" name="Adjust vacation request">
<documentation>
Your manager has disapproved your vacation request for ${numberOfDays} days.
Reason: ${managerMotivation}
</documentation>
<extensionElements>
<activiti:formProperty id="numberOfDays" name="Number of days" value="${numberOfDays}" type="long" required="true"/>
<activiti:formProperty id="startDate" name="First day of holiday (dd-MM-yyy)" value="${startDate}" datePattern="dd-MM-yyyy hh:mm" type="date" required="true" />
<activiti:formProperty id="vacationMotivation" name="Motivation" value="${vacationMotivation}" type="string" />
<activiti:formProperty id="resendRequest" name="Resend vacation request to manager?" type="enum" required="true">
<activiti:value id="true" name="Yes" />
<activiti:value id="false" name="No" />
</activiti:formProperty>
</extensionElements>
<humanPerformer>
<resourceAssignmentExpression>
<formalExpression>${employeeName}</formalExpression>
</resourceAssignmentExpression>
</humanPerformer>
</userTask>
<sequenceFlow id="flow6" sourceRef="adjustVacationRequestTask" targetRef="resendRequestDecision" /> <exclusiveGateway id="resendRequestDecision" name="Resend request?" />
<sequenceFlow id="flow7" sourceRef="resendRequestDecision" targetRef="handleRequest">
<conditionExpression xsi:type="tFormalExpression">${resendRequest == 'true'}</conditionExpression>
</sequenceFlow> <sequenceFlow id="flow8" sourceRef="resendRequestDecision" targetRef="theEnd2">
<conditionExpression xsi:type="tFormalExpression">${resendRequest == 'false'}</conditionExpression>
</sequenceFlow>
<endEvent id="theEnd2" /> </process> </definitions>

Java_Activiti5_菜鸟也来学Activiti5工作流_之入门简单例子(一)

上一篇:uwsgi_read_timeout超时处理


下一篇:ios按钮点击时的灰色框