activiti5第四弹----serviceTask中的java服务任务

activiti.cfg.xml内容:

<?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">
	<bean
		class="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来实现java服务任务

activiti5第四弹----serviceTask中的java服务任务_第1张图片


HelloService内容:

package org.mpc.final_activiti;

import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.JavaDelegate;

/**
 * 
 * 作为activiti的ServiceTask的实现,必须实现JavaDelegate接口,但是不需要实现序列化接口
 * 
 * activiti会使用反射机制将类初始化,因此在实现JavaDelegate接口的时候需要提供一个
 * 无参数的构造器,否则会抛出异常。
 * 
 * @author mpc
 *
 */
public class HelloService implements JavaDelegate {

	@Override
	public 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();
	}

}

测试方法:

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());
	}

}

测试结果:

activiti5第四弹----serviceTask中的java服务任务_第2张图片

从测试结果可以看到,我们在流程中定义的连个Service task都执行了,而且可以从红线标注处看到这两个Service是不一样的,也就是说activiti在每次需要执行一个Service task 的时候都重新创建了服务对象。


方式二:使用 Delegate expression来实现java服务任务

activiti5第四弹----serviceTask中的java服务任务_第3张图片

delegate对应的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 {

	/**
	 * 实现JavaDelegate接口,使用其中的execute方法 由于要放入流程定义中,所以要实现可序列话接口
	 * 
	 */
	private static final long serialVersionUID = 5593437463482732772L;

	@Override
	public 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();

	}

}

测试类:

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放入流程定义中启动流程
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("delegate", new Helloservice1());
		runtimeService.startProcessInstanceByKey("service2", map);
	}

}

测试结果:

activiti5第四弹----serviceTask中的java服务任务_第4张图片

结果显示两个Service task 都执行了,而且可以发现,使用的是同一个服务对象。这样更节省系统开销吧。。。


方式三:使用expression

activiti5第四弹----serviceTask中的java服务任务_第5张图片

第二个Service task 的main config内容:

activiti5第四弹----serviceTask中的java服务任务_第6张图片

myBean对应的服务类:

package org.mpc.final_activiti;

import java.io.Serializable;

import org.activiti.engine.runtime.Execution;

/**
 * 
 * 不需要实现javadelegate,但是由于要放入到流程定义中,所以需要实现可序列话接口
 * 
 * 
 */
public class HelloService2 implements Serializable {

	private static final long serialVersionUID = 2356L;
	private String name = "test_mpc";

	/**
	 * @Title: getName
	 * @Description: 这里的get方法是必要的,因为在流程中直接使用对象.属性的方式调用属性的话,就相当于调用这里的get方法
	 * @param @return 设定文件
	 * @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 ! ");
	}
}

测试类:

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());
	}

}

测试结果:

activiti5第四弹----serviceTask中的java服务任务_第7张图片


你可能感兴趣的:(java,exception,bean,delegate,activiti5)