工作流中的UEL表达式的使用

公共spring配置文件:

Xml代码   收藏代码
  1. xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"   
  3.        xmlns:context="http://www.springframework.org/schema/context"  
  4.        xmlns:tx="http://www.springframework.org/schema/tx"  
  5.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  6.        xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans.xsd  
  7.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  8.                            http://www.springframework.org/schema/tx      http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">  
  9.   
  10.   
  11.   <bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">  
  12.     <property name="driverClass" value="org.h2.Driver" />  
  13.     <property name="url" value="jdbc:h2:mem:activiti;DB_CLOSE_DELAY=1000" />  
  14.     <property name="username" value="sa" />  
  15.     <property name="password" value="" />  
  16.   bean>  
  17.    
  18.   <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  19.     <property name="dataSource" ref="dataSource" />  
  20.   bean>  
  21.   
  22.   <bean id="bean1" class="com.easyway.workflow.activiti.SentenceGenerator" />  
  23.     
  24.       
  25.   <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">  
  26.     <property name="dataSource" ref="dataSource" />  
  27.     <property name="transactionManager" ref="transactionManager" />  
  28.     <property name="jobExecutorActivate" value="false" />  
  29.       
  30.     <property name="databaseSchemaUpdate" value="create-drop" />  
  31.       
  32.     <property name="deploymentResources" value="classpath*:diagrams/*.bpmn20.xml" />   
  33.       
  34.     <property name="beans">  
  35.       <map>  
  36.         <entry key="bean1" value-ref="bean1" />  
  37.       map>  
  38.     property>  
  39.   bean>  
  40.     
  41.     
  42.   <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">  
  43.     <property name="processEngineConfiguration" ref="processEngineConfiguration" />  
  44.   bean>  
  45.     
  46.   <bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService" />  
  47.   <bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService" />  
  48.   <bean id="taskService" factory-bean="processEngine" factory-method="getTaskService" />  
  49.   <bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService" />  
  50.   <bean id="managementService" factory-bean="processEngine" factory-method="getManagementService" />  
  51.   
  52. beans>  
Java代码   收藏代码
  1. package com.easyway.workflow.activiti;  
  2. /** 
  3.  * 
  4.  *  工作流中的activiti中的UEL的两种用法要求: 
  5.  *  1.UEL使用的bean必须为spring暴露给activiti的bean。 
  6.  *  2.如果采用方法必须为public 
  7.  *    
  8.  *    
  9.  *  3.如果采用属性方位则必须为属性提供getter/setter方法 
  10.  *      
  11.  *     
  12.  *     
  13.  *     
  14.  * @author longgangbai 
  15.  *  
  16.  * 2011-12-18  下午09:48:36 
  17.  */  
  18. public class SentenceGenerator {  
  19.     
  20.   private String level;  
  21.     
  22.     /** 
  23.      * @return the level 
  24.      */  
  25.     public String getLevel() {  
  26.         return level;  
  27.     }  
  28.       
  29.     /** 
  30.      * @param level the level to set 
  31.      */  
  32.     public void setLevel(String level) {  
  33.         this.level = level;  
  34.     }  
  35.       
  36.     public String getSentence() {  
  37.         return "Activiti BPMN 2.0 process engine";  
  38.       }  
  39.   
  40. }  

  

 

实例一:

 

配置:

Xml代码   收藏代码
  1. xml version="1.0" encoding="UTF-8"?>  
  2. <definitions id="definitions"   
  3.   xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"  
  4.   xmlns:activiti="http://activiti.org/bpmn"  
  5.    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  6.   targetNamespace="bankService">  
  7.   
  8.   <process id="springexclusiveGateway">  
  9.     <startEvent id="theStart" />   
  10.       
  11.     <exclusiveGateway id="fork"  name="abc">  
  12.        <documentation> 可以理解为多种方式都可以达到目的地,但是适合自己的只有一条documentation>  
  13.     exclusiveGateway>  
  14.        
  15.      <exclusiveGateway id="join" name="abc" />  
  16.     <sequenceFlow id="flow1" sourceRef="theStart" targetRef="fork" />   
  17.     <sequenceFlow id="flow2" sourceRef="fork" targetRef="receivePayment" >   
  18.          
  19.     <conditionExpression xsi:type="tFormalExpression">${paymentReceived == false}conditionExpression>   
  20.     sequenceFlow>   
  21.     <sequenceFlow  id="flow3"  sourceRef="fork" targetRef="shipOrder" >   
  22.     <conditionExpression xsi:type="tFormalExpression">${shipOrder == true}conditionExpression>   
  23.     sequenceFlow>  
  24.        
  25.        
  26.     <userTask id="receivePayment" name="Receive Payment" />     
  27.     <sequenceFlow id="flow4" sourceRef="receivePayment" targetRef="join" />   
  28.        
  29.     <userTask id="shipOrder" name="Ship Order" />    
  30.     <sequenceFlow id="flow5" sourceRef="shipOrder" targetRef="join" />   
  31.        
  32.        
  33.     <sequenceFlow id="flow6" sourceRef="join" targetRef="archiveOrder" />   
  34.        
  35.        
  36.     <userTask id="archiveOrder" name="Archive Order" >  
  37.           <potentialOwner>   
  38.       <resourceAssignmentExpression>   
  39.         <formalExpression>user(kermit), group(management)formalExpression>   
  40.       resourceAssignmentExpression>   
  41.     potentialOwner>   
  42.         
  43.     userTask>  
  44.         
  45.     <sequenceFlow sourceRef="archiveOrder" targetRef="theEnd" />   
  46.        
  47.     <endEvent id="theEnd" />    
  48.       
  49.   process>  
  50.   definitions>  

 

代码:

Java代码   收藏代码
  1. package com.easyway.workflow.activiti;  
  2.   
  3. import java.util.HashMap;  
  4.   
  5. /** 
  6.  *  
  7.  *    
  8.  *  可以理解为多种方式都可以达到目的地,但是适合自己的只有一条 
  9.  *   
  10.  *       
  11.      
  12.             
  13.         
  14.         user(kermit), group(management)  
  15.         
  16.       
  17.  *   
  18.  *   
  19.  *  全局属性的使用 
  20.  *   
  21.  *   
  22.  * @author longgangbai 
  23.  *  
  24.  * 2011-12-18  下午10:33:15 
  25.  */  
  26. public class SpringExclusiveGateway {  
  27.     public static void main(String[] args) {  
  28.         //加载spring配置和工作流流程处理配置  
  29.         ApplicationContext ctx=new ClassPathXmlApplicationContext("application-context-expression.xml");  
  30.         //工作流相关的各种服务  
  31.         HistoryService historyService=(HistoryService)ctx.getBean("historyService");  
  32.         RuntimeService runtimeService=(RuntimeService)ctx.getBean("runtimeService");  
  33.         TaskService taskService=(TaskService)ctx.getBean("taskService");  
  34.         //设置调用流程过程中使用的参数  
  35.         HashMap variableMap = new HashMap();   
  36.         variableMap.put("paymentReceived"true);   
  37.         variableMap.put("shipOrder"true);   
  38.         //流程实例对象  
  39.         ProcessInstance pi = runtimeService.startProcessInstanceByKey("springexclusiveGateway",variableMap);   
  40.         TaskQuery query = taskService.createTaskQuery()   
  41.                        .processInstanceId(pi.getId())   
  42.                        .orderByTaskName()   
  43.                        .asc();   
  44.   
  45.         List tasks = query.list();   
  46.         System.out.println("tasksize ="+tasks.size());  
  47.           
  48.         Task task = tasks.get(0);   
  49.         System.out.println("Ship Order ="+task.getName());  
  50.   
  51.     }  
  52.   
  53. }  

 

 

实例二:

配置:

Xml代码   收藏代码
  1. xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <definitions id="definitions"   
  4.   xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"  
  5.   xmlns:activiti="http://activiti.org/bpmn"  
  6.   targetNamespace="Expressions">  
  7.     
  8.   <process id="limitedExpressionProcess">  
  9.     <startEvent id="start" />  
  10.     <sequenceFlow id="flow1" sourceRef="start" targetRef="serviceTask" />  
  11.      
  12.     <serviceTask id="serviceTask" name="定期会议开始" activiti:expression="${bean1.getSentence()}"  >  
  13.     serviceTask>  
  14.       
  15.     <sequenceFlow id="flow2" sourceRef="serviceTask" targetRef="userTask" />  
  16.       
  17.     <userTask id="userTask" name="会议中....." />   
  18.       
  19.     <sequenceFlow id="flow3" sourceRef="userTask" targetRef="serviceTask2" />  
  20.       
  21.       
  22.     <serviceTask id="serviceTask2" name="定期会议结束...." activiti:expression="${bean1.level}" />  
  23.       
  24.      <sequenceFlow id="flow4" sourceRef="serviceTask2" targetRef="end" />  
  25.     <endEvent id="end" />  
  26.       
  27.   process>  
  28.   
  29. definitions>  

 

代码:

Java代码   收藏代码
  1. /** 
  2. package com.easyway.workflow.activiti; 
  3.  
  4. import java.util.List; 
  5.  
  6. /** 
  7.  * 测试工作流中的UEL表达式的使用 
  8.  *  
  9.  * @author longgangbai 
  10.  *  
  11.  * 2011-12-18  下午07:35:29 
  12.  */  
  13. public class SpringLimitedExpressionActiviti {  
  14.     public static void main(String[] args) throws InterruptedException {  
  15.         //加载spring配置和工作流流程处理配置  
  16.         ApplicationContext ctx=new ClassPathXmlApplicationContext("application-context-expression.xml");  
  17.         //工作流相关的各种服务  
  18.         HistoryService historyService=(HistoryService)ctx.getBean("historyService");  
  19.         RuntimeService runtimeService=(RuntimeService)ctx.getBean("runtimeService");  
  20.         TaskService taskService=(TaskService)ctx.getBean("taskService");  
  21.         //流程实例对象  
  22.         ProcessInstance processInstance=runtimeService.startProcessInstanceByKey("limitedExpressionProcess");  
  23.         //查询当前任务数据  
  24.         System.out.println("处理任务之前:"+runtimeService.createProcessInstanceQuery().count());  
  25.         List taskList=taskService.createTaskQuery().list();  
  26.         //手动分配代办任务  
  27.         for (Task task : taskList) {  
  28.             taskService.claim(task.getId(), "kermit");  
  29.         }  
  30.         //手动处理完成任务  
  31.         for (Task task : taskList) {  
  32.             taskService.complete(task.getId());  
  33.         }  
  34.         //检查剩余任务数  
  35.         System.out.println("处理任务之前:"+taskService.createTaskQuery().count());  
  36.          // verify that the process is actually finished   
  37.         //验证流程是否处理完毕  
  38.         HistoricProcessInstance historicProcessInstance =    
  39.           historyService.createHistoricProcessInstanceQuery().processInstanceId(processInstance.getProcessInstanceId()).singleResult();   
  40.         System.out.println("Process instance end time: " + historicProcessInstance.getEndTime());  
  41.         //6分钟时间等待第二次执行  
  42.         Thread.sleep(6*60*1000);  
  43.           
  44.     }  
  45.   
  46. }  

 

实例三

配置:

Xml代码   收藏代码
  1. xml version="1.0" encoding="UTF-8"?>  
  2. <definitions id="definitions"   
  3.   xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"  
  4.   xmlns:activiti="http://activiti.org/bpmn"  
  5.    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  6.   targetNamespace="bankService">  
  7.   <process id="springparallelGateway">  
  8.     <startEvent id="theStart" />   
  9.         <sequenceFlow id="flow1" sourceRef="theStart" targetRef="fork" />   
  10.            
  11.         <parallelGateway id="fork" >  
  12.            <documentation> 理解为多种方式都可以到达目的地,并行的互不干涉,进度documentation>  
  13.         parallelGateway>  
  14.            
  15.         <sequenceFlow sourceRef="fork" targetRef="receivePayment" />   
  16.         <sequenceFlow sourceRef="fork" targetRef="shipOrder" />   
  17.            
  18.         <userTask id="receivePayment" name="Receive Payment" >  
  19.            <humanPerformer>   
  20.               <resourceAssignmentExpression>   
  21.                 <formalExpression>kermitformalExpression>   
  22.               resourceAssignmentExpression>   
  23.             humanPerformer>   
  24.         userTask>  
  25.              
  26.         <sequenceFlow sourceRef="receivePayment" targetRef="join" />   
  27.            
  28.         <userTask id="shipOrder" name="Ship Order" />    
  29.         <sequenceFlow sourceRef="shipOrder" targetRef="join" />   
  30.            
  31.         <parallelGateway id="join" />   
  32.         <sequenceFlow sourceRef="join" targetRef="archiveOrder" />   
  33.            
  34.         <userTask id="archiveOrder" name="Archive Order" />    
  35.         <sequenceFlow sourceRef="archiveOrder" targetRef="theEnd" />   
  36.            
  37.         <endEvent id="theEnd" />  
  38.       
  39.   process>  
  40.   definitions>  

 

 

代码:

 

Java代码   收藏代码
  1. /** 
  2. package com.easyway.workflow.activiti; 
  3.  
  4. import java.util.List; 
  5.  
  6. /** 
  7.  * 测试工作流中的UEL表达式的使用 
  8.  *  
  9.  *       
  10.  *     理解为多种方式都可以到达目的地,并行的互不干涉,进度 
  11.  *      
  12.  *      
  13.          
  14.              
  15.                 
  16.                 kermit  
  17.                 
  18.               
  19.          
  20.          
  21.  *  
  22.  * @author longgangbai 
  23.  *  
  24.  * 2011-12-18  下午07:35:29 
  25.  */  
  26. public class SpringParallelExpressionActiviti {  
  27.     public static void main(String[] args) throws InterruptedException {  
  28.         //加载spring配置和工作流流程处理配置  
  29.         ApplicationContext ctx=new ClassPathXmlApplicationContext("application-context-expression.xml");  
  30.         //工作流相关的各种服务  
  31.         HistoryService historyService=(HistoryService)ctx.getBean("historyService");  
  32.         RuntimeService runtimeService=(RuntimeService)ctx.getBean("runtimeService");  
  33.         TaskService taskService=(TaskService)ctx.getBean("taskService");  
  34.         //流程实例对象  
  35.         ProcessInstance processInstance=runtimeService.startProcessInstanceByKey("springparallelGateway");  
  36.         TaskQuery query = taskService.createTaskQuery()   
  37.                                  .processInstanceId(processInstance.getId())   
  38.                                  .orderByTaskName()   
  39.                                  .asc();   
  40.            
  41.         List tasks = query.list();   
  42.         System.out.println(tasks.size());   
  43.   
  44.         Task task1 = tasks.get(0);   
  45.         System.out.println("Receive Payment ="+ task1.getName());   
  46.         Task task2 = tasks.get(1);   
  47.         System.out.println("Ship Order"+task2.getName());  
  48.         //查看指定人的任务  
  49.         List kermittasks = taskService.createTaskQuery().taskAssignee("kermit").list();  
  50.         System.out.println("kermit task number  ="+ kermittasks.size());   
  51.            
  52.           
  53.     }  
  54.   
  55. }  

你可能感兴趣的:(工作流中的UEL表达式的使用)