【应用篇】Activiti监听(抽象)与业务颗粒结合的简单应用(二)

Activiti的简单应用,应用监听来实现简单的业务颗粒与工作流程结合,让流程带动业务颗粒执行的过程,此次的监听我们应用抽象的监听来实现,也就是说所有的普通业务类均应用此抽象监听,而不需要每一个类一个监听的来操作。

新建两个普通类:

package com.tgb.itoo.activiti.controller;

public class milaoshi {
	public static void SayHello(){
		System.out.println("milaoshi--sayHello--");
	}
/*	public static void SayHi(){
		System.out.println("milaoshi--sayHi--");
	}*/
}

package com.tgb.itoo.activiti.controller;

public class tanghuan {
	public static void SayHello(){
		System.out.println("tanghuan--sayHello--");
	}
	public static void SayHi(){
		System.out.println("tanghuan--sayHi--");
	}
}

抽象监听类:

package com.tgb.itoo.activiti.controller;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;

public class CommonDelegate implements TaskListener{

	static Map<Object,Object> map;

	public CommonDelegate(){}
	public CommonDelegate(Map<Object,Object> map){
		this.map=map;
	}

	@Override
	public void notify(DelegateTask delegateTask) {

		Object obj=map.get("object");
		Class clazz=obj.getClass();
		Method[] methods=clazz.getDeclaredMethods();
		for(int i=0;i<methods.length;i++ ){
			try {				

				Method beforMehod = clazz.getMethod(methods[i].getName());
		        beforMehod.invoke(obj);

			} catch (NoSuchMethodException | SecurityException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
	}

}

业务流程图 如图(tanghuan类和milaoshi类均绑定同一个监听):

流程变量 如图(不同意如图,同意为${result}):

最终生成的配置文件 如下:

<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.activiti.org/test">
  <process id="myProcess" name="My process" isExecutable="true">
    <startEvent id="startevent1" name="Start"></startEvent>
    <userTask id="usertask1" name="tanghuan">
      <extensionElements>
        <activiti:taskListener event="create" class="com.tgb.itoo.activiti.controller.CommonDelegate"></activiti:taskListener>
      </extensionElements>
    </userTask>
    <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow>
    <exclusiveGateway id="exclusivegateway1" name="Exclusive Gateway"></exclusiveGateway>
    <sequenceFlow id="flow2" sourceRef="usertask1" targetRef="exclusivegateway1"></sequenceFlow>
    <userTask id="usertask2" name="milaoshi">
      <extensionElements>
        <activiti:taskListener event="create" class="com.tgb.itoo.activiti.controller.CommonDelegate"></activiti:taskListener>
      </extensionElements>
    </userTask>
    <sequenceFlow id="flow3" name="同意" sourceRef="exclusivegateway1" targetRef="usertask2">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${result}]]></conditionExpression>
    </sequenceFlow>
    <endEvent id="endevent1" name="End"></endEvent>
    <sequenceFlow id="flow4" sourceRef="usertask2" targetRef="endevent1"></sequenceFlow>
    <sequenceFlow id="flow5" name="不同意" sourceRef="exclusivegateway1" targetRef="endevent1">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${!result}]]></conditionExpression>
    </sequenceFlow>
  </process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_myProcess">
    <bpmndi:BPMNPlane bpmnElement="myProcess" id="BPMNPlane_myProcess">
      <bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
        <omgdc:Bounds height="35.0" width="35.0" x="139.0" y="102.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1">
        <omgdc:Bounds height="55.0" width="105.0" x="219.0" y="92.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="exclusivegateway1" id="BPMNShape_exclusivegateway1">
        <omgdc:Bounds height="40.0" width="40.0" x="369.0" y="100.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="usertask2" id="BPMNShape_usertask2">
        <omgdc:Bounds height="55.0" width="105.0" x="454.0" y="93.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
        <omgdc:Bounds height="35.0" width="35.0" x="489.0" y="222.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
        <omgdi:waypoint x="174.0" y="119.0"></omgdi:waypoint>
        <omgdi:waypoint x="219.0" y="119.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
        <omgdi:waypoint x="324.0" y="119.0"></omgdi:waypoint>
        <omgdi:waypoint x="369.0" y="120.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3">
        <omgdi:waypoint x="409.0" y="120.0"></omgdi:waypoint>
        <omgdi:waypoint x="454.0" y="120.0"></omgdi:waypoint>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="14.0" width="100.0" x="419.0" y="120.0"></omgdc:Bounds>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow4" id="BPMNEdge_flow4">
        <omgdi:waypoint x="506.0" y="148.0"></omgdi:waypoint>
        <omgdi:waypoint x="506.0" y="222.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow5" id="BPMNEdge_flow5">
        <omgdi:waypoint x="389.0" y="140.0"></omgdi:waypoint>
        <omgdi:waypoint x="389.0" y="239.0"></omgdi:waypoint>
        <omgdi:waypoint x="489.0" y="239.0"></omgdi:waypoint>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="14.0" width="100.0" x="399.0" y="201.0"></omgdc:Bounds>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</definitions>

启动流程对应的代码:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;

import com.tgb.itoo.activiti.controller.CommonDelegate;
import com.tgb.itoo.activiti.controller.milaoshi;
import com.tgb.itoo.activiti.controller.tanghuan;

public class MapDemo {

	private static String readDataFromConsole(String prompt) {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String str = null;
		try {
			System.out.print(prompt);
			str = br.readLine();

		} catch (IOException e) {
			e.printStackTrace();
		}
		return str;
	}

	public static void main(String[] args) {

		String str = readDataFromConsole("Please input string:");
		System.out.println("The information from console: " + str);
		// 创建流程引擎
		ProcessEngineConfiguration processEngineConfiguration = ProcessEngineConfiguration
				.createStandaloneProcessEngineConfiguration();
		// 连接数据库的配置
		processEngineConfiguration.setJdbcDriver("com.mysql.jdbc.Driver");
		processEngineConfiguration
				.setJdbcUrl("jdbc:mysql://localhost:3306/activitiexam?useUnicode=true&characterEncoding=utf8");
		processEngineConfiguration.setJdbcUsername("root");
		processEngineConfiguration.setJdbcPassword("root");
		ProcessEngine processEngine = processEngineConfiguration
				.buildProcessEngine();

		System.out.println(processEngine);
		// 获取流程存储服务组件
		RepositoryService repositoryService = processEngine
				.getRepositoryService();

		// 获取运行时服务组件
		RuntimeService runtimeService = processEngine.getRuntimeService();

		// 获取流程任务组件
		TaskService taskService = processEngine.getTaskService();

		// 1、部署流程文件
		repositoryService.createDeployment().name("MyProcess")
				.addClasspathResource("diagrams/" + str + ".bpmn").deploy();

		Map<Object,Object> map=new HashMap<Object,Object>();
		tanghuan th=new tanghuan();
		milaoshi mxj=new milaoshi();
		//向map里面扔对象
		map.put("object", th);
		/*map.put("milaoshi", mxj);*/
		CommonDelegate common=new CommonDelegate(map);

		// 2、启动流程
		ProcessInstance processInstance = runtimeService
				.startProcessInstanceByKey("myProcess");
		String end="1";//processInstance.getId() != null
		while (end.equalsIgnoreCase("1")) {
			map.remove("object");
			map.put("object", mxj);
			// 3、查询第一个任务
			Task task = taskService.createTaskQuery()
					.processInstanceId(processInstance.getId()).singleResult();
			;			

			if (task != null) {
				System.out.println("============" + task.getId()
						+ "============" + task.getName() + "============");
				str = readDataFromConsole("Please input result:");
				Map<String, Object> variables = new HashMap<String, Object>();
				Boolean result;
				if (str.equalsIgnoreCase("true")) {
					result = true;
				} else {
					result = false;

				}
				variables.put("result", result);
				taskService.complete(task.getId(), variables); // 完成任务

			}else {
				end="0";
				System.out.println("任务已经完成");
			} 

		}

	}

}

执行结果:

总结:

以上这种方式主要是应用了反射技术实现,我们在客户端动态的向监听内扔对象,根据扔的对象来去执行对应的方法,减少了我们编写监听的个数。

时间: 2024-11-02 19:54:05

【应用篇】Activiti监听(抽象)与业务颗粒结合的简单应用(二)的相关文章

【应用篇】Activiti监听与业务颗粒结合的简单应用(三)

Activiti的简单应用,应用监听来实现简单的业务颗粒与工作流程结合,让流程带动业务颗粒执行的过程,此次的流程图应用多分支结构,并将流程中的判断值设为参数,由前台传递过去. 新建普通java类: package com.tgb.itoo.activiti.controller; public class xs { public static void SayHello(){ System.out.println("88xvshu"); } } 新建activiti监听类: packa

【COCOS CREATOR 系列教程之二】脚本开发篇&事件监听、常用函数等示例整合

[Cocos Creator ](千人群):  432818031 上一篇,介绍了Himi在使用过cc所有组件后的一篇总结,没有具体介绍每个组件的原因在于官方文档很齐全,而且也有视频的介绍. 所以希望童鞋们可以把我这两篇博文当成对组件.脚本两部分开发的整理与总结. 后续的文章,Himi应该主要更新一些官方还未补充或者还没有的教程.避免无用功. 下面直接放出代码,因为不是很难理解.所以不再一一赘述,都是常用的函数.事件监听.动作回调.定时器等开发过程中必接触的. 大致内容如下: cc 属性介绍 获

【应用篇】Activiti监听与业务颗粒结合的简单应用

Activiti的简单应用,应用监听来实现简单的业务颗粒与工作流程结合,让流程带动业务颗粒执行的过程. 新建一个普通类 package com.tgb.itoo.activiti.controller; public class jy { public static void SayHello(){ System.out.println("jingyuan88"); } } 新建一个监听类 package com.tgb.itoo.activiti.controller; import

vue基础篇---watch监听

watch可以让我们监控一个值的变化.从而做出相应的反应. 示例: <div id="app"> <input type="text" v-model="message"> </div> <script src="./node_modules/vue/dist/vue.js"></script> <script type="text/javascrip

js事件监听

通过添加事件监听进一步实现业务处理.可以对同一个元素添加相同的事件监听,执行会从上到下的原则,也可以添加多个事件监听针对同一元素. 针对元素监听: document.getElementById("idName").addEventListener("eventType",functionName);//注意:前面不加on 如click事件 针对window对象监听: window.addEventListener(“resize”,function(){ aler

Android 监听短信(同时监听广播和数据库)

暗扣,强烈谴责这种侵害用户利益的行为... 下面给大家介绍Android暗扣原理.......  Android4.4以下的系统玩游戏就要小心了哈 暗扣方式之一:短信订购,即监听--------拦截------------处理短信. 暗扣方式之二:模拟人为操作(又叫模拟流量),通过后台程序代码模拟人的点击行为,暗自给用户订购业务,由运营商收取你的费用,当然这其中也需要涉及监听/拦截/处理短信.使用这种方式的原理无非是Http处理网页,还涉及接入点切换问题,这里就不详细讲解. 回归正题:有的时候,

MutationObserver 监听DOM树变化

1 概述 Mutation observer 是用于代替 Mutation events 作为观察DOM树结构发生变化时,做出相应处理的API.为什么要使用mutation observer 去代替 mutation events 呢,我们先了解一下mutation events Mutation Events Mutation events 是在 DOM3中定义,用于监听DOM树结构变化的事件 它简单的用法如下: document.getElementById('list').addEvent

Eclipse插件开发中的选择监听机制(Selection Provider-Listener)

Eclipse插件开发中的选择监听机制(Selection Provider-Listener) 监听机制是eclipse插件开发或rcp应用开发中常用的技术,比如点击TableViewer或TreeViewer中的某个元素,需要针对当前selection做出某些处理. 实现方式一般有两种情况,即selection provider 和listener在一个视图中,或在不同的视图中: 1.selection provider自己增加监听进行处理 前者直接让selection provider实现

android自定义组件监听,监听网络,防止组件点击过快重复点击

1 组件监听事件,业务逻辑之前判断网络情况 public abstract class OnClickNetworkListener implements View.OnClickListener { @Override public void onClick(View v) { boolean isNetworkOk = Config.isNetworkConnected(Config.currentContext); if (isNetworkOk) { onNetworkClick(v);