物流ERP Activiti5.8 的应用一 -- 排他网关和流程的ReceiveTask的应用

先上流程实例: 物流ERP Activiti5.8 的应用一 -- 排他网关和流程的ReceiveTask的应用_第1张图片

 

BPMN 排他网关(那个X状符号)是指分支的执行路径不可以并行,只能走一条执行路径

 

ReceiveTask 是一个特殊的任务节点,他不能用taskService.complete驱动,流程执行至此就“中断”了,需外来的消息来驱动流程执行执行
所有有一个标识符。

测试代码如下:

 

package com.annto;

import static org.junit.Assert.*;

import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;

import org.activiti.engine.HistoryService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricActivityInstance;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.impl.RepositoryServiceImpl;
import org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.activiti.engine.impl.pvm.process.ActivityImpl;
import org.activiti.engine.impl.task.TaskDefinition;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:com/annto/SpringTransactionIntegrationTest-context.xml")
public class MyProcessTest {

	@Autowired
	private ProcessEngine processEngine;

	@Autowired
	private RuntimeService runtimeService;

	@Autowired
	private TaskService taskService;

	@Autowired
	RepositoryService repositoryService;

	@Test
	public void simpleProcessTest() throws Exception {
		Deployment deployment = repositoryService.createDeployment()
				.addClasspathResource("test/付款申请签字审批流程.bpmn20.xml").deploy();
		System.out.println("deploymentId:" + deployment.getId());

		// 获取流程定义
		ProcessDefinitionEntity processDefEn = (ProcessDefinitionEntity) ((RepositoryServiceImpl) repositoryService)
				.createProcessDefinitionQuery().processDefinitionName("付款申请签字审批流程").singleResult();
		System.out.println("definitionId : " + processDefEn.getId());

		processDefEn = (ProcessDefinitionEntity) ((RepositoryServiceImpl) repositoryService)
				.getDeployedProcessDefinition(processDefEn.getId());
		assertNotNull(processDefEn.getTaskDefinitions());
		assert (processDefEn.getTaskDefinitions().size() > 0);
		// 通过流程实例获取
		printProcessTasks(processDefEn);

		System.out.println("find process def " + processDefEn.getTaskDefinitions());

		HashMap map = new HashMap();
		map.put("price", 5000);
		map.put("this_出纳_财务经理", "herist");
		map.put("区域成本", "herist");
		map.put("this_出纳_财务经理", "herist");

		// 启动流程
		ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("付款申请签字审批流程", map);

		System.out.println("Started Process instance id " + processInstance.getProcessInstanceId());

		
		Task task = null;
		do {
			task = taskService.createTaskQuery().processInstanceId(processInstance.getProcessInstanceId())
					.singleResult();
			if (task != null) {
				System.out.print("\n--找到-- " + task.getName());
				taskService.complete(task.getId());
				System.out.print("  完成  \n" + processInstance.isEnded());
				renderStatusImg(processInstance);
				
			}

		} while (task != null);

		HistoryService historySvc = processEngine.getHistoryService();
		HistoricProcessInstance hisProcInst = historySvc.createHistoricProcessInstanceQuery().processInstanceId(processInstance.getProcessInstanceId())
		.singleResult();
		System.err.println("ended " + hisProcInst.getEndTime() + " . " + hisProcInst.getEndActivityId());
		
		HistoricActivityInstance hisActInst = historySvc.createHistoricActivityInstanceQuery()
			.processInstanceId(processInstance.getProcessInstanceId())
			.unfinished()
			.singleResult();
			
		System.err.println("alive act " + hisActInst.getActivityName());
		renderStatusImg(processInstance);
		
		//signal 驱动节点到完成状态
		this.runtimeService.signal(processInstance.getProcessInstanceId());
		
		hisProcInst = historySvc.createHistoricProcessInstanceQuery().processInstanceId(processInstance.getProcessInstanceId())
		.singleResult();
		System.err.println("ended " + hisProcInst.getEndTime() + " . " + hisProcInst.getEndActivityId());
					
	}

	/**
	 * 绘制状态图 和 审批活动的状态图
	 * 
	 * @param processInstance
	 * @throws IOException
	 */
	private void renderStatusImg(ProcessInstance processInstance) throws IOException {
		processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(processInstance.getId())
				.singleResult();

		if (processInstance == null || processInstance.isEnded()) {
			System.err.println("流程结束");
			return;
		}

		String definitionId = processInstance.getProcessDefinitionId();
		ProcessDefinitionEntity processDefEn = (ProcessDefinitionEntity) ((RepositoryServiceImpl) repositoryService)
				.getDeployedProcessDefinition(definitionId);

		// 获得当前活动,并绘制状态图
		List<String> activeIds = runtimeService.getActiveActivityIds(processInstance.getId());
		String actName = processEngine.getHistoryService().createHistoricActivityInstanceQuery()
				.activityId(activeIds.get(0)).singleResult().getActivityName();
		InputStream inputStream = ProcessDiagramGenerator.generateDiagram(processDefEn, "png", activeIds);

		saveToFile(actName + ".png", inputStream);
	}

	/**
	 * 获取流程定义的所有任务,打印之
	 */
	public void printProcessTasks(ProcessDefinitionEntity processDefEn) throws Exception {
		
		for (String taskKey : processDefEn.getTaskDefinitions().keySet()) {
			TaskDefinition taskDef = processDefEn.getTaskDefinitions().get(taskKey);
			System.out.println(taskKey + ": " + taskDef.getNameExpression());
		}
		
		for (ActivityImpl actvity : processDefEn.getActivities()) {		
			if(processDefEn.getTaskDefinitions().containsKey(actvity.getId()))
				System.out.println( "actvity: " + processDefEn.getTaskDefinitions().get(actvity.getId()).getAssigneeExpression());
			else
				System.out.println( "actvity: " + actvity.getId());
		}
		
	}

	public void saveToFile(String fileName, InputStream in) throws IOException {
		FileOutputStream fos = null;
		BufferedInputStream bis = null;
		// URL url = null;
		int BUFFER_SIZE = 1024;
		byte[] buf = new byte[BUFFER_SIZE];
		int size = 0;
		
		bis = new BufferedInputStream(in);
		// //建立文件
		fos = new FileOutputStream(fileName);
		//
		// //保存文件
		while ((size = bis.read(buf)) != -1)
			fos.write(buf, 0, size);
		//
		fos.close();
		bis.close();
	}
}

 

执行过程的图片:

 
物流ERP Activiti5.8 的应用一 -- 排他网关和流程的ReceiveTask的应用_第2张图片

 



物流ERP Activiti5.8 的应用一 -- 排他网关和流程的ReceiveTask的应用_第3张图片
 

 


 流程执行结束:
ended Mon Jun 18 21:12:48 CST 2012 . endevent1
 

 

应用场景:

 

在物流ERP中做业务流程整合,有部分流程是历史模块的应用,一个业务执行逻辑可能包括n个人工维护系统数据 + 系统后台的Job处理过程,如:报销流程的最后一个环节为系统的付款操作(需要人工执行批量支付,然后Job负责定时往Oracle ERP中记账,再通过Job把付款状态回写到本系统中来,再继续执行网银的付款,所以短时间内,未能把每个业务操作都整合到流程节点来,所以可以用一个Receive Task节点来表示一个这个付款业务,在这个节点结束之后,流程才真正完成!

你可能感兴趣的:(activiti5)