maven

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.lzh</groupId>
    <artifactId>activitiDemo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <properties>
        <activiti-version>5.18.0</activiti-version>
    </properties>
    <dependencies>
        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-servlet-api</artifactId>
            <version>7.0.62</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.3.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-io</artifactId>
            <version>1.3.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>
        <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.codehaus.groovy</groupId>
            <artifactId>groovy-all</artifactId>
            <version>2.4.3</version>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.3.168</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.6</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-jdk14</artifactId>
            <version>1.7.6</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
        </dependency>
    </dependencies>
</project>

resources资源下的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"  
       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="jdbcUrl" value="jdbc:mysql://localhost:3306/activiti" />  
    <property name="jdbcDriver" value="com.mysql.jdbc.Driver" />  
    <property name="jdbcUsername" value="root" />  
    <property name="jdbcPassword" value="root" />  
   //没有表给你创建表
    <property name="databaseSchemaUpdate" value="true" />  

  </bean>  

</beans>  
package cn.itcast.a_helloworld;

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 deploymentProcessDefinition(){
        Deployment deployment = processEngine.getRepositoryService()//与流程定义和部署对象相关的Service
                        .createDeployment()//创建一个部署对象
                        .name("helloworld入门程序")//添加部署的名称
                        .addClasspathResource("diagrams/helloworld.bpmn")//从classpath的资源中加载,一次只能加载一个文件
                        .addClasspathResource("diagrams/helloworld.png")//从classpath的资源中加载,一次只能加载一个文件
                        .deploy();//完成部署
        System.out.println("部署ID:"+deployment.getId());//1
        System.out.println("部署名称:"+deployment.getName());//helloworld入门程序  
    }

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


    /**查询当前人的个人任务*/
    @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.getCreateTime());
                System.out.println("任务的办理人:"+task.getAssignee());
                System.out.println("流程实例ID:"+task.getProcessInstanceId());
                System.out.println("执行对象ID:"+task.getExecutionId());
                System.out.println("流程定义ID:"+task.getProcessDefinitionId());
                System.out.println("########################################################");
            }
        }
    }

    /**完成我的任务*/
    @Test
    public void completeMyPersonalTask(){
        //任务ID
        String taskId = "302";
        processEngine.getTaskService()//与正在执行的任务管理相关的Service
                    .complete(taskId);
        System.out.println("完成任务:任务ID:"+taskId);
    }

    // 查看流程图1(xxx.png)

    @Test
    public void showView() throws Exception {
        // 从仓库中找需要展示的文件
        String deploymentId = "122501";
        List<String> names = processEngine.getRepositoryService()
                .getDeploymentResourceNames(deploymentId);
        String imageName = "";
        for (String name : names) {
            System.out.println("name:"+name);
            if(name.indexOf(".png")>=0){
                imageName = name;
            }
        }
        System.out.println("imageName:"+imageName);
        if(imageName!=null){
            //System.out.println(imageName);
            File f = new File("e:/"+ imageName);
            // 通过部署ID和文件名称得到文件的输入流
            InputStream in = processEngine.getRepositoryService()
                      .getResourceAsStream(deploymentId, imageName);
        FileUtils.copyInputStreamToFile(in, f);
        }
    }


    /**查看流程图2*/
    @Test
    public void getImgById(){
        /**
         * 思路:得到资源service 然后得到资源流,根据部署定义的deploymentId 和 资源名字来获取流,然后通过common-io第三方jar包来复制到指定目录
         */
        RepositoryService rep = processEngine.getRepositoryService();
        InputStream img = rep.getResourceAsStream("1", "diagrams/lzh.png");
        try {
            FileUtils.copyInputStreamToFile(img, new File("E://img/lzh.png"));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
 /**得到最新的发布版本*/
    @Test
    public void getListLastVersion(){
        /**
         * 思路:得到资源service 然后创建流程定义查询根据定义的版本升序排列得到list,然后new map 让key相同的覆盖之前的对象。得到最新的流程定义对象
         */
        List<ProcessDefinition> list = processEngine.getRepositoryService()
                                        .createProcessDefinitionQuery()
                                        .orderByProcessDefinitionVersion().asc().list();
        Map<String, ProcessDefinition> map=new LinkedHashMap<String, ProcessDefinition>();

        for(ProcessDefinition pd:list){
            map.put(pd.getKey(), pd);
        }

        Set<Entry<String, ProcessDefinition>> entrySet = map.entrySet();
        for(Entry<String, ProcessDefinition> e:entrySet){
            System.out.println(e.getKey()+"-->"+e.getValue().getVersion());
        }
    }
    /**删除流程根据定义流程key,就是说这个定义的全部版本都删除*/
    @Test
    public void deleteProcessByKey(){
        /**
         * 思路:得到资源service 然后创建流程定义查询根据流程定义key得到list,遍历list,通过service删除定义
         */
        List<ProcessDefinition> list = processEngine.getRepositoryService()
                                        .createProcessDefinitionQuery()
                                        .processDefinitionKey("myProcess_lzh2").list();

        for(ProcessDefinition pd:list){
            processEngine.getRepositoryService().deleteDeployment(pd.getDeploymentId(), true);
        }

    }

    /**得到流程实例状态*/
    @Test
    public void getState(){
        /**
         * 思路:得到运行时service 然后创建流程实例查询根据流程实例id得到唯一值。判断是否正在执行
         */
        ProcessInstance singleResult = processEngine.getRuntimeService()
                                                .createProcessInstanceQuery().processInstanceId("27501")
                                                .singleResult();
        if(singleResult!=null){
            System.out.println("该流程正在执行");
        }else{
            System.out.println("该流程已经执行完");
        }

    }

    /**得到历史任务完成信息*/
    @Test
    public void getHistoryTaskList(){
        /**
         * 思路:得到运行时service 然后创建历史任务实例查询,通过实例id,查找已经完成的任务
         */
        List<HistoricTaskInstance> list = processEngine.getHistoryService()
                                            .createHistoricTaskInstanceQuery().processInstanceId("2501")
                                            .finished().list();
        for(HistoricTaskInstance h:list){
            System.out.println(h.getId());
            System.out.println(h.getAssignee());
            System.out.println(h.getName());
            System.out.println("-----------------");
        }

    }
    /**得到历史活动信息*/
    @Test
    public void getHistoryActList(){
        /**
         * 思路:得到运行时service 然后创建历史活动实例查询,通过实例id,查找已经完成的历史活动
         */
        List<HistoricActivityInstance> list = processEngine.getHistoryService()
                                            .createHistoricActivityInstanceQuery().processInstanceId("27501").finished().list();
        for(HistoricActivityInstance h:list){
            System.out.println(h.getId());
            System.out.println(h.getAssignee());
            System.out.println(h.getActivityName());
            System.out.println(h.getActivityId());
            System.out.println(h.getActivityType());
            System.out.println("-----------------");
        }

    }