【应用篇】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 org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;

public class jingyuanDelegate implements TaskListener{

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

}

类似的再建两个类(ghy,xs)与两个监听(guhaiyanDelegate,xvshuDelegate)

画如下流程图:

配置相应的属性

每个类上添加相应的监听 如图:

给每条流向添加变量 如图:

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

<?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="同意" sourceRef="exclusivegateway1" targetRef="jingyuan">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${result}]]></conditionExpression>
    </sequenceFlow>
    <sequenceFlow id="flow7" name="不同意" sourceRef="exclusivegateway1" targetRef="endevent1">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${!result}]]></conditionExpression>
    </sequenceFlow>
    <exclusiveGateway id="exclusivegateway2" name="Exclusive Gateway"></exclusiveGateway>
    <sequenceFlow id="flow8" sourceRef="jingyuan" targetRef="exclusivegateway2"></sequenceFlow>
    <sequenceFlow id="flow9" name="同意" sourceRef="exclusivegateway2" targetRef="usertask1">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${result}]]></conditionExpression>
    </sequenceFlow>
    <sequenceFlow id="flow10" name="不同意" sourceRef="exclusivegateway2" 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="70.0" y="150.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="xvshu" id="BPMNShape_xvshu">
        <omgdc:Bounds height="55.0" width="105.0" x="150.0" y="140.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="jingyuan" id="BPMNShape_jingyuan">
        <omgdc:Bounds height="55.0" width="105.0" x="410.0" y="140.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1">
        <omgdc:Bounds height="55.0" width="105.0" x="710.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="300.0" y="147.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="exclusivegateway2" id="BPMNShape_exclusivegateway2">
        <omgdc:Bounds height="40.0" width="40.0" x="560.0" y="148.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
        <omgdi:waypoint x="105.0" y="167.0"></omgdi:waypoint>
        <omgdi:waypoint x="150.0" y="167.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow4" id="BPMNEdge_flow4">
        <omgdi:waypoint x="762.0" y="195.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="255.0" y="167.0"></omgdi:waypoint>
        <omgdi:waypoint x="300.0" y="167.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow6" id="BPMNEdge_flow6">
        <omgdi:waypoint x="340.0" y="167.0"></omgdi:waypoint>
        <omgdi:waypoint x="410.0" y="167.0"></omgdi:waypoint>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="14.0" width="24.0" x="350.0" y="167.0"></omgdc:Bounds>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow7" id="BPMNEdge_flow7">
        <omgdi:waypoint x="320.0" y="187.0"></omgdi:waypoint>
        <omgdi:waypoint x="320.0" y="287.0"></omgdi:waypoint>
        <omgdi:waypoint x="745.0" y="287.0"></omgdi:waypoint>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="14.0" width="36.0" x="330.0" y="243.0"></omgdc:Bounds>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow8" id="BPMNEdge_flow8">
        <omgdi:waypoint x="515.0" y="167.0"></omgdi:waypoint>
        <omgdi:waypoint x="560.0" y="168.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow9" id="BPMNEdge_flow9">
        <omgdi:waypoint x="600.0" y="168.0"></omgdi:waypoint>
        <omgdi:waypoint x="710.0" y="167.0"></omgdi:waypoint>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="14.0" width="24.0" x="610.0" y="168.0"></omgdc:Bounds>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow10" id="BPMNEdge_flow10">
        <omgdi:waypoint x="580.0" y="188.0"></omgdi:waypoint>
        <omgdi:waypoint x="580.0" y="287.0"></omgdi:waypoint>
        <omgdi:waypoint x="745.0" y="287.0"></omgdi:waypoint>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="14.0" width="36.0" x="590.0" y="243.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;

public class testDemo {

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

		// 2、启动流程
		ProcessInstance processInstance = runtimeService
				.startProcessInstanceByKey("myProcess");
		String end="1";//processInstance.getId() != null
		while (end.equalsIgnoreCase("1")) {
			// 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-10-13 14:46:49

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

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

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

【Darwin学习笔记】之RTSP连接监听初始化

网上有很多大侠已经对Darwin做了详细的剖析,在下仅本着积累经验的目的,将Darwin的学习过程记录下来,同时与网友们交流学习,今天先来分析下Darwin如果根据配置文件监听RTSP连接的流程. 1. Darwin系统初始化,Bool16 QTSServer::Initialize(.....) 2. QTSServer::CreateListeners(...)根据本地xml配置文件中的地址和端口进行监听的建立,主要有两个配置项: <PREF NAME="bind_ip_addr&qu