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

Activiti的简单应用,应用监听来实现简单的业务颗粒与工作流程结合,让流程带动业务颗粒执行的过程,此次的流程图应用多分支结构,并将流程中的判断值设为参数,由前台传递过去。

新建普通java类:

package com.tgb.itoo.activiti.controller;

public class xs {

	public static void SayHello(){
		System.out.println("88xvshu");
	}

}

新建activiti监听类:

package com.tgb.itoo.activiti.controller;

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

public class xvshuDelegate implements TaskListener{
	/**
	 *
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public void notify(DelegateTask delegateTask) {
		xs.SayHello();
	}

}

类似的Java类和监听类需要再创建3个

业务流程图 如图:

流程变量的属性设置(jingyuan-${result==flowjingyuan},milaoshi-${result==flowmilaoshi}) 如图:

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

<?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="xvshu" name="xvshu">
      <extensionElements>
        <activiti:taskListener event="create" class="com.tgb.itoo.activiti.controller.xvshuDelegate"></activiti:taskListener>
      </extensionElements>
    </userTask>
    <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="xvshu"></sequenceFlow>
    <userTask id="jingyuan" name="jingyuan">
      <extensionElements>
        <activiti:taskListener event="create" class="com.tgb.itoo.activiti.controller.jingyuanDelegate"></activiti:taskListener>
      </extensionElements>
    </userTask>
    <userTask id="usertask1" name="guhaiyan">
      <extensionElements>
        <activiti:taskListener event="create" class="com.tgb.itoo.activiti.controller.guhaiyanDelegate"></activiti:taskListener>
      </extensionElements>
    </userTask>
    <endEvent id="endevent1" name="End"></endEvent>
    <sequenceFlow id="flow4" sourceRef="usertask1" targetRef="endevent1"></sequenceFlow>
    <exclusiveGateway id="exclusivegateway1" name="Exclusive Gateway"></exclusiveGateway>
    <sequenceFlow id="flow5" sourceRef="xvshu" targetRef="exclusivegateway1"></sequenceFlow>
    <sequenceFlow id="flow6" name="jingyuan" sourceRef="exclusivegateway1" targetRef="jingyuan">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${result==flowjingyuan}]]></conditionExpression>
    </sequenceFlow>
    <userTask id="usertask2" name="guhaiyan">
      <extensionElements>
        <activiti:taskListener event="create" class="com.tgb.itoo.activiti.controller.guhaiyanDelegate"></activiti:taskListener>
      </extensionElements>
    </userTask>
    <sequenceFlow id="flow13" name="guhaiyan" sourceRef="exclusivegateway1" targetRef="usertask2">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${result==flowhaiyan}]]></conditionExpression>
    </sequenceFlow>
    <userTask id="milaoshi" name="milaoshi" activiti:async="true">
      <extensionElements>
        <activiti:taskListener event="create" class="com.tgb.itoo.activiti.controller.milaoshiDelegate"></activiti:taskListener>
      </extensionElements>
    </userTask>
    <sequenceFlow id="flow14" name="milaoshi" sourceRef="exclusivegateway1" targetRef="milaoshi">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${result==flowmilaoshi}]]></conditionExpression>
    </sequenceFlow>
    <sequenceFlow id="flow15" sourceRef="milaoshi" targetRef="endevent1"></sequenceFlow>
    <sequenceFlow id="flow16" sourceRef="usertask2" targetRef="endevent1"></sequenceFlow>
    <sequenceFlow id="flow17" name="guhaiyan" sourceRef="exclusivegateway2" targetRef="usertask1">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${result==flowhaiyan}]]></conditionExpression>
    </sequenceFlow>
    <sequenceFlow id="flow18" sourceRef="exclusivegateway2" targetRef="endevent1"></sequenceFlow>
    <exclusiveGateway id="exclusivegateway2" name="Exclusive Gateway"></exclusiveGateway>
    <sequenceFlow id="flow19" sourceRef="jingyuan" targetRef="exclusivegateway2"></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="60.0" y="150.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="xvshu" id="BPMNShape_xvshu">
        <omgdc:Bounds height="55.0" width="105.0" x="140.0" y="140.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="jingyuan" id="BPMNShape_jingyuan">
        <omgdc:Bounds height="55.0" width="105.0" x="400.0" y="140.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1">
        <omgdc:Bounds height="55.0" width="105.0" x="630.0" y="140.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
        <omgdc:Bounds height="35.0" width="35.0" x="745.0" y="270.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="exclusivegateway1" id="BPMNShape_exclusivegateway1">
        <omgdc:Bounds height="40.0" width="40.0" x="290.0" y="147.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="usertask2" id="BPMNShape_usertask2">
        <omgdc:Bounds height="55.0" width="105.0" x="350.0" y="14.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="milaoshi" id="BPMNShape_milaoshi">
        <omgdc:Bounds height="55.0" width="105.0" x="258.0" y="260.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="exclusivegateway2" id="BPMNShape_exclusivegateway2">
        <omgdc:Bounds height="40.0" width="40.0" x="539.0" y="147.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
        <omgdi:waypoint x="95.0" y="167.0"></omgdi:waypoint>
        <omgdi:waypoint x="140.0" y="167.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow4" id="BPMNEdge_flow4">
        <omgdi:waypoint x="735.0" y="167.0"></omgdi:waypoint>
        <omgdi:waypoint x="762.0" y="173.0"></omgdi:waypoint>
        <omgdi:waypoint x="762.0" y="270.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow5" id="BPMNEdge_flow5">
        <omgdi:waypoint x="245.0" y="167.0"></omgdi:waypoint>
        <omgdi:waypoint x="290.0" y="167.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow6" id="BPMNEdge_flow6">
        <omgdi:waypoint x="330.0" y="167.0"></omgdi:waypoint>
        <omgdi:waypoint x="400.0" y="167.0"></omgdi:waypoint>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="14.0" width="40.0" x="339.0" y="179.0"></omgdc:Bounds>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow13" id="BPMNEdge_flow13">
        <omgdi:waypoint x="310.0" y="147.0"></omgdi:waypoint>
        <omgdi:waypoint x="310.0" y="41.0"></omgdi:waypoint>
        <omgdi:waypoint x="350.0" y="41.0"></omgdi:waypoint>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="14.0" width="44.0" x="310.0" y="91.0"></omgdc:Bounds>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow14" id="BPMNEdge_flow14">
        <omgdi:waypoint x="310.0" y="187.0"></omgdi:waypoint>
        <omgdi:waypoint x="310.0" y="260.0"></omgdi:waypoint>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="14.0" width="38.0" x="310.0" y="219.0"></omgdc:Bounds>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow15" id="BPMNEdge_flow15">
        <omgdi:waypoint x="363.0" y="287.0"></omgdi:waypoint>
        <omgdi:waypoint x="745.0" y="287.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow16" id="BPMNEdge_flow16">
        <omgdi:waypoint x="455.0" y="41.0"></omgdi:waypoint>
        <omgdi:waypoint x="762.0" y="41.0"></omgdi:waypoint>
        <omgdi:waypoint x="762.0" y="270.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow17" id="BPMNEdge_flow17">
        <omgdi:waypoint x="579.0" y="167.0"></omgdi:waypoint>
        <omgdi:waypoint x="630.0" y="167.0"></omgdi:waypoint>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="14.0" width="100.0" x="578.0" y="150.0"></omgdc:Bounds>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow18" id="BPMNEdge_flow18">
        <omgdi:waypoint x="559.0" y="187.0"></omgdi:waypoint>
        <omgdi:waypoint x="559.0" y="287.0"></omgdi:waypoint>
        <omgdi:waypoint x="745.0" y="287.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow19" id="BPMNEdge_flow19">
        <omgdi:waypoint x="505.0" y="167.0"></omgdi:waypoint>
        <omgdi:waypoint x="539.0" y="167.0"></omgdi:waypoint>
      </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;

public class NameDemo {

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

		//使用Spring来管理工作流引擎的创建
		/* ProcessEngine processEngine = ProcessEngineConfiguration
	                .createProcessEngineConfigurationFromResource("applicationContext-common.xml")
	                .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();

		// 2、启动流程--根据key部署的话是默认找的最新版本
		ProcessInstance processInstance = runtimeService
				.startProcessInstanceByKey("myProcess");

		String end="0";//processInstance.getId() != null

		while (end.equalsIgnoreCase("0")) {
			//循环查询任务

			Task task = taskService.createTaskQuery()
					.processInstanceId(processInstance.getId()).singleResult();
			Map<String, Object> variables = new HashMap<String, Object>();
			if(task==null){
				end="1";
				System.out.println("任务已经结束");
				break;
			}else{
				str = readDataFromConsole("Please input result:");
				String flowjingyuan=readDataFromConsole("Please input flowjingyuan:");
				String flowhaiyan=readDataFromConsole("Please input flowhaiyan:");
				String flowmilaoshi=readDataFromConsole("Please input flowmilaoshi:");

				variables.put("result", str);
				variables.put("flowjingyuan", flowjingyuan);
				variables.put("flowhaiyan", flowhaiyan);
				variables.put("flowmilaoshi", flowmilaoshi);

				taskService.complete(task.getId(),variables);	//完成任务

			}				

		}

	}

}

执行结果:

总结:

首先解释一下,现在在客户端存在的所有暂停其实在真实的开发中均是不存在的,此处只是为了传递变量,告诉工作流如何继续向下一个结点执行,在真实的开发业务中是不同的人处理不同的任务,每次的查询只查询自己的任务,只有本身自己的任务完成后工作流才会自动向下执行。

解释一下执行结果:

xvshu结点是在工作流启动后立即执行,但是在办理xvshu结点时需要输入下一个结点的参数,当输入的result==flowjingyuan时,则立即执行jingyuan结点,输出jingyuan88,办理jingyuan结点时也需要输入下一个结点的参数,当输入的内容result==flowhaiyan即立即执行haiyan结点,输出88guhaiyan,办理guhaiyan结点时,需要输入下一个结点的参数,因为haiyan结点的下一个结点为结束,而且没有条件限制,故输入任何信息都会结束。

时间: 2024-10-26 03:15:27

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

【应用篇】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

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

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

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

Activiti的简单应用,应用监听来实现简单的业务颗粒与工作流程结合,让流程带动业务颗粒执行的过程,此次的监听我们应用抽象的监听来实现,也就是说所有的普通业务类均应用此抽象监听,而不需要每一个类一个监听的来操作. 新建两个普通类: package com.tgb.itoo.activiti.controller; public class milaoshi { public static void SayHello(){ System.out.println("milaoshi--sayHel

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

QT 子窗口监听主窗口信号(超级简单,但是好用,比如主窗口移动的时候,子窗口不要再继续处理任务)

MainWindow *ptr = NULL; ptr = (MainWindow*)parentWidget(); connect(ptr, SIGNAL(param_result(bool)), this, SLOT(param_add(bool))); http://blog.csdn.net/emdfans/article/details/42667403

Zookeeper 事件监听 - 史上最详解读

目录 写在前面 1.1. Curator 事件监听 1.1.1. Watcher 标准的事件处理器 1.1.2. NodeCache 节点缓存的监听 1.1.3. PathChildrenCache 子节点监听 1.1.4. Tree Cache 节点树缓存 写在最后 疯狂创客圈 亿级流量 高并发IM 实战 系列 疯狂创客圈 Java 分布式聊天室[ 亿级流量]实战系列之 -25[ 博客园 总入口 ] 写在前面 ? 大家好,我是作者尼恩.目前和几个小伙伴一起,组织了一个高并发的实战社群[疯狂创客

Android中Dialog对话框的调用及监听

Android中经常会需要在Android界面上弹出一些对话框提示用户,比如App的退出的时候都会有各种框来挽留你的心,支付宝的时候输入密码的密码框,非常常见及其实用的功能,类似于JS中的alter,C#中C/S中常用MessgeBox,总而言之就是一个功能弹,弹,弹,本文就简单的叙述一下Dialog的各种弹框使用及监听,顺便写了一个简单的自定义弹框,接下来请看正文: 一般对话框 先看下整个App页面: 看下一般对话框的结果: 具体代码的实现,其中定义了两个按钮,一个是确定(PositiveBu

[基础控件]---状态切换控件CompoundButton及其子类CheckBox、RadioButton、ToggleButton、switch事件监听与场景使用

一.事件监听 对于普通的Button,对其进行事件监听Google官方给出了常见的三种监听方式:1.对每一个button设置事件监听器button.setOnClickListener(View.OnclickListener  listener);此种方法当button按钮较多时代码显得多.乱.不够简洁明了. 2.在Activity中实现接口View.OnclickListener,然后重写void onClick(View v)方法,在方法中通过switch(v.getId())予以区分不同

监听tomcat服务器启动/关闭并从配置文件中读取参数进行初始化

监听tomcat服务器启动/关闭很简单(2步): 1. 建立一个类实现ServletContextListener接口,重写其中的方法(contextDestroyed和contextInitialized). package action; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; public class SystemListener implements Se