"Activiti Workflow Framework" Topic (3)-Activiti Workflow Framework HelloWorld Program

1. Create a flowchart method

Click the ActivitiTest project and create a diagrams directory in the src/main/java directory to store the flowcharts.
Right-click the current project and select Activiti Diagram. Enter the flowchart name HelloWorld, and then click OK.
Insert picture description here
There are related drawing icons on the right side of the control panel. operating

One of the processes must contain a start node and an end node, and there can be multiple end nodes.
Then use StartEvent, UserTask, EndEvent to draw the following flowchart, and then use the SequenceFlow connection in Connection to connect.

2. Create a flowchart, as shown below

Insert picture description here

2.1. Specify the flow chart name, ID and the handler of UserTask

1) Select the first node, enter the name of the current node in the name attribute, enter the processor of the node in Assignee, and then set the value of the 3 nodes by analogy.
Insert picture description here
Insert picture description here
Insert picture description here

2) Then click on the blank next to the flowchart, enter the ID and Name of the process, and then save
Insert picture description here

3. Deploy the flowchart to the Activiti data table

/**
 * 部署流程定义(操作数据表:act_re_deployment、act_re_procdef、act_ge_bytearray)
 */
@Test
public void test1(){
    
    
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    System.out.println(processEngine);
    RepositoryService service = processEngine.getRepositoryService();
    Deployment deployment = service.createDeployment()
            .name("helloworld入门")
            .addClasspathResource("diagrams/helloworld.bpmn")
            .addClasspathResource("diagrams/helloworld.png")
            .deploy();
    System.out.println("流程定义部署ID:"+deployment.getId());
    System.out.println("流程定义部署NAME:"+deployment.getName());
}

4. Query process definition list

/**
 * 查询流程定义列表
 */
@Test
public void test2() {
    
    
    // 流程定义查询对象,用于查询表act_re_procdef
    ProcessDefinitionQuery query = processEngine.getRepositoryService().createProcessDefinitionQuery();
    // 添加过滤条件
    query.processDefinitionKey("helloworldkey");
    // 添加排序条件
    query.orderByProcessDefinitionVersion().desc();
    // 添加分页查询
    query.listPage(0, 10);
    List<ProcessDefinition> list = query.list();
    for (ProcessDefinition pd : list) {
    
    
        System.out.println(pd.getId() + "--" + pd.getName());
    }
}

5. Start the process instance

Among them runtimeService.startProcessInstanceByKey("helloworldkey");, the helloworldkeyid value in the corresponding flowchart, act_re_procdefand the key field in the process definition table in the data table

@Test
public void flowStart(){
    
    
	String processDefinitionKey = "helloworldkey";
	//获取正在执行流程实例和执行对象相关的service
	RuntimeService runtimeService = processEngine.getRuntimeService();
	//使用流程定义key启动流程实例,key对象是HelloWorld.bpmn文件中的ID属性值,对应的是act_re_procdef表中的key
	ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey);
	System.out.println(processInstance.getId()); //流程实例ID
}

After the process is started act_ru_execution, a piece of data will be generated in the table. This piece of data is the task being executed by the current process, and the value of the act_id_ field corresponds to the ID value of the flowchart node

act_ru_taskA piece of task data will be generated in the table, execution_id_corresponding to the act_ru_executionprimary key, which proc_inst_id_is the process instance ID, the name_value is the process node name, and the assignee_field is the current processing of the to-do

5. Query to-do tasks

/**
 * 查询个人任务列表
 */
@Test
public void test7() {
    
    
	TaskQuery query = processEngine.getTaskService().createTaskQuery();
	String assignee = "张三";
	query.taskAssignee(assignee);
	List<Task> list = query.list();
	for (Task task : list) {
    
    
		System.out.println("待办任务ID:"+task.getId());
		System.out.println("待办任务名称:"+task.getName());
		System.out.println("任务创建时间:"+task.getCreateTime());
		System.out.println("任务办理人:"+task.getAssignee());
		System.out.println("流程实例ID:"+task.getProcessInstanceId());
		System.out.println("执行对象ID:"+task.getExecutionId());
		System.out.println("流程定义ID:"+task.getProcessDefinitionId());
	}
}

6. Complete to-do tasks

/**
 * 办理任务
 */
@Test
public void test8(){
    
    
	String taskId= "7504";
	processEngine.getTaskService().complete(taskId);
	System.out.println("办理完成,任务ID是:"+taskId);
}

The taskId corresponds to act_ru_taskthe primary key ID of the table. Because the current to-do task is the first node to submit an application, when the to-do is executed, the next to-do data will flow to the approval [department manager], and so on. The to-do tasks of the department manager and general manager are all inquired and executed.

7. Summary

Service object
RepositoryService provided by the Activiti framework ---- Operate static resources (process definition, bpmn, png)
RuntimeService-----Operation process instance (start process instance, query process instance, end process instance)
TaskService---
--Operation task (query task, handling task) HistoryService----operation history data

Object provided by Activiti framework (corresponding to table)
Deployment-----act_re_deployment
ProcessDefinition----act_re_procdef
ProcessInstance-----act_ru_execution
Task-----act_ru_task

Guess you like

Origin blog.csdn.net/BruceLiu_code/article/details/113615691