Activiti的简单入门例子(经典的请假例子)

经典的请假例子:

流程如下,首先需要部门经理审批,如果请假天数大于2天,则需要总经理审批,否则HR审批即可


一:创建maven项目,项目结构如下:

Activiti的简单入门例子(经典的请假例子)_第1张图片

加入依赖

pom.xml


	org.activiti
	activiti-engine
	5.17.0


	org.activiti
	activiti-bpmn-layout
	5.17.0


	org.postgresql
	postgresql
	9.3-1103-jdbc41


	org.slf4j
	slf4j-log4j12
	1.7.12

activiti.cfg.xml 

数据库使用postgresql



 
    
        
        
        
        
        
        
    

流程文件leave.bpmn20.xml




	
		
		
		
		
		
		
		
			= 3}]]>
		
		
		
			
		
		
		
		
	



流程图如下:

Activiti的简单入门例子(经典的请假例子)_第2张图片


二:代码演示

Leave.java


package com.lala.activiti;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;

public class Leave
{
	static void log(Object obj)
    {
    	System.out.println(obj);
    }
	
	static void run(ProcessEngine processEngine) throws Exception
	{
		RepositoryService repositoryService = processEngine.getRepositoryService();
		
        repositoryService.createDeployment().addClasspathResource("leave.bpmn20.xml").deploy();
        
        RuntimeService runtimeService = processEngine.getRuntimeService();
        
        Map variables = new HashMap<>();
        variables.put("userid", "10010");
        variables.put("day", 3);
        
        runtimeService.startProcessInstanceByKey("leave_process", variables);
        
        TaskService taskService = processEngine.getTaskService();
        
        TaskQuery query = taskService.createTaskQuery();
        
        List tasks = query.list();

        for(Task task : tasks)
        {
        	log(task.getId() + "," + task.getName());
        	taskService.complete(task.getId(), variables);
        }
        
        log("--------------------");
        
        tasks = query.list();

        for(Task task : tasks)
        {
        	log(task.getId() + "," + task.getName());
        	taskService.complete(task.getId(), variables);
        }
        
        log("--------------------");
        
        tasks = query.list();

        for(Task task : tasks)
        {
        	log(task.getId() + "," + task.getName());
        	taskService.complete(task.getId(), variables);
        }
	}
	public static void main(String[] args)
	{
		ProcessEngine processEngine = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml").buildProcessEngine();
	
		try {
			run(processEngine);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		processEngine.close();
	}
}

variables.put("day", 3);

大于等于3的时候,输出


9,部门经理审批
--------------------
11,总经理审批
--------------------
13,HR审批


否则,输出

9,部门经理审批
--------------------
11,HR审批

你可能感兴趣的:(框架技术)