activiti简单入门

Source

转载自:http://blog.csdn.net/caoyue_new/article/details/52163048
####Activiti 用户手册
先让我们来了解一下什么是Activiti

##Activiti 是什么

Activiti是一个开源的工作流引擎,它实现了BPMN 2.0规范,可以发布设计好的流程定义,并通过api进行流程调度。
Activiti基于Apache许可的开源BPM平台,创始人Tom Baeyens是JBoss Activiti的项目架构师,它的特色是提供了eclipse插件,开发人员可以通过插件直接绘画出业务流程图。
常用的工作流框架:Shark、Activiti、OSWorkflow、YAWL等。

##工作流(workflow)

业务过程的部分或整体在计算机应用环境下的自动化,它主要解决的是”使在流程中的多个参与者按照预定义的规则传递文档、信息或任务的过程自动进行,从而完成某个预期的业务目标“。
工作流管理系统(Workflow Management System, WfMS) 是一个软件系统(为企业业务运行提供的一个软件的支撑环境),它通过执行经过计算的流程定义去支持一批专门设定的业务流程,它完成工作量的定义(具体活动、规则)和管理,并按照在系统中预先定义好的工作流规则进行工作流实例的执行。

##工作流引擎

ProcessEngine对象,这是Activiti工作的核心。负责生成流程运行时的各种实例及数据、监控和管理流程的运行。

##BPMN 2.0

业务流程符号和模型(Business Process Model and Notation,BPMN) ,描述流程的基本符号,包括这些图元如何组合成一个业务流程图(Business Process Diagram)

了解了基本概念之后让我们先做个小例子体验一下,还是用经典的 helloworld。
我们这里使用 ecplise4.4、mysql 数据库,为了更方便的使用 activiti,建议安装 activiti 的插件,插件可以在这里下载。

##配置数据库 (activiti.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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">

    <bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
        <!-- 数据库连接配置 -->
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/testactiviti?createDatabaseIfNotExist=true&amp;useUnicode=true&amp;characterEncoding=utf8"></property>
        <property name="jdbcDriver" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUsername" value="root"></property>
        <property name="jdbcPassword" value="12345"></property>
        <!-- 建表策略 -->
        <property name="databaseSchemaUpdate" value="true"></property>
    </bean>
</beans>

##maven配置

<dependency>
    <groupId>org.activiti</groupId>
    <artifactId>activiti-engine</artifactId>
    <version>${activiti-version}</version>
</dependency>
<dependency>
      <groupId>org.activiti</groupId>
      <artifactId>activiti-spring</artifactId>
      <version>${activiti-version}</version>
</dependency>
<dependency>
   <groupId>org.activiti</groupId>
   <artifactId>activiti-bpmn-layout</artifactId>
   <version>${activiti.version}</version>
</dependency>
<dependency>
   <groupId>org.activiti</groupId>
   <artifactId>activiti-spring</artifactId>
   <version>${activiti.version}</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.34</version>
</dependency>
<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.11</version>
</dependency>

##首先画出Activiti流程图(helloworld.bpmn)
这里写图片描述
##对应代码:

<?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="helloworld" name="HelloWorld实例" isExecutable="true">
    <startEvent id="startevent1" name="Start"></startEvent>
    <userTask id="usertask1" name="提交申请" activiti:assignee="张三"></userTask>
    <userTask id="usertask2" name="部门经理【审批】" activiti:assignee="李四"></userTask>
    <userTask id="usertask3" name="总经理【审批】" activiti:assignee="王五"></userTask>
    <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow>
    <sequenceFlow id="flow2" sourceRef="usertask1" targetRef="usertask2"></sequenceFlow>
    <sequenceFlow id="flow3" sourceRef="usertask2" targetRef="usertask3"></sequenceFlow>
    <endEvent id="endevent1" name="End"></endEvent>
    <sequenceFlow id="flow4" sourceRef="usertask3" targetRef="endevent1"></sequenceFlow>
  </process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_helloworld">
    <bpmndi:BPMNPlane bpmnElement="helloworld" id="BPMNPlane_helloworld">
      <bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
        <omgdc:Bounds height="35.0" width="35.0" x="275.0" y="50.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1">
        <omgdc:Bounds height="55.0" width="105.0" x="240.0" y="120.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="usertask2" id="BPMNShape_usertask2">
        <omgdc:Bounds height="55.0" width="105.0" x="240.0" y="210.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="usertask3" id="BPMNShape_usertask3">
        <omgdc:Bounds height="55.0" width="105.0" x="240.0" y="300.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
        <omgdc:Bounds height="35.0" width="35.0" x="275.0" y="380.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
        <omgdi:waypoint x="292.0" y="85.0"></omgdi:waypoint>
        <omgdi:waypoint x="292.0" y="120.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
        <omgdi:waypoint x="292.0" y="175.0"></omgdi:waypoint>
        <omgdi:waypoint x="292.0" y="210.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3">
        <omgdi:waypoint x="292.0" y="265.0"></omgdi:waypoint>
        <omgdi:waypoint x="292.0" y="300.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow4" id="BPMNEdge_flow4">
        <omgdi:waypoint x="292.0" y="355.0"></omgdi:waypoint>
        <omgdi:waypoint x="292.0" y="380.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</definitions>

##HelloWorld.java

package com.activiti.test;

import java.util.List;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;

public class HelloWorld {

    //创建流程引擎实例(方法三)
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

    /**创建流程引擎实例(方法一)**/
    @Test
    public void createTable() {
        //创建activiti配置对象的实例
        ProcessEngineConfiguration configuration = ProcessEngineConfiguration.createStandaloneProcessEngineConfiguration();
        //设置数据库连接信息
        configuration.setJdbcUrl("jdbc:mysql://localhost:3306/testactiviti?createDatabaseIfNotExist&amp;useUnicode=true&amp;characterEncoding=utf8");
        configuration.setJdbcDriver("com.mysql.jdbc.Driver");
        configuration.setJdbcUsername("root");
        configuration.setJdbcPassword("12345");

        System.out.println("-------run----------");
        //设置数据库建表策略
        /**
         * DB_SCHEMA_UPDATE_TRUE:如果不存在表就创建表,存在就直接使用
         * DB_SCHEMA_UPDATE_FALSE:如果不存在表就抛出异常
         * DB_SCHEMA_UPDATE_CREATE_DROP:每次都先删除表,再创建新的表
         */
        configuration.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_CREATE_DROP);
        //使用配置对象创建流程实例
        processEngine = configuration.buildProcessEngine();
        System.out.println(processEngine);
    }

    /**常见流程引擎实例(方法二)**/
    @Test
    public void testDBByProperties() throws Exception {
        // 加载classpath下名为activiti.cfg.xml文件,创建核心流程引擎对象
        ProcessEngine processEngine = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml").buildProcessEngine();
        System.out.println(processEngine);
    }

    //部署流程定义
    @Test
    public void deploy() {
        Deployment deployment = processEngine.getRepositoryService()//获取管理流程定义实例
                            .createDeployment()//获取部署实例,添加要部署的流程并部署
                            .addClasspathResource("diagrams/helloworld.png")//从classpath中加载,一次只能加载一个文件
                            .addClasspathResource("diagrams/helloworld.bpmn")
                            .name("helloworld入门实例")//添加部署的名称
                            .deploy();//部署
        System.out.println("部署ID:" + deployment.getId());
        System.out.println("部署名称:" + deployment.getName());
    }

    //启动流程实例
    @Test
    public void startProcess() {
        ProcessInstance in = processEngine.getRuntimeService()//获取正在执行的流程实例和流程对象相关的Service
                        .startProcessInstanceByKey("helloworld");//使用流程定义的key启动流程实例,key对应helloworld.bpmn文件中id的属性值,
                                                                //使用key值启动,默认是按照最新版本的流程定义启动
        System.out.println("流程定义的ID:" + in.getProcessDefinitionId());
        System.out.println("流程实例的ID:" + in.getId());
    }

    /**查询当前人的个人任务*/
    @Test
    public void findMyPersonalTask() {
        String assignee = "李四";//办理人
        List<Task> list = processEngine.getTaskService()//与正在执行的任务管理相关的Service
                .createTaskQuery()//创建任务查询对象
                .taskAssignee(assignee)//指定个人任务查询,指定办理人
                .list();
        if (list != null && list.size() > 0) {
            for (Task task : list) {
                System.out.println("任务ID:" + task.getId());
                System.out.println("任务名称:" + task.getName());
                System.out.println("任务办理人:" + task.getAssignee());
                System.out.println("任务的创建时间:"+task.getCreateTime());
                System.out.println("流程定义ID:" + task.getProcessDefinitionId());
                System.out.println("流程实例ID:" + task.getProcessInstanceId());
                System.out.println("执行对象ID:" + task.getExecutionId());
            }
        }
    }

    /**完成我的任务**/
    @Test
    public void completeMyPersonalTask() {
        String taskId = "12502";//任务ID
        processEngine.getTaskService()
                    .complete(taskId);
        System.out.println("完成任务:任务ID:"+taskId);
    }
}