【应用篇】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("任务已经完毕"); } } } }


运行结果:




总结:

    以上这样的方式主要是应用了反射技术实现。我们在client动态的向监听内扔对象,依据扔的对象来去运行相应的方法,降低了我们编写监听的个数。

 


版权声明:本文博客原创文章,博客,未经同意,不得转载。









本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/4741991.html,如需转载请自行联系原作者


相关文章
|
4天前
|
弹性计算 关系型数据库 微服务
基于 Docker 与 Kubernetes(K3s)的微服务:阿里云生产环境扩容实践
在微服务架构中,如何实现“稳定扩容”与“成本可控”是企业面临的核心挑战。本文结合 Python FastAPI 微服务实战,详解如何基于阿里云基础设施,利用 Docker 封装服务、K3s 实现容器编排,构建生产级微服务架构。内容涵盖容器构建、集群部署、自动扩缩容、可观测性等关键环节,适配阿里云资源特性与服务生态,助力企业打造低成本、高可靠、易扩展的微服务解决方案。
1106 0
|
3天前
|
机器学习/深度学习 人工智能 前端开发
通义DeepResearch全面开源!同步分享可落地的高阶Agent构建方法论
通义研究团队开源发布通义 DeepResearch —— 首个在性能上可与 OpenAI DeepResearch 相媲美、并在多项权威基准测试中取得领先表现的全开源 Web Agent。
532 10
|
13天前
|
人工智能 运维 安全
|
12天前
|
人工智能 测试技术 API
智能体(AI Agent)搭建全攻略:从概念到实践的终极指南
在人工智能浪潮中,智能体(AI Agent)正成为变革性技术。它们具备自主决策、环境感知、任务执行等能力,广泛应用于日常任务与商业流程。本文详解智能体概念、架构及七步搭建指南,助你打造专属智能体,迎接智能自动化新时代。
|
4天前
|
弹性计算 Kubernetes jenkins
如何在 ECS/EKS 集群中有效使用 Jenkins
本文探讨了如何将 Jenkins 与 AWS ECS 和 EKS 集群集成,以构建高效、灵活且具备自动扩缩容能力的 CI/CD 流水线,提升软件交付效率并优化资源成本。
301 0
|
11天前
|
人工智能 异构计算
敬请锁定《C位面对面》,洞察通用计算如何在AI时代持续赋能企业创新,助力业务发展!
敬请锁定《C位面对面》,洞察通用计算如何在AI时代持续赋能企业创新,助力业务发展!
|
12天前
|
机器学习/深度学习 人工智能 自然语言处理
B站开源IndexTTS2,用极致表现力颠覆听觉体验
在语音合成技术不断演进的背景下,早期版本的IndexTTS虽然在多场景应用中展现出良好的表现,但在情感表达的细腻度与时长控制的精准性方面仍存在提升空间。为了解决这些问题,并进一步推动零样本语音合成在实际场景中的落地能力,B站语音团队对模型架构与训练策略进行了深度优化,推出了全新一代语音合成模型——IndexTTS2 。
807 23
|
4天前
|
缓存 供应链 监控
VVIC seller_search 排行榜搜索接口深度分析及 Python 实现
VVIC搜款网seller_search接口提供服装批发市场的商品及商家排行榜数据,涵盖热销榜、销量排名、类目趋势等,支持多维度筛选与数据分析,助力选品决策、竞品分析与市场预测,为服装供应链提供有力数据支撑。
|
4天前
|
缓存 监控 API
Amazon item_review 商品评论接口深度分析及 Python 实现
亚马逊商品评论接口(item_review)可获取用户评分、评论内容及时间等数据,支持多维度筛选与分页调用,结合Python实现情感分析、关键词提取与可视化,助力竞品分析、产品优化与市场决策。