关闭

工作流中ServiceTask的各种方法

335人阅读 评论(0) 收藏 举报

      在工作流中的activiti的serviceTask几种方法:

 

模拟自动执行任务工作流的流程图:

 

 

 

 

工作流配置:

serviceTaskExpression.bpmn20.xml

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="Examples">  
  7.     
  8.   <process id="serviceTaskProcess">  
  9.   <startEvent id="start"  />  
  10.    <sequenceFlow id="flow1" sourceRef="start" targetRef="serviceTask" />  
  11.      
  12.    <!-- 调用委托类的依赖注入功能 -->  
  13.     <serviceTask id="serviceTask" name="Java service invocation"    
  14.       activiti:class="com.easyway.activiti.servicetask.ReverseStringsFieldInjected">   
  15.           <extensionElements>   
  16.             <activiti:field name="text1">   
  17.               <activiti:expression>Welcome to Beijing</activiti:expression>   
  18.             </activiti:field>   
  19.             <activiti:field name="text2">   
  20.                <activiti:expression>Hello Mr. Bai</activiti:expression>   
  21.             </activiti:field>   
  22.           </extensionElements>   
  23.     </serviceTask>  
  24.     
  25.     <sequenceFlow id="flow2" sourceRef="serviceTask" targetRef="serviceTask1" />  
  26.       
  27.     <!-- 调用spring bean中的相关的委托bean -->  
  28.     <serviceTask id="serviceTask1" activiti:delegateExpression="${delegateExpressionBean}" />  
  29.       
  30.       
  31.       
  32.      <sequenceFlow id="flow3" sourceRef="serviceTask1" targetRef="servicetask2" />  
  33.        
  34.        
  35.      <!-- 调用spring bean中的方法 -->  
  36.     <serviceTask id="servicetask2"    
  37.              name="My Java Service Task"    
  38.              activiti:expression="#{sentenceGenerator.getSentence()}" />  
  39.    
  40.     <sequenceFlow id="flow4" sourceRef="servicetask2" targetRef="servicetask3" />  
  41.       
  42.   <!-- 调用spring bean中的相关的委托bean -->  
  43.     <serviceTask id="servicetask3"    
  44.          name="Java service invocation"    
  45.          activiti:class="com.easyway.activiti.servicetask.ToUppercase">   
  46.         <extensionElements>   
  47.           <activiti:field name="text" stringValue="Hello World" />   
  48.         </extensionElements>              
  49.     </serviceTask>       
  50.   
  51.     <sequenceFlow id="flow5" sourceRef="servicetask3" targetRef="endEvent" />  
  52.     <endEvent id="endEvent" />  
  53.   </process>  
  54.   
  55. </definitions>  

 

工作流的实现:

Java代码  收藏代码
  1. package com.easyway.activiti.servicetask;  
  2.   
  3. import org.activiti.engine.delegate.DelegateExecution;  
  4.   
  5. /** 
  6.  * 从配置文件注入多个属性类型必须为Expression 
  7.  * @author longgangbai 
  8.  *  
  9.  * 2011-12-20  下午10:08:01 
  10.  */  
  11. public class ReverseStringsFieldInjected implements JavaDelegate {   
  12.        
  13.       private Expression text1;   
  14.       private Expression text2;   
  15.        
  16.       public void execute(DelegateExecution execution) {   
  17.         String value1 = (String) text1.getValue(execution);   
  18.         execution.setVariable("var1"new StringBuffer(value1).reverse().toString());   
  19.        
  20.         String value2 = (String) text2.getValue(execution);   
  21.         execution.setVariable("var2"new StringBuffer(value2).reverse().toString());   
  22.       }   
  23.     }  

 

 

Java代码  收藏代码
  1. package com.easyway.activiti.servicetask;  
  2.   
  3. import org.activiti.engine.delegate.DelegateExecution;  
  4.   
  5. /** 
  6.  *  从配置文件注入一个字符串用于变为写 
  7.  * @author longgangbai 
  8.  *  
  9.  * 2011-12-20  下午09:56:59 
  10.  */  
  11. public class ToUppercase implements JavaDelegate {   
  12.       private Expression text;   
  13.       public void execute(DelegateExecution execution) throws Exception {   
  14.           String value1 = (String) text.getValue(execution);   
  15.            execution.setVariable("input", value1);   
  16.       }   
  17.          
  18.     }  

 

 

servicetaskSpringTest-context.xml

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <beans xmlns="http://www.springframework.org/schema/beans"   
  4.        xmlns:context="http://www.springframework.org/schema/context"  
  5.        xmlns:tx="http://www.springframework.org/schema/tx"  
  6.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  7.        xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans.xsd  
  8.                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  9.                            http://www.springframework.org/schema/tx      http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">  
  10.   
  11.   <bean id="sentenceGenerator" class="com.easyway.activiti.servicetask.SentenceGenerator" />  
  12.     
  13.   <bean id="delegateExpressionBean" class="com.easyway.activiti.servicetask.DelegateExpressionBean">  
  14.     <property name="sentenceGenerator"  ref="sentenceGenerator" />  
  15.   </bean>  
  16.     
  17.   <bean id="sentenceToUpperCaseBean" class="com.easyway.activiti.servicetask.SentenceToUpperCaseBean">  
  18.     <property name="sentenceGenerator" ref="sentenceGenerator" />  
  19.   </bean>  
  20.     
  21.   <bean id="myExecutionListenerBean" class="com.easyway.activiti.servicetask.ActivitiExecutionListenerBean" />  
  22.   <bean id="myTaskListener" class="com.easyway.activiti.servicetask.ActivitiTaskListener" />  
  23.     
  24.   <bean id="myTaskListenerBean" class="com.easyway.activiti.servicetask.ActivitiTaskListenerBean" />  
  25.   
  26.   <bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">  
  27.     <property name="driverClass" value="org.h2.Driver" />  
  28.     <property name="url" value="jdbc:h2:mem:activiti;DB_CLOSE_DELAY=1000" />  
  29.     <property name="username" value="sa" />  
  30.     <property name="password" value="" />  
  31.   </bean>  
  32.   
  33.   <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  34.     <property name="dataSource" ref="dataSource" />  
  35.   </bean>  
  36.     
  37.   <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">  
  38.     <property name="dataSource" ref="dataSource" />  
  39.     <property name="transactionManager" ref="transactionManager" />  
  40.     <property name="databaseSchemaUpdate" value="create-drop" />  
  41.     <property name="jobExecutorActivate" value="false" />  
  42.   </bean>  
  43.     
  44.   <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">  
  45.     <property name="processEngineConfiguration" ref="processEngineConfiguration" />  
  46.   </bean>  
  47.     
  48.   <bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService" />  
  49.   <bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService" />  
  50.   <bean id="taskService" factory-bean="processEngine" factory-method="getTaskService" />  
  51.   <bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService" />  
  52.   <bean id="managementService" factory-bean="processEngine" factory-method="getManagementService" />  
  53.   
  54. </beans>  

 

 

测试类:

Java代码  收藏代码
  1. package com.easyway.activiti.servicetask;  
  2.   
  3. import org.activiti.engine.runtime.ProcessInstance;  
  4. import org.activiti.engine.test.Deployment;  
  5. import org.activiti.spring.impl.test.SpringActivitiTestCase;  
  6. import org.springframework.test.context.ContextConfiguration;  
  7.   
  8. /** 
  9.  * 工作流中ServiceTask的各种方法 
  10.  * 1.通过activiti:class使用相关的javaDelegate的子类。 
  11.  *    <serviceTask id="serviceTask" name="Java service invocation"   
  12.       activiti:class="com.easyway.activiti.servicetask.ReverseStringsFieldInjected">  
  13.           <extensionElements>  
  14.             <activiti:field name="text1">  
  15.               <activiti:expression>Welcome to Beijing</activiti:expression>  
  16.             </activiti:field>  
  17.             <activiti:field name="text2">  
  18.                <activiti:expression>Hello Mr. Bai</activiti:expression>  
  19.             </activiti:field>  
  20.           </extensionElements>  
  21.     </serviceTask> 
  22.     2.通过spring的使用委派表达式实现 
  23.       <serviceTask id="serviceTask1" activiti:delegateExpression="${delegateExpressionBean}" /> 
  24.     3.通过表达式将实现 
  25.         <serviceTask id="servicetask2"   
  26.              name="My Java Service Task"   
  27.              activiti:expression="#{sentenceGenerator.getSentence()}" /> 
  28.      
  29.     4.通过activiti:class注入相关的属性 
  30.         <serviceTask id="servicetask3"   
  31.          name="Java service invocation"   
  32.          activiti:class="com.easyway.activiti.servicetask.ToUppercase">  
  33.         <extensionElements>  
  34.           <activiti:field name="text" stringValue="Hello World" />  
  35.         </extensionElements>             
  36.     </serviceTask>     
  37.  * @author longgangbai 
  38.  *  
  39.  * 2011-12-20  下午09:29:23 
  40.  */  
  41. @ContextConfiguration("classpath:servicetaskSpringTest-context.xml")  
  42. public class ServiceTaskExpressionTest extends SpringActivitiTestCase {  
  43.     
  44.   @Deployment(resources={"diagrams/serviceTaskExpression.bpmn20.xml"})  
  45.   public void testDelegateExpression() {  
  46.     ProcessInstance procInst = runtimeService.startProcessInstanceByKey("serviceTaskProcess");  
  47.     assertEquals(true,procInst.isEnded());  
  48.   }  
  49.   
  50. }  

 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:22094次
    • 积分:710
    • 等级:
    • 排名:千里之外
    • 原创:38篇
    • 转载:75篇
    • 译文:0篇
    • 评论:0条
    文章分类