工作流中ServiceTask的各种方法

系统 1366 0

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

 

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

 

 

 

 

工作流配置:

serviceTaskExpression.bpmn20.xml

    <?xml version="1.0" encoding="UTF-8"?>

<definitions id="definitions" 
  xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"
  xmlns:activiti="http://activiti.org/bpmn"
  targetNamespace="Examples">
  
  <process id="serviceTaskProcess">
  <startEvent id="start"  />
   <sequenceFlow id="flow1" sourceRef="start" targetRef="serviceTask" />
   
   <!-- 调用委托类的依赖注入功能 -->
    <serviceTask id="serviceTask" name="Java service invocation"  
	  activiti:class="com.easyway.activiti.servicetask.ReverseStringsFieldInjected"> 
		  <extensionElements> 
		    <activiti:field name="text1"> 
		      <activiti:expression>Welcome to Beijing</activiti:expression> 
		    </activiti:field> 
		    <activiti:field name="text2"> 
		       <activiti:expression>Hello Mr. Bai</activiti:expression> 
		    </activiti:field> 
		  </extensionElements> 
	</serviceTask>
  
    <sequenceFlow id="flow2" sourceRef="serviceTask" targetRef="serviceTask1" />
    
    <!-- 调用spring bean中的相关的委托bean -->
    <serviceTask id="serviceTask1" activiti:delegateExpression="${delegateExpressionBean}" />
    
    
    
     <sequenceFlow id="flow3" sourceRef="serviceTask1" targetRef="servicetask2" />
     
     
     <!-- 调用spring bean中的方法 -->
    <serviceTask id="servicetask2"  
             name="My Java Service Task"  
             activiti:expression="#{sentenceGenerator.getSentence()}" />
 
    <sequenceFlow id="flow4" sourceRef="servicetask2" targetRef="servicetask3" />
    
  <!-- 调用spring bean中的相关的委托bean -->
    <serviceTask id="servicetask3"  
         name="Java service invocation"  
         activiti:class="com.easyway.activiti.servicetask.ToUppercase"> 
	    <extensionElements> 
	      <activiti:field name="text" stringValue="Hello World" /> 
	    </extensionElements>            
    </serviceTask>     

    <sequenceFlow id="flow5" sourceRef="servicetask3" targetRef="endEvent" />
    <endEvent id="endEvent" />
  </process>

</definitions>
  

 

工作流的实现:

    package com.easyway.activiti.servicetask;

import org.activiti.engine.delegate.DelegateExecution;

/**
 * 从配置文件注入多个属性类型必须为Expression
 * @author longgangbai
 * 
 * 2011-12-20  下午10:08:01
 */
public class ReverseStringsFieldInjected implements JavaDelegate { 
	 
	  private Expression text1; 
	  private Expression text2; 
	 
	  public void execute(DelegateExecution execution) { 
	    String value1 = (String) text1.getValue(execution); 
	    execution.setVariable("var1", new StringBuffer(value1).reverse().toString()); 
	 
	    String value2 = (String) text2.getValue(execution); 
	    execution.setVariable("var2", new StringBuffer(value2).reverse().toString()); 
	  } 
	}
  

 

 

    package com.easyway.activiti.servicetask;

import org.activiti.engine.delegate.DelegateExecution;

/**
 *  从配置文件注入一个字符串用于变为写
 * @author longgangbai
 * 
 * 2011-12-20  下午09:56:59
 */
public class ToUppercase implements JavaDelegate { 
	  private Expression text; 
	  public void execute(DelegateExecution execution) throws Exception { 
		  String value1 = (String) text.getValue(execution); 
		   execution.setVariable("input", value1); 
	  } 
	   
	}

  

 

 

servicetaskSpringTest-context.xml

    <?xml version="1.0" encoding="UTF-8"?>

<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="sentenceGenerator" class="com.easyway.activiti.servicetask.SentenceGenerator" />
  
  <bean id="delegateExpressionBean" class="com.easyway.activiti.servicetask.DelegateExpressionBean">
    <property name="sentenceGenerator"  ref="sentenceGenerator" />
  </bean>
  
  <bean id="sentenceToUpperCaseBean" class="com.easyway.activiti.servicetask.SentenceToUpperCaseBean">
    <property name="sentenceGenerator" ref="sentenceGenerator" />
  </bean>
  
  <bean id="myExecutionListenerBean" class="com.easyway.activiti.servicetask.ActivitiExecutionListenerBean" />
  <bean id="myTaskListener" class="com.easyway.activiti.servicetask.ActivitiTaskListener" />
  
  <bean id="myTaskListenerBean" class="com.easyway.activiti.servicetask.ActivitiTaskListenerBean" />

  <bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
    <property name="driverClass" value="org.h2.Driver" />
    <property name="url" value="jdbc:h2:mem:activiti;DB_CLOSE_DELAY=1000" />
    <property name="username" value="sa" />
    <property name="password" value="" />
  </bean>

  <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
  </bean>
  
  <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
    <property name="dataSource" ref="dataSource" />
    <property name="transactionManager" ref="transactionManager" />
    <property name="databaseSchemaUpdate" value="create-drop" />
    <property name="jobExecutorActivate" value="false" />
  </bean>
  
  <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
    <property name="processEngineConfiguration" ref="processEngineConfiguration" />
  </bean>
  
  <bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService" />
  <bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService" />
  <bean id="taskService" factory-bean="processEngine" factory-method="getTaskService" />
  <bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService" />
  <bean id="managementService" factory-bean="processEngine" factory-method="getManagementService" />

</beans>
  

 

 

测试类:

    package com.easyway.activiti.servicetask;

import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.test.Deployment;
import org.activiti.spring.impl.test.SpringActivitiTestCase;
import org.springframework.test.context.ContextConfiguration;

/**
 * 工作流中ServiceTask的各种方法
 * 1.通过activiti:class使用相关的javaDelegate的子类。
 *    <serviceTask id="serviceTask" name="Java service invocation"  
	  activiti:class="com.easyway.activiti.servicetask.ReverseStringsFieldInjected"> 
		  <extensionElements> 
		    <activiti:field name="text1"> 
		      <activiti:expression>Welcome to Beijing</activiti:expression> 
		    </activiti:field> 
		    <activiti:field name="text2"> 
		       <activiti:expression>Hello Mr. Bai</activiti:expression> 
		    </activiti:field> 
		  </extensionElements> 
	</serviceTask>
	2.通过spring的使用委派表达式实现
	  <serviceTask id="serviceTask1" activiti:delegateExpression="${delegateExpressionBean}" />
    3.通过表达式将实现
        <serviceTask id="servicetask2"  
             name="My Java Service Task"  
             activiti:expression="#{sentenceGenerator.getSentence()}" />
    
    4.通过activiti:class注入相关的属性
        <serviceTask id="servicetask3"  
         name="Java service invocation"  
         activiti:class="com.easyway.activiti.servicetask.ToUppercase"> 
	    <extensionElements> 
	      <activiti:field name="text" stringValue="Hello World" /> 
	    </extensionElements>            
    </serviceTask>    
 * @author longgangbai
 * 
 * 2011-12-20  下午09:29:23
 */
@ContextConfiguration("classpath:servicetaskSpringTest-context.xml")
public class ServiceTaskExpressionTest extends SpringActivitiTestCase {
  
  @Deployment(resources={"diagrams/serviceTaskExpression.bpmn20.xml"})
  public void testDelegateExpression() {
    ProcessInstance procInst = runtimeService.startProcessInstanceByKey("serviceTaskProcess");
    assertEquals(true,procInst.isEnded());
  }

}
  

 

工作流中ServiceTask的各种方法


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论