Java_Activiti 5 의 가 볍 고 간단 한 입문(1)

16995 단어 자바Activiti
 
// 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 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://activiti.org/bpmn20" id="definitions">
  <process id="vacationRequest" name="Vacation request" isExecutable="true">
    <startEvent id="request" activiti:initiator="employeeName">
      <extensionElements>
        <activiti:formProperty id="numberOfDays" name="Number of days" type="long" required="true"></activiti:formProperty>
        <activiti:formProperty id="startDate" name="First day of holiday (dd-MM-yyy)" type="date" datePattern="dd-MM-yyyy hh:mm" required="true"></activiti:formProperty>
        <activiti:formProperty id="vacationMotivation" name="Motivation" type="string"></activiti:formProperty>
      </extensionElements>
    </startEvent>
    <sequenceFlow id="flow1" sourceRef="request" targetRef="handleRequest"></sequenceFlow>
    <userTask id="handleRequest" name="Handle vacation request" activiti:candidateGroups="management">
      <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>
          <activiti:value id="false" name="Reject"></activiti:value>
        </activiti:formProperty>
        <activiti:formProperty id="managerMotivation" name="Motivation" type="string"></activiti:formProperty>
      </extensionElements>
    </userTask>
    <sequenceFlow id="flow2" sourceRef="handleRequest" targetRef="requestApprovedDecision"></sequenceFlow>
    <exclusiveGateway id="requestApprovedDecision" name="Request approved?"></exclusiveGateway>
    <sequenceFlow id="flow3" sourceRef="requestApprovedDecision" targetRef="sendApprovalMail">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${vacationApproved == 'true'}]]></conditionExpression>
    </sequenceFlow>
    <manualTask id="sendApprovalMail" name="Send confirmation e-mail"></manualTask>
    <sequenceFlow id="flow4" sourceRef="sendApprovalMail" targetRef="theEnd1"></sequenceFlow>
    <endEvent id="theEnd1"></endEvent>
    <sequenceFlow id="flow5" sourceRef="requestApprovedDecision" targetRef="adjustVacationRequestTask">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${vacationApproved == 'false'}]]></conditionExpression>
    </sequenceFlow>
    <userTask id="adjustVacationRequestTask" name="Adjust vacation request" activiti:assignee="${employeeName}">
      <documentation>Your manager has disapproved your vacation request for ${numberOfDays} days.
        Reason: ${managerMotivation}</documentation>
      <extensionElements>
        <activiti:formProperty id="numberOfDays" name="Number of days" type="long" required="true"></activiti:formProperty>
        <activiti:formProperty id="startDate" name="First day of holiday (dd-MM-yyy)" type="date" datePattern="dd-MM-yyyy hh:mm" required="true"></activiti:formProperty>
        <activiti:formProperty id="vacationMotivation" name="Motivation" type="string"></activiti:formProperty>
        <activiti:formProperty id="resendRequest" name="Resend vacation request to manager?" type="enum" required="true">
          <activiti:value id="true" name="Yes"></activiti:value>
          <activiti:value id="false" name="No"></activiti:value>
        </activiti:formProperty>
      </extensionElements>
    </userTask>
    <sequenceFlow id="flow6" sourceRef="adjustVacationRequestTask" targetRef="resendRequestDecision"></sequenceFlow>
    <exclusiveGateway id="resendRequestDecision" name="Resend request?"></exclusiveGateway>
    <sequenceFlow id="flow7" sourceRef="resendRequestDecision" targetRef="handleRequest">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${resendRequest == 'true'}]]></conditionExpression>
    </sequenceFlow>
    <sequenceFlow id="flow8" sourceRef="resendRequestDecision" targetRef="theEnd2">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${resendRequest == 'false'}]]></conditionExpression>
    </sequenceFlow>
    <endEvent id="theEnd2"></endEvent>
  </process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_vacationRequest">
    <bpmndi:BPMNPlane bpmnElement="vacationRequest" id="BPMNPlane_vacationRequest">
      <bpmndi:BPMNShape bpmnElement="request" id="BPMNShape_request">
        <omgdc:Bounds height="35.0" width="35.0" x="0.0" y="178.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="handleRequest" id="BPMNShape_handleRequest">
        <omgdc:Bounds height="60.0" width="100.0" x="80.0" y="163.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="requestApprovedDecision" id="BPMNShape_requestApprovedDecision">
        <omgdc:Bounds height="40.0" width="40.0" x="230.0" y="114.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="sendApprovalMail" id="BPMNShape_sendApprovalMail">
        <omgdc:Bounds height="60.0" width="100.0" x="320.0" y="0.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="theEnd1" id="BPMNShape_theEnd1">
        <omgdc:Bounds height="35.0" width="35.0" x="480.0" y="12.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="adjustVacationRequestTask" id="BPMNShape_adjustVacationRequestTask">
        <omgdc:Bounds height="60.0" width="100.0" x="320.0" y="160.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="resendRequestDecision" id="BPMNShape_resendRequestDecision">
        <omgdc:Bounds height="40.0" width="40.0" x="280.0" y="272.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="theEnd2" id="BPMNShape_theEnd2">
        <omgdc:Bounds height="35.0" width="35.0" x="283.0" y="370.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
        <omgdi:waypoint x="35.0" y="195.0"></omgdi:waypoint>
        <omgdi:waypoint x="80.0" y="193.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
        <omgdi:waypoint x="180.0" y="193.0"></omgdi:waypoint>
        <omgdi:waypoint x="192.0" y="180.0"></omgdi:waypoint>
        <omgdi:waypoint x="192.0" y="134.0"></omgdi:waypoint>
        <omgdi:waypoint x="230.0" y="134.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3">
        <omgdi:waypoint x="270.0" y="134.0"></omgdi:waypoint>
        <omgdi:waypoint x="282.0" y="134.0"></omgdi:waypoint>
        <omgdi:waypoint x="282.0" y="30.0"></omgdi:waypoint>
        <omgdi:waypoint x="320.0" y="30.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow4" id="BPMNEdge_flow4">
        <omgdi:waypoint x="420.0" y="30.0"></omgdi:waypoint>
        <omgdi:waypoint x="432.0" y="30.0"></omgdi:waypoint>
        <omgdi:waypoint x="432.0" y="30.0"></omgdi:waypoint>
        <omgdi:waypoint x="480.0" y="29.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow5" id="BPMNEdge_flow5">
        <omgdi:waypoint x="270.0" y="134.0"></omgdi:waypoint>
        <omgdi:waypoint x="282.0" y="134.0"></omgdi:waypoint>
        <omgdi:waypoint x="282.0" y="190.0"></omgdi:waypoint>
        <omgdi:waypoint x="320.0" y="190.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow6" id="BPMNEdge_flow6">
        <omgdi:waypoint x="370.0" y="220.0"></omgdi:waypoint>
        <omgdi:waypoint x="370.0" y="265.0"></omgdi:waypoint>
        <omgdi:waypoint x="370.0" y="292.0"></omgdi:waypoint>
        <omgdi:waypoint x="320.0" y="292.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow7" id="BPMNEdge_flow7">
        <omgdi:waypoint x="280.0" y="292.0"></omgdi:waypoint>
        <omgdi:waypoint x="130.0" y="291.0"></omgdi:waypoint>
        <omgdi:waypoint x="130.0" y="253.0"></omgdi:waypoint>
        <omgdi:waypoint x="130.0" y="223.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow8" id="BPMNEdge_flow8">
        <omgdi:waypoint x="300.0" y="312.0"></omgdi:waypoint>
        <omgdi:waypoint x="300.0" y="370.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</definitions>

좋은 웹페이지 즐겨찾기