创建Activiti项目详解编程语言

 1.创建项目

创建Activiti项目详解编程语言

 

创建Activiti项目详解编程语言

 

创建Activiti项目详解编程语言

 2.导入Activiti的jar包

项目上右击创建lib文件夹

创建Activiti项目详解编程语言

创建Activiti项目详解编程语言

 

将jar包复制进lib文件夹

创建Activiti项目详解编程语言

创建Activiti项目详解编程语言

创建Activiti项目详解编程语言

 

3.导入数据库连接jar包

创建Activiti项目详解编程语言

 

4.使用代码创建Activiti用来存放流程数据的23张表

创建Activiti项目详解编程语言

 

package junit; 
 
import org.activiti.engine.ProcessEngine; 
import org.activiti.engine.ProcessEngineConfiguration; 
import org.junit.Test; 
 
public class TestActiviti { 
    /** 
     * 使用代码创建工作流需要的23张表 
     */ 
    @Test 
    public void createTable(){ 
        ProcessEngineConfiguration processEngineConfiguration = ProcessEngineConfiguration.createStandaloneProcessEngineConfiguration(); 
        //连接数据库配置 
        processEngineConfiguration.setJdbcDriver("com.mysql.jdbc.Driver"); 
        processEngineConfiguration.setJdbcUrl("jdbc:mysql://localhost:3306/activiti?useUnicode=true&characterEncoding=utf8"); 
        processEngineConfiguration.setJdbcUsername("root"); 
        processEngineConfiguration.setJdbcPassword("******"); 
         
        /** 
          public static final String DB_SCHEMA_UPDATE_FALSE = "false";//不能自动创建表,需要表存在 
          public static final String DB_SCHEMA_UPDATE_CREATE_DROP = "create-drop";//先删除表再创建表 
          public static final String DB_SCHEMA_UPDATE_TRUE = "true";//如果表不存在,自动创建表 
          */ 
        processEngineConfiguration.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE); 
        //工作流的核心对象,ProcessEngine对象 
        ProcessEngine processEngine = processEngineConfiguration.buildProcessEngine(); 
        System.out.println("processEngine:"+ processEngine); 
    } 
}

 5.优化使用配置文件来创建工作流需要的23张表

创建Activiti项目详解编程语言

 

activiti.cfg.xml

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" 
    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 
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd 
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> 
 
    <!--  
    ProcessEngineConfiguration processEngineConfiguration = ProcessEngineConfiguration.createStandaloneProcessEngineConfiguration(); 
        //连接数据库配置 
        processEngineConfiguration.setJdbcDriver("com.mysql.jdbc.Driver"); 
        processEngineConfiguration.setJdbcUrl("jdbc:mysql://localhost:3306/activiti?useUnicode=true&characterEncoding=utf8"); 
        processEngineConfiguration.setJdbcUsername("root"); 
        processEngineConfiguration.setJdbcPassword("******"); 
         
        /** 
          public static final String DB_SCHEMA_UPDATE_FALSE = "false";//不能自动创建表,需要表存在 
          public static final String DB_SCHEMA_UPDATE_CREATE_DROP = "create-drop";//先删除表再创建表 
          public static final String DB_SCHEMA_UPDATE_TRUE = "true";//如果表不存在,自动创建表 
          */ 
        processEngineConfiguration.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE); 
     --> 
     
    <bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration"> 
        <property name="jdbcDriver" value="com.mysql.jdbc.Driver"></property> 
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/activiti?useUnicode=true&amp;characterEncoding=utf8"></property> 
        <property name="jdbcUsername" value="root"></property> 
        <property name="jdbcPassword" value="******"></property> 
        <!-- 没有表创建表 --> 
        <property name="databaseSchemaUpdate" value="true"></property> 
    </bean> 
 
</beans>

 

log4j.properties

log4j.rootLogger=INFO, CA 
 
# ConsoleAppender 
log4j.appender.CA=org.apache.log4j.ConsoleAppender 
log4j.appender.CA.layout=org.apache.log4j.PatternLayout 
log4j.appender.CA.layout.ConversionPattern= %d{hh:mm:ss,SSS} [%t] %-5p %c %x - %m%n

 

TestActiviti.java
package junit; 
 
import org.activiti.engine.ProcessEngine; 
import org.activiti.engine.ProcessEngineConfiguration; 
import org.junit.Test; 
 
public class TestActiviti { 
    /** 
     * 使用代码创建工作流需要的23张表 
     */ 
    @Test 
    public void createTable(){ 
        ProcessEngineConfiguration processEngineConfiguration = ProcessEngineConfiguration.createStandaloneProcessEngineConfiguration(); 
        //连接数据库配置 
        processEngineConfiguration.setJdbcDriver("com.mysql.jdbc.Driver"); 
        processEngineConfiguration.setJdbcUrl("jdbc:mysql://localhost:3306/activiti?useUnicode=true&characterEncoding=utf8"); 
        processEngineConfiguration.setJdbcUsername("root"); 
        processEngineConfiguration.setJdbcPassword("******"); 
         
        /** 
          public static final String DB_SCHEMA_UPDATE_FALSE = "false";//不能自动创建表,需要表存在 
          public static final String DB_SCHEMA_UPDATE_CREATE_DROP = "create-drop";//先删除表再创建表 
          public static final String DB_SCHEMA_UPDATE_TRUE = "true";//如果表不存在,自动创建表 
          */ 
        processEngineConfiguration.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE); 
        //工作流的核心对象,ProcessEngine对象 
        ProcessEngine processEngine = processEngineConfiguration.buildProcessEngine(); 
        System.out.println("processEngine:"+ processEngine); 
    } 
     
    /* 
     * 使用配置文件来创建工作流需要的23张表 
     */ 
    @Test 
    public void createTable2(){ 
//        ProcessEngineConfiguration processEngineConfiguration = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml"); 
//        //工作流的核心对象,ProcessEngine对象 
//        ProcessEngine processEngine = processEngineConfiguration.buildProcessEngine(); 
         
        ProcessEngine processEngine = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml").buildProcessEngine(); 
        System.out.println("processEngine:"+ processEngine); 
    } 
}

 6.创建流程图

创建Activiti项目详解编程语言

创建Activiti项目详解编程语言 

创建Activiti项目详解编程语言

创建Activiti项目详解编程语言


创建Activiti项目详解编程语言


创建Activiti项目详解编程语言


创建Activiti项目详解编程语言


<?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="HelloWorldProcess" isExecutable="true"> 
    <startEvent id="startevent1" name="Start"></startEvent> 
    <endEvent id="endevent1" name="End"></endEvent> 
    <userTask id="usertask1" name="提交申请" activiti:assignee="张三"></userTask> 
    <userTask id="usertask2" name="审批【部门经理】" activiti:assignee="李四"></userTask> 
    <userTask id="usertask3" name="审批【总经理】" activiti:assignee="王五"></userTask> 
    <sequenceFlow id="flow2" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow> 
    <sequenceFlow id="flow3" sourceRef="usertask1" targetRef="usertask2"></sequenceFlow> 
    <sequenceFlow id="flow4" sourceRef="usertask2" targetRef="usertask3"></sequenceFlow> 
    <sequenceFlow id="flow5" 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="41.0" x="20.0" y="240.0"></omgdc:Bounds> 
      </bpmndi:BPMNShape> 
      <bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1"> 
        <omgdc:Bounds height="35.0" width="35.0" x="570.0" y="240.0"></omgdc:Bounds> 
      </bpmndi:BPMNShape> 
      <bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1"> 
        <omgdc:Bounds height="55.0" width="105.0" x="90.0" y="230.0"></omgdc:Bounds> 
      </bpmndi:BPMNShape> 
      <bpmndi:BPMNShape bpmnElement="usertask2" id="BPMNShape_usertask2"> 
        <omgdc:Bounds height="55.0" width="101.0" x="240.0" y="230.0"></omgdc:Bounds> 
      </bpmndi:BPMNShape> 
      <bpmndi:BPMNShape bpmnElement="usertask3" id="BPMNShape_usertask3"> 
        <omgdc:Bounds height="55.0" width="111.0" x="400.0" y="230.0"></omgdc:Bounds> 
      </bpmndi:BPMNShape> 
      <bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2"> 
        <omgdi:waypoint x="61.0" y="257.0"></omgdi:waypoint> 
        <omgdi:waypoint x="90.0" y="257.0"></omgdi:waypoint> 
      </bpmndi:BPMNEdge> 
      <bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3"> 
        <omgdi:waypoint x="195.0" y="257.0"></omgdi:waypoint> 
        <omgdi:waypoint x="240.0" y="257.0"></omgdi:waypoint> 
      </bpmndi:BPMNEdge> 
      <bpmndi:BPMNEdge bpmnElement="flow4" id="BPMNEdge_flow4"> 
        <omgdi:waypoint x="341.0" y="257.0"></omgdi:waypoint> 
        <omgdi:waypoint x="400.0" y="257.0"></omgdi:waypoint> 
      </bpmndi:BPMNEdge> 
      <bpmndi:BPMNEdge bpmnElement="flow5" id="BPMNEdge_flow5"> 
        <omgdi:waypoint x="511.0" y="257.0"></omgdi:waypoint> 
        <omgdi:waypoint x="570.0" y="257.0"></omgdi:waypoint> 
      </bpmndi:BPMNEdge> 
    </bpmndi:BPMNPlane> 
  </bpmndi:BPMNDiagram> 
</definitions>

 7.部署流程定义

创建Activiti项目详解编程语言

 

HelloWorld.java
package com.mycom.helloWorld; 
 
import org.activiti.engine.ProcessEngine; 
import org.activiti.engine.ProcessEngines; 
import org.activiti.engine.repository.Deployment; 
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") 
                        .deploy();//完成部署 
        System.out.println(deployment.getId()); 
        System.out.println(deployment.getName()); 
         
    } 
}

 

创建Activiti项目详解编程语言

 

8.启动流程实例

创建Activiti项目详解编程语言

 

package com.mycom.helloWorld; 
 
import org.activiti.engine.ProcessEngine; 
import org.activiti.engine.ProcessEngines; 
import org.activiti.engine.repository.Deployment; 
import org.activiti.engine.runtime.ProcessInstance; 
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") 
                        .deploy();//完成部署 
        System.out.println("部署ID:"+deployment.getId()); 
        System.out.println("部署名称:"+deployment.getName()); 
         
    } 
     
    /* 
     * 启动流程实例 
     */ 
    @Test 
    public void startProcessInstance(){ 
        //流程定义的key 
        String processDefinitionKey = "HelloWorld"; 
        ProcessInstance processInstance = processEngine.getRuntimeService()//与正在执行的流程实例和执行对象相关的Service 
                        .startProcessInstanceByKey(processDefinitionKey);//使用流程定义的key启动流程实例,key对应HelloWorld.bpmn文件中的ID的属性值,使用key值启动,默认是按照最新版本的流程定义启动 
                         
        System.out.println("流程实例ID:"+processInstance.getId());//流程实例ID 
        System.out.println("流程定义ID:"+processInstance.getProcessDefinitionId());//流程定义ID 
         
    } 
}

 

创建Activiti项目详解编程语言

 

 

创建Activiti项目详解编程语言

 

9. 查询当前人的个人任务

创建Activiti项目详解编程语言

 

 

package com.mycom.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") 
                        .deploy();//完成部署 
        System.out.println("部署ID:"+deployment.getId()); 
        System.out.println("部署名称:"+deployment.getName()); 
         
    } 
     
    /* 
     * 启动流程实例 
     */ 
    @Test 
    public void startProcessInstance(){ 
        //流程定义的key 
        String processDefinitionKey = "HelloWorld"; 
        ProcessInstance processInstance = processEngine.getRuntimeService()//与正在执行的流程实例和执行对象相关的Service 
                        .startProcessInstanceByKey(processDefinitionKey);//使用流程定义的key启动流程实例,key对应HelloWorld.bpmn文件中的ID的属性值,使用key值启动,默认是按照最新版本的流程定义启动 
                         
        System.out.println("流程实例ID:"+processInstance.getId());//流程实例ID 
        System.out.println("流程定义ID:"+processInstance.getProcessDefinitionId());//流程定义ID 
         
    } 
     
    /* 
     * 查询当前人的个人任务 
     */ 
    @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()); 
            } 
        } 
    } 
}

 

 创建Activiti项目详解编程语言

 

10.完成我的任务

创建Activiti项目详解编程语言

 

package com.mycom.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") 
                        .deploy();//完成部署 
        System.out.println("部署ID:"+deployment.getId()); 
        System.out.println("部署名称:"+deployment.getName()); 
         
    } 
     
    /* 
     * 启动流程实例 
     */ 
    @Test 
    public void startProcessInstance(){ 
        //流程定义的key 
        String processDefinitionKey = "HelloWorld"; 
        ProcessInstance processInstance = processEngine.getRuntimeService()//与正在执行的流程实例和执行对象相关的Service 
                        .startProcessInstanceByKey(processDefinitionKey);//使用流程定义的key启动流程实例,key对应HelloWorld.bpmn文件中的ID的属性值,使用key值启动,默认是按照最新版本的流程定义启动 
                         
        System.out.println("流程实例ID:"+processInstance.getId());//流程实例ID 
        System.out.println("流程定义ID:"+processInstance.getProcessDefinitionId());//流程定义ID 
         
    } 
     
    /* 
     * 查询当前人的个人任务 
     */ 
    @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()); 
            } 
        } 
    } 
     
    /* 
     * 完成我的任务 
     */ 
    @Test 
    public void completeMyPersonalTask(){ 
        //任务Id 
        String taskId = "104"; 
        processEngine.getTaskService()//与正在执行任务相关的Service 
                        .complete(taskId); 
        System.out.println("完成任务:任务Id:"+taskId); 
         
    } 
     
     
     
}

 

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/industrynews/13199.html

(0)
上一篇 2021年7月19日 15:31
下一篇 2021年7月19日 15:31

相关推荐

发表回复

登录后才能评论