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

Posted yangykaifa

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Activiti的简单入门样例(经典的请假样例)相关的知识,希望对你有一定的参考价值。

经典的请假样例:

流程例如以下,首先须要部门经理审批。假设请假天数大于2天,则须要总经理审批,否则HR审批就可以


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

技术分享

增加依赖

pom.xml

<dependency>
	<groupId>org.activiti</groupId>
	<artifactId>activiti-engine</artifactId>
	<version>5.17.0</version>
</dependency>
<dependency>
	<groupId>org.activiti</groupId>
	<artifactId>activiti-bpmn-layout</artifactId>
	<version>5.17.0</version>
</dependency>
<dependency>
	<groupId>org.postgresql</groupId>
	<artifactId>postgresql</artifactId>
	<version>9.3-1103-jdbc41</version>
</dependency>
<dependency>
	<groupId>org.slf4j</groupId>
	<artifactId>slf4j-log4j12</artifactId>
	<version>1.7.12</version>
</dependency>

activiti.cfg.xml 

数据库使用postgresql

<?

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="drop-create"/> <property name="jdbcUrl" value="jdbc:postgresql://127.0.0.1/db_cloud"/> <property name="jdbcDriver" value="org.postgresql.Driver"/> <property name="jdbcUsername" value="admin"/> <property name="jdbcPassword" value="root"/> <property name="jobExecutorActivate" value="true"/> </bean> </beans>


流程文件leave.bpmn20.xml

<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"
	xmlns:activiti="http://activiti.org/bpmn" targetNamespace="Examples"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.omg.org/spec/BPMN/20100524/MODEL
             http://www.omg.org/spec/BPMN/2.0/20100501/BPMN20.xsd">

	<process id="leave_process" name="某公司请假流程">
		<startEvent id="leave" name="请假申请"></startEvent>
		
		<userTask id="dept_manager" name="部门经理审批"></userTask>
		<userTask id="boss" name="总经理审批"></userTask>
		<userTask id="hr" name="HR审批"></userTask>
		<sequenceFlow id="flow1" sourceRef="leave" targetRef="dept_manager"></sequenceFlow>
		<sequenceFlow id="flow2" sourceRef="dept_manager" targetRef="boss">
			<conditionExpression xsi:type="tFormalExpression"><![CDATA[${day >= 3}]]></conditionExpression>
		</sequenceFlow>
		<sequenceFlow id="flow3" sourceRef="boss" targetRef="hr"></sequenceFlow>
		<sequenceFlow id="flow4" sourceRef="dept_manager" targetRef="hr">
			<conditionExpression xsi:type="tFormalExpression"><![CDATA[${day < 3}]]></conditionExpression>
		</sequenceFlow>
		<sequenceFlow id="flow5" sourceRef="hr" targetRef="end"></sequenceFlow>
		
		<endEvent id="end" name="结束"></endEvent>
	</process>

</definitions>

流程图例如以下:

技术分享


二:代码演示

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<String, Object> 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<Task> 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审批
























以上是关于Activiti的简单入门样例(经典的请假样例)的主要内容,如果未能解决你的问题,请参考以下文章

经典入门_Hash的应用

《入门经典》——6.22

《算法竞赛入门经典》5.12TeX括号

《算法竞赛入门经典》5.22阶乘的精确值

有关int范围的例题(算法竞赛入门经典)

《算法竞赛入门经典》5.13周期串