activiti5第四弹----serviceTask中的java服务任务
activiti.cfg.xml內(nèi)容:
<?xml version="1.0"?> <beans default-lazy-init="false"xsi:schemaLocation=" http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jee="http://www.springframework.org/schema/jee"xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context"xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://www.springframework.org/schema/beans"><beanclass="org.activiti.engine.impl.cfg.StandaloneInMemProcessEngineConfiguration"id="processEngineConfiguration"><property name="jdbcUrl" value="jdbc:mysql://localhost:3306/activi1" /><property name="jdbcDriver" value="com.mysql.jdbc.Driver" /><property name="jdbcUsername" value="root" /><property name="jdbcPassword" value="root" /><property name="databaseSchemaUpdate" value="true" /><property name="jobExecutorActivate" value="true" /><property name="mailServerHost" value="mail.my-corp.com" /><property name="mailServerPort" value="5025" /><property name="history" value="full"></property></bean> </beans>方式一:使用java class來(lái)實(shí)現(xiàn)java服務(wù)任務(wù)
HelloService內(nèi)容:
package org.mpc.final_activiti;import org.activiti.engine.delegate.DelegateExecution; import org.activiti.engine.delegate.JavaDelegate;/*** * 作為activiti的ServiceTask的實(shí)現(xiàn),必須實(shí)現(xiàn)JavaDelegate接口,但是不需要實(shí)現(xiàn)序列化接口* * activiti會(huì)使用反射機(jī)制將類初始化,因此在實(shí)現(xiàn)JavaDelegate接口的時(shí)候需要提供一個(gè)* 無(wú)參數(shù)的構(gòu)造器,否則會(huì)拋出異常。* * @author mpc**/ public class HelloService implements JavaDelegate {@Overridepublic void execute(DelegateExecution arg0) throws Exception {System.out.println("---------------------------------------------");System.out.println();System.out.println("Hello Service " + this.toString()+ "Is Saying Hello To Every One !");System.out.println("---------------------------------------------");System.out.println();}}測(cè)試方法: package final_activiti.progress;import org.activiti.engine.impl.test.PluggableActivitiTestCase; import org.activiti.engine.test.Deployment; import org.junit.Test;public class ServiceTask1Test extends PluggableActivitiTestCase {@Test@Deployment(resources = "final_activiti/progress/serviceTask_01.bpmn")public void test() {assertProcessEnded(runtimeService.startProcessInstanceByKey("service1").getId());}}
測(cè)試結(jié)果:
從測(cè)試結(jié)果可以看到,我們?cè)诹鞒讨卸x的連個(gè)Service task都執(zhí)行了,而且可以從紅線標(biāo)注處看到這兩個(gè)Service是不一樣的,也就是說(shuō)activiti在每次需要執(zhí)行一個(gè)Service task 的時(shí)候都重新創(chuàng)建了服務(wù)對(duì)象。
方式二:使用 Delegate expression來(lái)實(shí)現(xiàn)java服務(wù)任務(wù)
delegate對(duì)應(yīng)的java類:
package org.mpc.final_activiti;import java.io.Serializable;import org.activiti.engine.delegate.DelegateExecution; import org.activiti.engine.delegate.JavaDelegate;public class Helloservice1 implements Serializable, JavaDelegate {/*** 實(shí)現(xiàn)JavaDelegate接口,使用其中的execute方法 由于要放入流程定義中,所以要實(shí)現(xiàn)可序列話接口* */private static final long serialVersionUID = 5593437463482732772L;@Overridepublic void execute(DelegateExecution arg0) throws Exception {System.out.println("---------------------------------------------");System.out.println();System.out.println("Hello Service " + this.toString()+ "Is Saying Hello To Every One !");System.out.println("---------------------------------------------");System.out.println();}}測(cè)試類:
package final_activiti.progress;import java.util.HashMap; import java.util.Map;import org.activiti.engine.impl.test.PluggableActivitiTestCase; import org.activiti.engine.test.Deployment; import org.junit.Test; import org.mpc.final_activiti.Helloservice1;public class ServiceTask2Test extends PluggableActivitiTestCase {@Test@Deployment(resources = "final_activiti/progress/serviceTask_02.bpmn")public void test() {//定義delegate放入流程定義中啟動(dòng)流程Map<String, Object> map = new HashMap<String, Object>();map.put("delegate", new Helloservice1());runtimeService.startProcessInstanceByKey("service2", map);}}測(cè)試結(jié)果:
結(jié)果顯示兩個(gè)Service task 都執(zhí)行了,而且可以發(fā)現(xiàn),使用的是同一個(gè)服務(wù)對(duì)象。這樣更節(jié)省系統(tǒng)開銷吧。。。
方式三:使用expression
第二個(gè)Service task 的main config內(nèi)容:
myBean對(duì)應(yīng)的服務(wù)類:
package org.mpc.final_activiti;import java.io.Serializable;import org.activiti.engine.runtime.Execution;/*** * 不需要實(shí)現(xiàn)javadelegate,但是由于要放入到流程定義中,所以需要實(shí)現(xiàn)可序列話接口* * */ public class HelloService2 implements Serializable {private static final long serialVersionUID = 2356L;private String name = "test_mpc";/*** @Title: getName* @Description: 這里的get方法是必要的,因?yàn)樵诹鞒讨兄苯邮褂脤?duì)象.屬性的方式調(diào)用屬性的話,就相當(dāng)于調(diào)用這里的get方法* @param @return 設(shè)定文件* @return String 返回類型* @throws* @author mpc*/public String getName() {return name;}public void print(Execution ex) {System.out.println("This is HellService2 Called by process "+ ex.getId() + "who's saying hello ! ");} }測(cè)試類: package final_activiti.progress;import java.util.HashMap; import java.util.Map;import org.activiti.engine.impl.test.PluggableActivitiTestCase; import org.activiti.engine.runtime.ProcessInstance; import org.activiti.engine.test.Deployment; import org.junit.Test; import org.mpc.final_activiti.HelloService2;public class ServiceTask3Test extends PluggableActivitiTestCase {@Test@Deployment(resources = "final_activiti/progress/serviceTask_03.bpmn")public void test() {Map<String, Object> map = new HashMap<String, Object>();map.put("myBean", new HelloService2());ProcessInstance pi = runtimeService.startProcessInstanceByKey("service3", map);String s = (String) runtimeService.getVariable(pi.getId(), "myVar");assertTrue("test_mpc".equals(s));taskService.complete(taskService.createTaskQuery().singleResult().getId());assertProcessEnded(pi.getId());}}
測(cè)試結(jié)果:
總結(jié)
以上是生活随笔為你收集整理的activiti5第四弹----serviceTask中的java服务任务的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 软件设计师 --哈夫曼树的一个经典问题
- 下一篇: python3 中方法各种参数和返回值