(2) the java program goes through the workflow activiti and the workflow activiti

Source: Internet
Author: User

(2) the java program goes through the workflow activiti and the workflow activiti

The workflow completes the execution steps from process definition to creating a process instance.

The above is the execution step of the simplest process to be executed from top to bottom. This chapter uses the Code as an example. There are not many codes, but they are all executed once, you will have a rough understanding of the entire activiti process.


  1,The first step is to use the eclipse plug-in activiti-designer to draw a simple flowchart (Here we only use three components startEvent, endEvent, and UserTask, so it is relatively simple)



The flowchart is as follows:


When you click Create purchase order, define an id and name for the node (the same applies to the following nodes)





Click mainConfig on the left and enter a name in Assingee to set the handler for the process. (Of course, another dynamic acquisition method will be used in the actual program, this is just a convenient way to understand the overall process)

In the following example, set the handler for each node.




Click the blank area to set the id and name of the entire process.


After clicking here to save, The purchasingflow.png will be generated in the same directory. The simple process is completed here.


2. Use the activiti api to upload the previously defined workflow bpmn file and png file to the activiti database.

1/* 2 * Get the process engine object. The following method will find the name under the classpath directory as activi by default. cfg. in xml 3 *, the bean id is the processEngineConfiguration ProcessEngineConfiguration engine configuration object to obtain the ProcessEngine object 4 */5 private ProcessEngine engine = ProcessEngines. getdefaprocesprocessengine (); 6 7 // deploy a process 8 @ Test 9 public void deployProcessDefinition () {10 // obtain the resource service11 RepositoryService repositoryService = engine based on the engine. getRepositoryService (); 12 // deploy the bpmn file 13 String bpmnName = "purchasingflow. bpmn "; 14 InputStream bpmnIn = this. getClass (). getClassLoader (). getResourceAsStream ("digoal/purchasingflow. bpmn "); 15 // deploy the image 16 String pngName =" purchasingflow.png "; 17 InputStream pngIn = this. getClass (). getClassLoader (). getResourceAsStream ("dimo-/ purchasingflow.png"); 18 // Add these two files for Deployment 19 deployment Deployment = repositoryService. createDeployment () 20. addInputStream (bpmnName, bpmnIn) 21. addInputStream (pngName, pngIn) 22. deploy (); 23 24 System. out. println ("deployment id:" + deployment. getId (); 25 System. out. println ("name of deployment:" + deployment. getDeploymentTime (); 26 27}

The Simple api above deploys the procurement process definition to our activiti database. Open the database act_re_procdef to query the process definition we just uploaded.


  3,Before starting a process instance, we will repeat the above process definition. Then we will query the process definition based on the process definition id (purchasingflow) to see what will happen.

1 // query Process Definition 2 @ Test 3 public void queryProcessDefinition () {4 5 RepositoryService repositoryService = engine. getRepositoryService (); 6 // create a process definition query object 7 ProcessDefinitionQuery definitionQuery = repositoryService. createProcessDefinitionQuery (); 8 9 String processDefinitionKey = "purchasingflow"; 10 // set the query condition 11 definitionQuery for the key defined by the process. processDefinitionKey (processDefinitionKey); 12 // query all Process Definitions 13 List <ProcessDefinition> processDefinitionList = definitionQuery. list (); 14 for (ProcessDefinition definition: processDefinitionList) {15 System. out. println ("-------------------------"); 16 System. out. println ("process definition id:" + definition. getId (); 17 System. out. println ("Process Resource Name:" + definition. getResourceName (); 18 System. out. println ("process deployment id:" + definition. getDeploymentId (); 19} 20 21 22}

The above results output three process definitions with different deployment IDs and different process definitions. With this question, we will start a process instance and explain it in detail below.


  4,Start a process instance

1 // start a process instance 2 @ Test 3 public void startProcessInstance () {4 5 RuntimeService runtimeService = engine. getRuntimeService (); 6 7 String processDefinitionKey = "purchasingflow"; 8 // start a process instance 9 ProcessInstance processInstance = runtimeService according to the key defined in the process. startProcessInstanceByKey (processDefinitionKey); 10 System. out. println ("process instance id:" + processInstance. getId (); 11 System. out. println ("process definition id:" + processInstance. getProcessDefinitionId (); 12 13 14}

We found that the id of the printed process definition is the process definition of our last upload, So we come to the conclusion that,Upload the process definition with the same id Multiple times. Start the process instance according to the process definition, and get the latest version of the process definition.


   5,Query the task of the current processor. We previously wrote the first node when defining the process (that is, the processor who created the purchase order is zhangsan)

1 // query the current user's agent task 2 @ Test 3 public void queryProcessInstance () {4 5 // query the service 6 TaskService taskService = engine used by the task. getTaskService (); 7 // get the task query object 8 TaskQuery taskQuery = taskService. createTaskQuery (); 9 taskQuery. taskAssignee ("zhangsan"); 10 // query all tasks under this condition in the 11 List <Task> tasks = taskQuery. list (); 12 for (Task task: tasks) {13 System. out. println ("current task id:" + task. getId (); 14 System. out. println ("process definition id of the current task:" + task. getProcessDefinitionId (); 15 System. out. println ("key of the current task:" + task. getTaskDefinitionKey (); 16} 17 18}

We can query the id of the current task and the process definition id through zhangsan. You will find that the task cannot be queried if it is not entered in zhangsan, because the task handler we specified is zhangsan.


  6,Process a task. After zhangsan obtains the task, it must be processed.

1 // complete a process 2 @ Test 3 public void completeProcessInstance () {4 5 // task id. After integration, the task of the user is queried by the current login identity, then obtain the id 6 String taskId = "402"; 7 TaskService taskService = engine. getTaskService (); 8 // complete the task 9 taskService Based on the task id. complete (taskId); 10 11}


  7,When we query the current task through lisi, that is, the handler of the next node, we will find that a task is queried, and then repeat 5 to 6 until the task ends.










Related Article

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.