在工作流中的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()); } }