当前位置: 首页 > news >正文

Springboot中使用activemq

1. 引入ActiveMQ的SpringBoot插件

<!-- ActiveMQ --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> </dependency>

2. application中增加activemq的配置

spring: activemq: #ActiveMQ服务器地址 broker-url: tcp://127.0.0.1:61616 user: admin password: admin jms: #false == Queue ; true == Topic ; 默认为false pub-sub-domain: false

3. Java代码

package com.geofly.apicall.mq; import cn.hutool.json.JSONObject; import cn.hutool.json.JSONUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.geofly.apicall.model.entity.FlowConfig; import com.geofly.apicall.service.FlowConfigService; import com.geofly.apicall.service.FlowEngineService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jms.annotation.JmsListener; import org.springframework.stereotype.Component; import java.util.Map; /** * ActiveMQ 消息消费者 * 接收消息并触发指定的流程 */ @Slf4j @Component public class ActiveMQConsumer { @Autowired private FlowConfigService flowConfigService; @Autowired private FlowEngineService flowEngineService; /** * 接收消息并触发流程 * 消息格式示例: {"triggerName": "workorder_finish", "params": {"id": 123, "status": "done"}} * * @param message JSON 格式的消息字符串 */ @JmsListener(destination = "${spring.activemq.destination:flow.trigger.destination}") public void receiveMessage(String message) { log.info("接收到 ActiveMQ 消息: {}", message); try { // 1. 解析消息 if (!JSONUtil.isTypeJSONObject(message)) { log.warn("消息不是有效的 JSON 对象: {}", message); return; } JSONObject json = JSONUtil.parseObj(message); String triggerName = json.getStr("triggerName"); // 兼容性处理:如果消息中直接传了 flowCode 也可以 String flowCode = json.getStr("flowCode"); Map<String, Object> params = null; if (json.containsKey("params")) { params = json.get("params", Map.class); } // 2. 查找流程 FlowConfig flowConfig = null; if (triggerName != null) { flowConfig = flowConfigService.getByTriggerName(triggerName); } else if (flowCode != null) { flowConfig = flowConfigService.getOne( new LambdaQueryWrapper<FlowConfig>() .eq(FlowConfig::getFlowCode, flowCode) ); } if (flowConfig == null) { log.error("未找到对应的流程,triggerName: {}, flowCode: {}", triggerName, flowCode); return; } // 3. 检查状态 if (flowConfig.getStatus() != 1) { log.warn("流程已禁用: {} ({})", flowConfig.getFlowName(), flowConfig.getFlowCode()); return; } // 4. 执行流程 log.info("从 ActiveMQ 消息触发流程: {} (ID: {})", flowConfig.getFlowName(), flowConfig.getId()); flowEngineService.executeFlow(flowConfig.getId(), params); } catch (Exception e) { log.error("处理 ActiveMQ 消息异常: {}", message, e); } } }

这里监听了一个事件,如果ActiveMQ收到相应的事件就会触发这个方法

http://www.jsqmd.com/news/274762/

相关文章:

  • 公路修建(洛谷P1265)
  • 程序监控与异常防护-PART-Simulink-看门狗
  • 1120
  • LIDA 477 编码器位移/速度/加速度采集与转换-PART-LIDA 477-采集转换
  • 文件IO
  • 1121
  • 软件升级回退报告
  • SQL Server数据库
  • 1124
  • 1125
  • 灵活用工系统开发全流程与案例分享【弹性用工解决方案|附源码】
  • RocksDB 可直接运行的实战示例(多语言 + 完整安装 + 基础 CRUD + 事务 + 生产调优)
  • 7月4日
  • VideoDownloadHelper视频下载助手终极指南:全网视频轻松保存
  • 专业陪诊系统:守护银发健康
  • 1126
  • 1013
  • RocksDB 全面指南
  • 7月5日
  • 1128
  • Python 自动去除 代码中Debug 代码的终极方案(AST 实战)
  • 亲测好用10个AI论文软件,专科生轻松搞定毕业论文!
  • 1015
  • 1016
  • 2026年最大风口:AI应用全面爆发,五大核心板块提前布局(附收藏清单)
  • FinBERT详解
  • 1210
  • 2026年郑州喷涂机服务商TOP5推荐:钢结构喷涂机、油漆喷涂机、防腐油漆喷涂机、无气喷涂机、双组份喷涂机、气动喷涂机、品牌适配、场景覆盖与务实服务之选 - 海棠依旧大
  • 1211
  • 【深度收藏】RLHF训练全解析:人类反馈如何塑造ChatGPT的对话能力