码迷,mamicode.com
首页 > 其他好文 > 详细

Activiti手动执行的应用(UserTask)

时间:2018-05-01 14:10:42      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:statistic   工作   frame   string   构建   点击   sch   技术   engine   

工作流模拟某公司请假流程情景如下:
   1.开发人员请假流程,如果开发人员请假,如果请假天数小于3天,组长批准,人事批准即可请假。
   2.如果请假大约三天,需要项目经理或者产品经理批准,并且项目总监批准

 

流程图:

 技术分享图片

 

流程配置:

<?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: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="UserExecuteTask" name="UserExecuteTask"> <startEvent id="startevent1" name="Start"></startEvent> <parallelGateway id="parallelgateway1" name="多种请假方式"></parallelGateway> <userTask id="servicetask1" name="项目经理同意" > <extensionElements> <activiti:taskListener event="complete" class="com.easyway.workflow.activiti.gateway.DeveloperManagerUserTaskListener"/> </extensionElements> </userTask> <userTask id="servicetask2" name="产品经理同意"> <extensionElements> <activiti:taskListener event="complete" class="com.easyway.workflow.activiti.gateway.ProductManagerUserTaskListener"/> </extensionElements> </userTask> <userTask id="servicetask3" name="项目组长同意" > <extensionElements> <activiti:taskListener event="complete" class="com.easyway.workflow.activiti.gateway.TeamLeaderUserTaskListener"/> </extensionElements> </userTask> <parallelGateway id="parallelgateway2" name="First Join"></parallelGateway> <userTask id="servicetask4" name="项目总监同意" > <extensionElements> <activiti:taskListener event="complete" class="com.easyway.workflow.activiti.gateway.ProductBossUserTaskListener"/> </extensionElements> </userTask> <parallelGateway id="parallelgateway3" name="人事判断"></parallelGateway> <userTask id="servicetask5" name="人事部门同意" > <extensionElements> <activiti:taskListener event="complete" class="com.easyway.workflow.activiti.gateway.HumanResouceUserTaskListener"/> </extensionElements> </userTask> <endEvent id="endevent1" name="End"></endEvent> <sequenceFlow id="flow1" name="" sourceRef="startevent1" targetRef="parallelgateway1"></sequenceFlow> <sequenceFlow id="flow2" name="" sourceRef="parallelgateway1" targetRef="servicetask1"></sequenceFlow> <sequenceFlow id="flow3" name="" sourceRef="parallelgateway1" targetRef="servicetask2"></sequenceFlow> <sequenceFlow id="flow4" name="" sourceRef="parallelgateway1" targetRef="servicetask3"></sequenceFlow> <sequenceFlow id="flow5" name="" sourceRef="servicetask1" targetRef="parallelgateway2"></sequenceFlow> <sequenceFlow id="flow6" name="" sourceRef="servicetask2" targetRef="parallelgateway2"></sequenceFlow> <sequenceFlow id="flow7" name="" sourceRef="servicetask3" targetRef="parallelgateway3"></sequenceFlow> <sequenceFlow id="flow8" name="" sourceRef="parallelgateway2" targetRef="servicetask4"></sequenceFlow> <sequenceFlow id="flow9" name="" sourceRef="servicetask4" targetRef="parallelgateway3"></sequenceFlow> <sequenceFlow id="flow10" name="" sourceRef="parallelgateway3" targetRef="servicetask5"></sequenceFlow> <sequenceFlow id="flow11" name="" sourceRef="servicetask5" targetRef="endevent1"></sequenceFlow> </process> </definitions>

流程实现:

package com.easyway.workflow.activiti.gateway;  
  
  
import java.util.logging.Logger;  
/** 
 * execute方法的参数DelegateExecution execution可以在流程中各个结点之间传递流程变量。  
 * <activiti:taskListener>元素的event属性,它一共包含三种事件:"create"、"assignment"、"complete",分别表示结点执行处理逻辑的时机为:
在处理类实例化时、在结点处理逻辑被指派时、在结点处理逻辑执行完成时,可以根据自己的需要进行指定。 *上述流程定义中,4个任务结点对应的处理类 * *<activiti:taskListener>元素的event属性,它一共包含三种事件:"create"、"assignment"、"complete",分别表示结点执行处理逻辑的时机为:
在处理类实例化时、在结点处理逻辑被指派时、在结点处理逻辑执行完成时,可以根据自己的需要进行指定。 * *<userTask id="servicetask2" name="产品经理同意"> <extensionElements> <activiti:taskListener event="complete" class="com.easyway.workflow.activiti.gateway.ProductManagerUserTaskListener"/> </extensionElements> </userTask> * * * 项目经理审批过程 *
@author longgangbai * * * 2011-12-17 下午07:45:47 */ public class DeveloperManagerUserTaskListener implements TaskListener { private final Logger log = Logger.getLogger(DeveloperManagerUserTaskListener.class.getName()); /* (non-Javadoc) * @see org.activiti.engine.delegate.TaskListener#notify(org.activiti.engine.delegate.DelegateTask) */ @Override public void notify(DelegateTask delegateTask) { try { Thread.sleep(10000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } log.info("variavles=" + delegateTask.getVariables()); delegateTask.setVariable("项目经理", "请假天数大约3天,同意请假。"); log.info("项目经理,请假天数大约3天,同意请假。."); } }

 

package com.easyway.workflow.activiti.gateway;  
  
  
import java.util.logging.Logger;  
/** 
  * execute方法的参数DelegateExecution execution可以在流程中各个结点之间传递流程变量。  
 * 
 *<activiti:taskListener>元素的event属性,它一共包含三种事件:"create"、"assignment"、"complete",分别表示结点执行处理逻辑的时机为:
在处理类实例化时、在结点处理逻辑被指派时、在结点处理逻辑执行完成时,可以根据自己的需要进行指定。 * <userTask id="servicetask5" name="人事部门同意" > <extensionElements> <activiti:taskListener event="complete" class="com.easyway.workflow.activiti.gateway.HumanResouceUserTaskListener"/> </extensionElements> </userTask> * 人事部门审批过程 *
@author longgangbai * * 2011-12-17 下午07:45:47 */ public class HumanResouceUserTaskListener implements TaskListener { private final Logger log = Logger.getLogger(HumanResouceUserTaskListener.class.getName()); /* (non-Javadoc) * @see org.activiti.engine.delegate.TaskListener#notify(org.activiti.engine.delegate.DelegateTask) */ @Override public void notify(DelegateTask delegateTask) { try { Thread.sleep(10000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } log.info("variavles=" + delegateTask.getVariables()); delegateTask.setVariable("人事部门", "请假天数大约3天,同意请假。"); log.info("人事部门 ,请假天数大约3天,同意请假。"); } }

 

package com.easyway.workflow.activiti.gateway;  
  
  
import java.util.logging.Logger;  
/** 
 *  execute方法的参数DelegateExecution execution可以在流程中各个结点之间传递流程变量。  
 *   
 *  <activiti:taskListener>元素的event属性,它一共包含三种事件:"create"、"assignment"、"complete",分别表示结点执行处理逻辑的时机为:
在处理类实例化时、在结点处理逻辑被指派时、在结点处理逻辑执行完成时,可以根据自己的需要进行指定。 * 配置如下 * <userTask id="servicetask4" name="项目总监同意" > <extensionElements> <activiti:taskListener event="complete" class="com.easyway.workflow.activiti.gateway.ProductBossUserTaskListener"/> </extensionElements> </userTask> * * * 项目总监审批过程 *
@author longgangbai * * 2011-12-17 下午07:45:47 */ public class ProductBossUserTaskListener implements TaskListener { private final Logger log = Logger.getLogger(ProductBossUserTaskListener.class.getName()); /* (non-Javadoc) * @see org.activiti.engine.delegate.TaskListener#notify(org.activiti.engine.delegate.DelegateTask) */ @Override public void notify(DelegateTask execution) { try { Thread.sleep(10000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } log.info("variavles=" + execution.getVariables()); execution.setVariable("项目总监", "请假天数大约3天,同意请假。"); log.info("项目总监,请假天数大约3天,同意请假。"); } }
package com.easyway.workflow.activiti.gateway;  
  
  
import java.util.logging.Logger;  
/** 
  * execute方法的参数DelegateExecution execution可以在流程中各个结点之间传递流程变量。 
  *  
  * <activiti:taskListener>元素的event属性,它一共包含三种事件:"create"、"assignment"、"complete",分别表示结点执行处理逻辑的时机为:
在处理类实例化时、在结点处理逻辑被指派时、在结点处理逻辑执行完成时,可以根据自己的需要进行指定。 * * 配置如下 * <userTask id="servicetask1" name="项目经理同意" > <extensionElements> <activiti:taskListener event="complete" class="com.easyway.workflow.activiti.gateway.DeveloperManagerUserTaskListener"/> </extensionElements> </userTask> * 产品经理审批过程 *
@author longgangbai * * 2011-12-17 下午07:45:47 */ public class ProductManagerUserTaskListener implements TaskListener { private final Logger log = Logger.getLogger(ProductManagerUserTaskListener.class.getName()); /* (non-Javadoc) * @see org.activiti.engine.delegate.TaskListener#notify(org.activiti.engine.delegate.DelegateTask) */ @Override public void notify(DelegateTask execution) { try { Thread.sleep(10000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } log.info("variavles=" + execution.getVariables()); execution.setVariable("产品经理", "请假天数大约3天,同意请假。"); log.info("产品经理,请假天数大约3天,同意请假。"); } }
package com.easyway.workflow.activiti.gateway;  
  
  
import java.util.logging.Logger;  
/** 
 *  
 *  
 * <activiti:taskListener>元素的event属性,它一共包含三种事件:"create"、"assignment"、"complete",分别表示结点执行处理逻辑的时机为:
在处理类实例化时、在结点处理逻辑被指派时、在结点处理逻辑执行完成时,可以根据自己的需要进行指定。 * * * 配置如下: * <userTask id="servicetask3" name="项目组长同意" > <extensionElements> <activiti:taskListener event="complete" class="com.easyway.workflow.activiti.gateway.TeamLeaderUserTaskListener"/> </extensionElements> </userTask> * 项目组长批准请假 * 模拟当开发人员请假小于3天,组长比准 *
@author longgangbai * * 2011-12-17 上午09:07:37 */ public class TeamLeaderUserTaskListener implements TaskListener { private final Logger log = Logger.getLogger(TeamLeaderUserTaskListener.class.getName()); /* (non-Javadoc) * @see org.activiti.engine.delegate.TaskListener#notify(org.activiti.engine.delegate.DelegateTask) */ @Override public void notify(DelegateTask execution) { try { Thread.sleep(10000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } log.info("variavles=" + execution.getVariables()); execution.setVariable("项目组长", "请假天数小于3天,同意请假。"); log.info("项目组长,请假天数小于3天,同意请假。"); } }
package com.easyway.workflow.activiti.gateway;  
  
  
import junit.framework.TestCase;  
/** 
 * 主要是在测试之前做一些初始化工作,主要包括流程引擎实例 
 * 的构建,及其流程提供的基本服务。 
 * 目的:让开发者熟悉工作流使用过程使用的几个步骤 
 * 1.加载相关的工作流全局配置文件activiti.cfg.xml配置文件信息 
 * 2.获取工作流相关的服务(RepositoryService,RuntimeService, 
 *    TaskService,HistoryService,FormService,ManagementService, 
 *    IdentityService等) 
 * 2.加载工作流文件*.bpmn20.xml信息 
 *  
 * 3.部署工作流 
 *    部署工作流由多种方式,在以后会相继讲解 
 *  
 * @author longgangbai 
 *  
 * 2011-12-17  下午07:48:59 
 */  
public abstract class AbstractTest extends TestCase {  
  
    private ProcessEngine processEngine;  
    protected String deploymentId;  
    protected RepositoryService repositoryService;  
    protected RuntimeService runtimeService;  
    protected TaskService taskService;  
    protected FormService formService;  
    protected HistoryService historyService;  
    protected IdentityService identityService;  
    protected ManagementService managementService;  
      
      
    /** 
     * 测试用例开始初始化工作 
     * 1.创建相关的工作流程对象ProcessEngine 
     * 2.创建相关的服务 
     * 3. 
     */  
    @Override  
    protected void setUp() throws Exception {  
        super.setUp();  
        //由于ProcessEngine为线程安全性对象,整个项目可以共用一个  
        if(processEngine==null) {  
            //此处使用此种方法调用的activiti的配置文件为 classpath路径下的activiti.cfg.xml  
            //采用的H2的数据库  
            processEngine = ProcessEngines.getDefaultProcessEngine();  
        }  
        //获取工作流的各种服务信息  
        repositoryService = processEngine.getRepositoryService();  
        runtimeService = processEngine.getRuntimeService();  
        taskService = processEngine.getTaskService();  
        formService = processEngine.getFormService();  
        historyService = processEngine.getHistoryService();  
        identityService = processEngine.getIdentityService();  
        managementService = processEngine.getManagementService();  
        //调用扩展的初始化工作  
        initialize();  
          
    }  
      
    /** 
     * test销毁方法 
     */  
    @Override  
    protected void tearDown() throws Exception {  
        super.tearDown();  
        destroy();  
    }  
      
    /** 
     * 便于子类的工作的初始化的扩展工作 
     *  
     *  
     * @throws Exception 
     */  
    protected abstract void initialize() throws Exception;  
    /** 
     * 便于子类的工作的销毁的扩展工作 
     *  
     * @throws Exception 
     */  
    protected abstract void destroy() throws Exception;  
}  
/** 
package com.easyway.workflow.activiti.gateway; 
import java.util.Date; 
/** 
 * 测试实例如下 
 *  
 * @author longgangbai 
 *  
 * 2011-12-18  上午12:00:03 
 */  
public class ParallelGatewayTest extends AbstractTest {   
    private String deploymentId;   
    private Date start = null;   
    private Date end = null;   
  
    @Override   
    protected void initialize() throws Exception {   
        deploymentId = repositoryService.createDeployment()   
        .addClasspathResource("diagrams/UserExecuteTask.bpmn20.xml")   
        .deploy().getId();   
    }   
  
    @Override   
    protected void destroy() throws Exception {   
            repositoryService.deleteDeployment(deploymentId, true);   
    }   
  
    @Deployment   
    public void testUnbalancedForkJoin() {   
        //获取流程实例对象  
        ProcessInstance pi = runtimeService.startProcessInstanceByKey("UserExecuteTask");   
        //获取任务服务对象的查询  
        TaskQuery query = taskService.createTaskQuery().processInstanceId(pi.getId()).orderByTaskName().asc();   
       //获取所有的任务  
        List<Task> tasks = query.list();   
        assertEquals(3, tasks.size());   
        start = new Date();   
         
        for(Task task : tasks) {  
            //执行任务  
            taskService.complete(task.getId());  
            System.out.println("name =+"+task.getName());  
            end = new Date();   
            System.out.println("" + (end.getTime()-start.getTime()) + "ms.");   
        }   
        //查询任务  
        tasks = query.list();   
        assertEquals(1, tasks.size());   
        for(Task task : tasks) {   
            taskService.complete(task.getId());   
            System.out.println("name =+"+task.getName());  
            end = new Date();   
            System.out.println("" + (end.getTime()-start.getTime()) + "ms.");   
        }   
        end = new Date();   
        System.out.println("" + (end.getTime()-start.getTime()) + "ms.");   
    }   
}  

 运行结果:

2011-12-18 13:15:29 org.activiti.engine.ProcessEngines initProcessEnginFromResource  
信息: initializing process engine for resource jar:file:/C:/activiti-rest/activiti-cfg.jar!/activiti.cfg.xml  
2011-12-18 13:15:32 org.activiti.engine.impl.ProcessEngineImpl <init>  
信息: ProcessEngine default created  
2011-12-18 13:15:32 org.activiti.engine.ProcessEngines initProcessEnginFromResource  
信息: initialised process engine default  
2011-12-18 13:15:32 org.activiti.engine.impl.jobexecutor.JobAcquisitionThread run  
信息: JobAcquisitionThread starting to acquire jobs  
2011-12-18 13:15:32 org.activiti.engine.impl.bpmn.deployer.BpmnDeployer deploy  
信息: Processing resource diagrams/UserExecuteTask.bpmn20.xml  
2011-12-18 13:15:33 org.activiti.engine.impl.bpmn.parser.BpmnParse parseDefinitionsAttributes  
信息: XMLSchema currently not supported as typeLanguage  
2011-12-18 13:15:33 org.activiti.engine.impl.bpmn.parser.BpmnParse parseDefinitionsAttributes  
信息: XPath currently not supported as expressionLanguage  
2011-12-18 13:15:43 com.easyway.workflow.activiti.gateway.ProductManagerUserTaskListener notify  
信息: variavles={}  
2011-12-18 13:15:43 com.easyway.workflow.activiti.gateway.ProductManagerUserTaskListener notify  
信息: 产品经理,请假天数大约3天,同意请假。  
name =+产品经理同意  
10094ms.  
2011-12-18 13:15:53 com.easyway.workflow.activiti.gateway.TeamLeaderUserTaskListener notify  
信息: variavles={产品经理=请假天数大约3天,同意请假。}  
2011-12-18 13:15:53 com.easyway.workflow.activiti.gateway.TeamLeaderUserTaskListener notify  
信息: 项目组长,请假天数小于3天,同意请假。  
name =+项目组长同意  
20172ms.  
2011-12-18 13:16:03 com.easyway.workflow.activiti.gateway.DeveloperManagerUserTaskListener notify  
信息: variavles={项目组长=请假天数小于3天,同意请假。, 产品经理=请假天数大约3天,同意请假。}  
2011-12-18 13:16:03 com.easyway.workflow.activiti.gateway.DeveloperManagerUserTaskListener notify  
信息: 项目经理,请假天数大约3天,同意请假。.  
name =+项目经理同意  
30313ms.  
2011-12-18 13:16:13 com.easyway.workflow.activiti.gateway.ProductBossUserTaskListener notify  
信息: variavles={项目组长=请假天数小于3天,同意请假。, 项目经理=请假天数大约3天,同意请假。, 产品经理=请假天数大约3天,同意请假。}  
2011-12-18 13:16:13 com.easyway.workflow.activiti.gateway.ProductBossUserTaskListener notify  
信息: 项目总监,请假天数大约3天,同意请假。  
name =+项目总监同意  
40485ms.  
40485ms.  

 

 

Activiti手动执行的应用(UserTask)

标签:statistic   工作   frame   string   构建   点击   sch   技术   engine   

原文地址:https://www.cnblogs.com/liaojie970/p/8976051.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!