Java_Activiti5 _ a simple example of getting started with Activiti5 workflow _ (1): activiti5 workflow tutorial

Source: Internet
Author: User

Java_Activiti5 _ a simple example of getting started with Activiti5 workflow _ (1): activiti5 workflow tutorial

1 // VacationRequest. java 2 3/** 4 * author: Feng Meng live 1__^ 5 * dates: 10:32:58, January 1, September 1, 2015 * class: demonstrate simple company leave Process 7*8 * a simple process consists of three steps: 9*1. Release Process (deployment process definition) 10*2. Start Process instance 11*3. Complete the task (query the task first and then complete the task) 12*4. Suspend or activate a process instance (optional) 13 */14 public class VacationRequest {15 public static void main (String [] args) {16 17/** 18 * Step 1: release Process 19 */20 ProcessEngine processEngine = ProcessEngineConfiguration // use the process engine configuration class to create a process engine 21. CreateProcessEngineConfigurationFromResource ("activiti. cfg. xml "). buildProcessEngine (); 22 RepositoryService repositoryService = processEngine. getRepositoryService (); // obtain the knowledge base service 23 repositoryService through the process engine. createDeployment (). addClasspathResource ("VacationRequest. bpmn "). deploy (); // defines 24 System by just using the database deployment process. out. println ("Number of Process Definitions =" + repositoryService. createDeploymentQuery (). count (); // query the number of all published Process Definitions 25 26 /** 27 * Step 2: Start a process instance 28 */29/* to define Map to store process variables: process variables are often used, because they give special meaning to different process instance 30 defined by the same process, simply put, the process variable is the key to distinguishing the process instance 31 */32 Map <String, object> variables = new HashMap <> (); // defines a Map to store the flow variable 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 (); // get the running service 37 runtimeService. startProcessInstanceByKey ("vacationRequest", variables); // start the process instance by running the service, and set the process variable (either by key or id deployment) 38 System. out. println ("Number of process instances =" + runtimeService. createProcessInstanceQuery (). count (); // query the number of all process instances by running the service 39 40/** 41 * Part 3: complete task 42 */43 TaskService taskService = processEngine. getTaskService (); // pass Obtain the Task Service 44 List <Task> tasks = taskService through the process engine. createTaskQuery (). taskCandidateGroup ("management "). list (); // query the Task candidate group through the task Service (which is partitioned by the Group) 45 for (Task task: tasks) {// print the print Task 46 System. err. println ("task to be found =" + task. getName (); 47} 48 Task task = tasks. get (0); // get the first task 49 Map <String, Object> taskVariables = new HashMap <> (); // define a Map to store the task variable 50 taskVariables. put ("vacationApproved", "false"); 51 taskVariables. pu T ("managerMotivation", "We have a tight deadline! "); 52 taskService. complete (task. getId (), taskVariables); // complete task 53 54/** 55 * suspension based on the Id, to activate a process 56 */57/* 58 *, we can suspend a process definition. When the process definition is suspended, a new process cannot be created (an exception is thrown ). 59 * you can use the RepositoryService to suspend a process: 60 */61 // repositoryService. suspendProcessDefinitionByKey ("vacationRequest"); // suspend a process definition 62 // try {63 // runtimeService. startProcessInstanceByKey ("vacationRequest"); // start a process instance 64 //} catch (ActivitiException e) {// an Activiti custom exception will be thrown here 65 // e. printStackTrace (); 66 //} 67 68/* 69 * Note: 70 * can also suspend a process instance. When a task is suspended, the process cannot continue to be executed (for example, an exception is thrown when the task is completed), and 71 * asynchronous operations (such as timers) are not executed. To suspend a process instance, you can call the runtimeService. suspendProcessInstance method. 72 * The runtimeService. activateProcessInstanceXXX method can be called to activate a process instance. 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&amp;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       

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.