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

activiti.cfg.xml内容:



	
		
		
		
		
		
		
		
		
		
	


方式一:使用java class来实现java服务任务



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

}

测试结果:


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


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


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 map = new HashMap();
		map.put("delegate", new Helloservice1());
		runtimeService.startProcessInstanceByKey("service2", map);
	}

}

测试结果:


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


方式三:使用expression


第二个Service task 的main config内容:


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 map = new HashMap();
		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());
	}

}

测试结果:



转载于:https://www.cnblogs.com/qzmpc/p/6339579.html

你可能感兴趣的:(activiti5第四弹----serviceTask中的java服务任务)