Activiti工作流介绍

本文详细介绍Activiti工作流引擎的配置与使用,包括数据库连接设置、依赖项引入及流程定义。通过具体示例,展示如何部署流程、启动流程实例以及处理任务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Activiti项目是一项新的基于Apache许可的开源BPM平台,从基础开始构建,旨在提供支持新的BPMN 2.0标准,包括支持对象管理组(OMG),面对新技术的机遇,诸如互操作性和云架构,提供技术实现。

在这里插入图片描述在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

		<!--添加Activiti工作流的支持 一般需要exclusions -->
		<dependency>
			<groupId>org.activiti</groupId>
			<artifactId>activiti-engine</artifactId>
			<version>5.19.0.2</version>
		</dependency>
		<!--添加Activiti工作流对Spring的支持-->
		<dependency>
			<groupId>org.activiti</groupId>
			<artifactId>activiti-spring</artifactId>
			<version>5.19.0.2</version>
		</dependency>
		<dependency>
			<groupId>org.activiti</groupId>
			<artifactId>activiti-bpmn-model</artifactId>
			<version>5.19.0.2</version>
		</dependency>
        <!-- 数据库驱动依赖 -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.41</version>
		</dependency>

activity.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"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context.xsd">
	
	<bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneInMemProcessEngineConfiguration">
		<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test?characterEncoding=utf-8"></property>
		<property name="jdbcDriver" value="com.mysql.jdbc.Driver"></property>
		<property name="jdbcUsername" value="root"></property>
		<property name="jdbcPassword" value="root"></property>
		<property name="databaseSchemaUpdate" value="true"></property>
	</bean>

</beans>

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

<?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://www.activiti.org/test">
  <process id="myProcess" name="My process" isExecutable="true">
    <startEvent id="startevent1" name="Start"></startEvent>
    <endEvent id="endevent1" name="End"></endEvent>
    <userTask id="usertask1" name="用户登记" activiti:assignee="${userName}"></userTask>
    <exclusiveGateway id="exclusivegateway1" name="Exclusive Gateway"></exclusiveGateway>
    <userTask id="usertask2" name="部门经理审批" activiti:assignee="${userName}"></userTask>
    <userTask id="usertask3" name="人事经理审批" activiti:assignee="${userName}"></userTask>
    <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow>
    <sequenceFlow id="flow2" sourceRef="usertask1" targetRef="exclusivegateway1"></sequenceFlow>
    <sequenceFlow id="flow3" sourceRef="exclusivegateway1" targetRef="usertask2">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${days <= 5}]]></conditionExpression>
    </sequenceFlow>
    <sequenceFlow id="flow4" sourceRef="exclusivegateway1" targetRef="usertask3">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${days > 5}]]></conditionExpression>
    </sequenceFlow>
    <sequenceFlow id="flow5" sourceRef="usertask2" targetRef="endevent1"></sequenceFlow>
    <sequenceFlow id="flow6" sourceRef="usertask3" targetRef="endevent1"></sequenceFlow>
  </process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_myProcess">
    <bpmndi:BPMNPlane bpmnElement="myProcess" id="BPMNPlane_myProcess">
      <bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
        <omgdc:Bounds height="35.0" width="35.0" x="30.0" y="190.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
        <omgdc:Bounds height="35.0" width="35.0" x="600.0" y="180.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1">
        <omgdc:Bounds height="55.0" width="105.0" x="120.0" y="180.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="exclusivegateway1" id="BPMNShape_exclusivegateway1">
        <omgdc:Bounds height="40.0" width="40.0" x="279.0" y="187.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="usertask2" id="BPMNShape_usertask2">
        <omgdc:Bounds height="55.0" width="105.0" x="380.0" y="81.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="usertask3" id="BPMNShape_usertask3">
        <omgdc:Bounds height="55.0" width="105.0" x="390.0" y="259.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
        <omgdi:waypoint x="65.0" y="207.0"></omgdi:waypoint>
        <omgdi:waypoint x="120.0" y="207.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
        <omgdi:waypoint x="225.0" y="207.0"></omgdi:waypoint>
        <omgdi:waypoint x="279.0" y="207.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3">
        <omgdi:waypoint x="299.0" y="187.0"></omgdi:waypoint>
        <omgdi:waypoint x="320.0" y="107.0"></omgdi:waypoint>
        <omgdi:waypoint x="380.0" y="108.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow4" id="BPMNEdge_flow4">
        <omgdi:waypoint x="299.0" y="227.0"></omgdi:waypoint>
        <omgdi:waypoint x="320.0" y="287.0"></omgdi:waypoint>
        <omgdi:waypoint x="390.0" y="286.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow5" id="BPMNEdge_flow5">
        <omgdi:waypoint x="432.0" y="136.0"></omgdi:waypoint>
        <omgdi:waypoint x="617.0" y="180.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow6" id="BPMNEdge_flow6">
        <omgdi:waypoint x="442.0" y="259.0"></omgdi:waypoint>
        <omgdi:waypoint x="617.0" y="215.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</definitions>

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

package com.test;

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.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import com.test.mapper.OrderMapper;

@RunWith(SpringRunner.class)
@SpringBootTest(classes=Starter.class)
public class Tester
{
	private ProcessEngine initFromCode()
	{
		ProcessEngineConfiguration pec = ProcessEngineConfiguration.createStandaloneProcessEngineConfiguration();
		pec.setJdbcUrl("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC");
		pec.setJdbcDriver("com.mysql.jdbc.Driver");
		pec.setJdbcUsername("root");
		pec.setJdbcPassword("root");
		pec.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
		ProcessEngine processEngine = pec.buildProcessEngine();
		return processEngine;
	}
	
	private ProcessEngine initFromXml()
	{
    	ProcessEngineConfiguration pec = ProcessEngineConfiguration
    			.createProcessEngineConfigurationFromResource("activiti.cfg.xml");
    	ProcessEngine processEngine = pec.buildProcessEngine();
    	return processEngine;
	}
	
	private ProcessEngine initDefault()
	{
		ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
		return processEngine;
	}
	
	private ProcessEngine initProcessEngine()
	{
		return initFromCode();
	}
	
    private Deployment deployProcess()
    {
    	ProcessEngine pec = initProcessEngine();
		//部署的服务对象
		RepositoryService repositoryService = pec.getRepositoryService();
		//部署请假任务:
		Deployment deploy = repositoryService.createDeployment()
				.addClasspathResource("holiday.bpmn")
				.name("请假")
				.deploy();
		return deploy;
    }
    
    private ProcessInstance startProcess(String processName,String userName)
    {
    	//String processName = "activitiReview";
    	//String userName = "chqx";
    	ProcessEngine pec = initProcessEngine();
		RuntimeService runtimeService = pec.getRuntimeService();
		Map<String, Object> variables = new HashMap<String,Object>();
		variables.put("userName",userName);
		//根据key得到流程实例
		ProcessInstance instProcess = runtimeService.startProcessInstanceByKey(processName,variables);
		return instProcess;
    }
    
    private List<Task> getWorkItem(String processName,String userName)
    {
    	//String processName = "activitiReview";
    	//String userName = "chqx";
    	ProcessEngine pec = initProcessEngine();
    	
    	TaskService tsrv = pec.getTaskService();
    	List<Task> list = tsrv.createTaskQuery()
                .processDefinitionKey(processName)
                .taskAssignee(userName)
                .list();

    	return list;
    }
    
    private void completeTask(String taskId,String userName)
    {
    	ProcessEngine pec = initProcessEngine();
    	TaskService tsrv = pec.getTaskService();
		Map<String, Object> variables = new HashMap<String,Object>();
		variables.put("userName",userName);
		variables.put("days","6");

    	tsrv.complete(taskId, variables);
    }
    
    @Test
    public void testActiviti()
    {
    	//Deployment deployment = deployProcess();
    	//System.out.println("工作流模板发布:"+deployment.getId()+","+deployment.getName());
    	
    	String processName = "myProcess";
    	String userName = "wx";
    	
    	//ProcessInstance instProc = startProcess(processName,userName);
    	//System.out.println("工作流实例:"+instProc.getId()+","+instProc.getName());
    	

    	List<Task> tasks = getWorkItem(processName,userName);
    	for(Task task:tasks)
    	{
    		System.out.println("Task.id="+task.getId()+",参与者="+task.getAssignee()+",任务名称:="+task.getName());
    		completeTask(task.getId(),"wx");
    	}

    }
    
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值