Java Activiti(2)--基础深入流程添加、删除、完成任务等

一、流程实现

public class ActivitiActionTest {
    private ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

    //1.1、创建表
    public static void createTabelAuto() {
        //创建对象
        ProcessEngineConfiguration conf = ProcessEngineConfiguration.createStandaloneProcessEngineConfiguration();
        String driver = PropertiesUtil.getValue("jdbc_driver", "conf/jdbc.properties");
        String url = PropertiesUtil.getValue("jdbc_url", "conf/jdbc.properties");
        String username = PropertiesUtil.getValue("jdbc_username", "conf/jdbc.properties");
        String password = PropertiesUtil.getValue("jdbc_password", "conf/jdbc.properties");
        //设置数据库
        conf.setJdbcDriver(driver);
        conf.setJdbcUrl(url);
        conf.setJdbcUsername(username);
        conf.setJdbcPassword(password);
        //自动建表
        conf.setDatabaseSchemaUpdate("true");

        //创建引擎
        ProcessEngine processEngine = conf.buildProcessEngine();
    }

    //1.2、创建表,processEngineConfiguration为数据库实体bean
    public static void createTabelByXML() {
        //创建对象
        String resource = "activiti-context2.xml";
        String beanName = "processEngineConfiguration";
        ProcessEngineConfiguration conf = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource(resource, beanName);
        //创建引擎
        ProcessEngine processEngine = conf.buildProcessEngine();
    }


    //2、部署流程定义
    @Test
    public void deployLeave() throws Exception {
        DeploymentBuilder builder = processEngine.getRepositoryService().createDeployment();
        //方式1
//        builder.addClasspathResource("flow/leave.bpmn");
//        builder.addClasspathResource("flow/leave.png");
//        Deployment deploy = builder.deploy();
        builder.addClasspathResource("flow/baoxiao.bmp");
        builder.addClasspathResource("flow/baoxiao.png");
        builder.name("报销流程部署");
        Deployment deploy = builder.deploy();
        //方式2
//        ZipInputStream zipInputStream = new ZipInputStream(this.getClass().getClassLoader().getResourceAsStream("flow/leave.zip"));
//        builder.addZipInputStream(zipInputStream);
//        builder.name("请假流程部署");
//        Deployment deploy = builder.deploy();
        System.out.println("deploy.getId()==" + deploy.getId());
    }

    //3.1、查询部署表
    @Test
    public void queryDeployments() throws Exception {
        DeploymentQuery deploymentQuery = processEngine.getRepositoryService().createDeploymentQuery();
        List<Deployment> list = deploymentQuery.list();
        for (Deployment d : list) {
            System.out.println(d.getId() + ":" + d.getName());
        }
    }

    //3.2、删除流程部署的id
    @Test
    public void deleteDeployment() throws Exception {
        String deploymentId = "1";
        //processEngine.getRepositoryService().deleteDeployment(deploymentId);
        //级联删除
        processEngine.getRepositoryService().deleteDeployment(deploymentId, true);
    }

    //3.3、根据流程部署的id查询部署文件信息与输入流
    @Test
    public void getDeployFile() throws Exception {
        String deploymentId = "10001";
        List<String> names = processEngine.getRepositoryService().getDeploymentResourceNames(deploymentId);
        for (String f : names) {
            System.out.println("f===" + f);
            InputStream inputStream = processEngine.getRepositoryService().getResourceAsStream(deploymentId, f);
            //把文件保存到本地

//            FileOutputStream fos = new FileOutputStream(new File("d:\\" + f));
//            int len = -1;
//            byte[] b = new byte[1024];
//            while((len = inputStream.read())!= -1){
//                fos.write(b, 0, len);
//            }
//            fos.flush();
//            fos.close();
            FileUtils.copyInputStreamToFile(inputStream, new File("d:\\" + f));
            inputStream.close();
        }

    }

    //3.4、根据流程部署的id查询部署文件图片
    @Test
    public void getDeployPNG() throws Exception {
        String processDefinitionId = "leaveFlow:4:10004";
        InputStream inputStream = processEngine.getRepositoryService().getProcessDiagram(processDefinitionId);
        FileUtils.copyInputStreamToFile(inputStream, new File("d:\\leave.png"));
        inputStream.close();
    }

    //4.1、获得流程定义的id
    @Test
    public void queryProcessDefinition() throws Exception {
        ProcessDefinitionQuery query = processEngine.getRepositoryService().createProcessDefinitionQuery();
        //添加过滤条件
        query.processDefinitionKey("baoxiaoFlow");
        //添加排序
        query.orderByProcessDefinitionVersion().asc();
        //分页
        query.listPage(0, 10);
        List<ProcessDefinition> processDefinitions = query.list();
        for (ProcessDefinition p : processDefinitions) {
            System.out.println("p.getId()===" + p.getId());
        }
    }

    //4.1.1、查询最新流程定义
    @Test
    public void queryLatestProcessDefinition() throws Exception {
        ProcessDefinitionQuery query = processEngine.getRepositoryService().createProcessDefinitionQuery();
        //添加排序
        query.orderByProcessDefinitionVersion().asc();
        List<ProcessDefinition> list = query.list();
        Map<String , ProcessDefinition> map = new HashMap<String, ProcessDefinition>();
        for (ProcessDefinition pd : list) {
            map.put(pd.getKey(), pd);//key不同leaveFlow, baoxiaoFlow
        }
        System.out.println(map);
        ArrayList<ProcessDefinition> lastList = new ArrayList<ProcessDefinition>(map.values());
        for (ProcessDefinition pd: lastList){
            System.out.println(pd.getName() + ":" + pd.getVersion());
        }
    }

    //4.2、删除流程部署的id,因为部署与定义是主副关联关系,删除主表时就能删除流程定义
    @Test
    public void deleteProcessDefinition() throws Exception {
        String deploymentId = "1";
        //processEngine.getRepositoryService().deleteDeployment(deploymentId);
        //级联删除
        processEngine.getRepositoryService().deleteDeployment(deploymentId, true);
    }

    //4.3、根据流程定义的id启动流程实例
    @Test
    public void startProcessInstanceById() throws Exception {
        String processDefinitionId = "leaveFlow:4:10004";
        ProcessInstance processInstance = processEngine.getRuntimeService().startProcessInstanceById(processDefinitionId);
        System.out.println("processInstance.getId()===" + processInstance.getId());
    }

    //4.4、根据流程定义的key启动流程实例,启动版本version最高的
    @Test
    public void startProcessInstanceByKey() throws Exception {
        String processDefinitionKey = "leaveFlow";
        ProcessInstance processInstance = processEngine.getRuntimeService().startProcessInstanceByKey(processDefinitionKey);
        System.out.println("processInstance.getId()===" + processInstance.getId());
        System.out.println("processInstance.getProcessDefinitionId()===" + processInstance.getProcessDefinitionId());
    }

    @Test
    public void querys() throws Exception {
        List<ProcessDefinition> processDefinitions = processEngine.getRepositoryService().createProcessDefinitionQuery().list();
        List<Deployment> deployments = processEngine.getRepositoryService().createDeploymentQuery().list();
        List<ProcessInstance> processInstances = processEngine.getRuntimeService().createProcessInstanceQuery().list();
        List<Task> tasks = processEngine.getTaskService().createTaskQuery().list();

    }

    //5.1,查询流程实例,对应excution表
    @Test
    public void queryProcessInstance() throws Exception {
        ProcessInstanceQuery query = processEngine.getRuntimeService().createProcessInstanceQuery();
        query.processDefinitionId("leaveFlow");
        query.orderByProcessDefinitionKey().desc();
        query.listPage(0,2);
        List<ProcessInstance> processInstances = query.list();
        for (ProcessInstance pi : processInstances) {
            //getActivityId活动id
            System.out.println(pi.getId() + ":" + pi.getActivityId());
        }
    }

    //5.2,结束流程实例,同时会关闭任务
    @Test
    public void endProcessInstance() throws Exception {
        String processInstanceId = "17504";
        processEngine.getRuntimeService().deleteProcessInstance(processInstanceId, "ok");
    }

    //6.1、根据流程实例,查询任务列表  ,说明一个流程定义对应多个流程实例,一个流程实例对应多个任务列表
    @Test
    public void getProcessTask() throws Exception {
        String processInstanceId = "5001";
        //1
        //String assignee = "tom";
        //2
        String assignee = "jack";
        //3
        //String assignee = "smith";
        //任务列表
        TaskQuery taskQuery = processEngine.getTaskService().createTaskQuery();
        //查询tom的任务列表
        taskQuery.taskAssignee(assignee);
        taskQuery.orderByTaskCreateTime().desc();
        List<Task> tasks = taskQuery.list();
        for (Task task : tasks) {
            System.out.println(task.getId() + ":" + task.getName());
        }
    }

    //6.2、办理任务,办理完成后,第三步就找不到tom的这个任务了,跑到下个jack的任务中了,5与6是相互重复的
    @Test
    public void dealProcessTask() throws Exception {
        //String taskId = "12506"; //提交
        String taskId = "22502";  //项目经理审批
        processEngine.getTaskService().complete(taskId);
    }

    //6.3、向下执行一步,跳过当前审批
    @Test
    public void nextProcessTask() throws Exception {
        //String taskId = "12506"; //提交
        String executionId = "20002";  //项目经理审批
        //processEngine.getRuntimeService().signal(executionId);
    }
}

二、基本连接数据库文件activiti-context.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">
    <!-- 配置流程引擎配置对象 -->
    <bean id="processEngineConfiguration"
        class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
        <property name="jdbcDriver" value="com.mysql.jdbc.Driver" />
        <property name="jdbcUrl" value="jdbc:mysql:///activiti_1110" />
        <property name="jdbcUsername" value="root" />
        <property name="jdbcPassword" value="root" />
        <property name="databaseSchemaUpdate" value="true" />
    </bean>

    <!-- 配置一个流程引擎工厂bean,用于创建流程引擎对象 -->
    <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
        <!-- 通过set方法注入流程引擎配置对象 -->
        <property name="processEngineConfiguration" ref="processEngineConfiguration" />
    </bean>
</beans>

三、报销流程xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" 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" xmlns:tns="http://www.activiti.org/test" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" expressionLanguage="http://www.w3.org/1999/XPath" id="m1505312057485" name="" targetNamespace="http://www.activiti.org/test" typeLanguage="http://www.w3.org/2001/XMLSchema">
  <process id="baoxiaoFlow" isClosed="false" isExecutable="true" name="baoxiaoFlow" processType="None">
    <startEvent id="startId" name="start"/>
    <endEvent id="endId" name="end"/>
    <userTask activiti:assignee="tom" activiti:async="false" activiti:exclusive="true" id="processSubmitId" name="提交报销申请"/>
    <userTask activiti:assignee="jack" activiti:exclusive="true" id="processSupervisorId" name="经理审批"/>
    <sequenceFlow id="_6" sourceRef="startId" targetRef="processSubmitId"/>
    <sequenceFlow id="_7" sourceRef="processSubmitId" targetRef="processSupervisorId"/>
    <userTask activiti:assignee="smith" activiti:exclusive="true" id="processManagerId" name="总经理审批"/>
    <sequenceFlow id="_10" sourceRef="processSupervisorId" targetRef="processManagerId"/>
    <sequenceFlow id="_11" sourceRef="processManagerId" targetRef="endId"/>
    <sequenceFlow id="startId-processSubmitId" sourceRef="startId" targetRef="processSubmitId"/>
  </process>
  <bpmndi:BPMNDiagram documentation="background=#FFFFFF;count=1;horizontalcount=1;orientation=0;width=842.4;height=1195.2;imageableWidth=832.4;imageableHeight=1185.2;imageableX=5.0;imageableY=5.0" id="Diagram-_1" name="New Diagram">
    <bpmndi:BPMNPlane bpmnElement="baoxiaoFlow">
      <bpmndi:BPMNShape bpmnElement="startId" id="Shape-startId">
        <omgdc:Bounds height="32.0" width="32.0" x="390.0" y="115.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="32.0" width="32.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="endId" id="Shape-endId">
        <omgdc:Bounds height="32.0" width="32.0" x="400.0" y="495.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="32.0" width="32.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="processSubmitId" id="Shape-processSubmitId">
        <omgdc:Bounds height="55.0" width="85.0" x="370.0" y="215.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="55.0" width="85.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="processSupervisorId" id="Shape-processSupervisorId">
        <omgdc:Bounds height="55.0" width="85.0" x="365.0" y="305.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="55.0" width="85.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="processManagerId" id="Shape-processManagerId">
        <omgdc:Bounds height="55.0" width="85.0" x="370.0" y="405.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="55.0" width="85.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="_6" id="BPMNEdge__6" sourceElement="startId" targetElement="processSubmitId">
        <omgdi:waypoint x="406.0" y="147.0"/>
        <omgdi:waypoint x="406.0" y="215.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="startId-processSubmitId" id="BPMNEdge_startId-processSubmitId" sourceElement="startId" targetElement="processSubmitId">
        <omgdi:waypoint x="406.0" y="147.0"/>
        <omgdi:waypoint x="406.0" y="215.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="-1.0" width="-1.0" x="-1.0" y="-1.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="_7" id="BPMNEdge__7" sourceElement="processSubmitId" targetElement="processSupervisorId">
        <omgdi:waypoint x="410.0" y="270.0"/>
        <omgdi:waypoint x="410.0" y="305.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="_11" id="BPMNEdge__11" sourceElement="processManagerId" targetElement="endId">
        <omgdi:waypoint x="416.0" y="460.0"/>
        <omgdi:waypoint x="416.0" y="495.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="_10" id="BPMNEdge__10" sourceElement="processSupervisorId" targetElement="processManagerId">
        <omgdi:waypoint x="410.0" y="360.0"/>
        <omgdi:waypoint x="410.0" y="405.0"/>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</definitions>

—————————————————————————————————————————————————–

java架构师项目实战,高并发集群分布式,大数据高可用视频教程,共760G

下载地址:

https://item.taobao.com/item.htm?id=555888526201

01.高级架构师四十二个阶段高
02.Java高级系统培训架构课程148课时
03.Java高级互联网架构师课程
04.Java互联网架构Netty、Nio、Mina等-视频教程
05.Java高级架构设计2016整理-视频教程
06.架构师基础、高级片
07.Java架构师必修linux运维系列课程
08.Java高级系统培训架构课程116课时
+
hadoop系列教程,java设计模式与数据结构, Spring Cloud微服务, SpringBoot入门

—————————————————————————————————————————————————–

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lovoo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值