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

solon 集成 activemq-client (sdk)

原始状态的 activemq-client sdk 集成非常方便,也更适合定制。就是有些同学,可能对原始接口会比较陌生,会希望有个具体的示例。

<dependency><groupId>org.apache.activemq</groupId><artifactId>activemq-client</artifactId><version>${activemq.version}</version>
</dependency><dependency><groupId>org.apache.activemq</groupId><artifactId>activemq-pool</artifactId><version>${activemq.version}</version>
</dependency>

希望更加简化使用的同学,可以使用:

activemq-solon-cloud-plugin (使用更简单,定制性弱些)

1、添加集成配置

先使用 Solon 初始器 先生成一个 Solon Web 模板项目,然后添加上面的 activemq-client 依赖。再做个配置约定(也可按需定义):

  • “solon.activemq”,作为配置前缀
    • “properties”,作为公共配置
    • “producer”,作为生态者专属配置(估计用不到)
    • “consumer”,作为消费者专属配置(估计用不到)

具体的配置属性,参考自:ActiveMQConnectionFactory

solon.app:name: "demo-app"group: "demo"# 配置可以自由定义,与 @Bean 代码对应起来即可(以下为参考)
solon.activemq:properties:  #公共配置(配置项,参考:ActiveMQConnectionFactory)brokerURL: "failover:tcp://localhost:61616"redeliveryPolicy:initialRedeliveryDelay: 5000backOffMultiplier: 2useExponentialBackOff: truemaximumRedeliveries: -1maximumRedeliveryDelay: 3600_000

添加 java 配置器

@Configuration
public class ActivemqConfig {@Bean(destroyMethod = "stop")public Connection client(@Inject("${solon.activemq.properties}") Props common) throws Exception {String brokerURL = (String) common.remove("brokerURL");String userName = (String) common.remove("userName");String password = (String) common.remove("password");ActiveMQConnectionFactory factory;if (Utils.isEmpty(userName)) {factory = new ActiveMQConnectionFactory(brokerURL);} else {factory = new ActiveMQConnectionFactory(brokerURL, userName, password);}//绑定额外的配置并创建连接Connection connection = common.bindTo(factory).createConnection();connection.start();return connection;}@Beanpublic IProducer producer(Connection connection) throws Exception {return new IProducer(connection);}@Beanpublic void consumer(Connection connection,MessageListener messageListener) throws Exception {Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);Destination destination = session.createTopic("topic.test");MessageConsumer consumer = session.createConsumer(destination);consumer.setMessageListener(messageListener);}
}

activemq 的消息发送的代码比较复杂,所以我们可以做个包装处理(用于上面的配置构建),临时命名为 IProducer:

public class IProducer {private Connection connection;public IProducer(Connection connection) {this.connection = connection;}public void send(String topic, MessageBuilder messageBuilder) throws JMSException {Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);Destination destination = session.createTopic(topic);MessageProducer producer = session.createProducer(destination);producer.send(destination, messageBuilder.build(session));}@FunctionalInterfacepublic static interface MessageBuilder {Message build(Session session) throws JMSException;}
}

3、代码应用

发送(或生产),这里代控制器由用户请求再发送消息(仅供参考):

@Controller
public class DemoController {@Injectprivate IProducer producer;@Mapping("/send")public void send(String msg) throws Exception {//发送producer.send("topic.test", s -> s.createTextMessage("test"));}
}

监听(或消费),这里采用订阅回调的方式:(仅供参考)

@Component
public class DemoMessageListener implements MessageListener {@Overridepublic void onMessage(Message message) {System.out.println(message);RunUtil.runAndTry(message::acknowledge);}
}
http://www.lryc.cn/news/507571.html

相关文章:

  • LRU 缓存
  • 使用ZLMediaKit 开源项目搭建RTSP 服务器
  • 数组晨考2day08
  • 《鸿蒙HarmonyOS应用开发从入门到精通(第2版)》简介
  • 麒麟操作系统服务架构保姆级教程(二)sersync、lsync备份和NFS持久化存储
  • 将OBJ或GLB文件转换为3DTiles
  • Flink DataStream API 编程指南
  • tryhackme-Pre Security-HTTP in Detail(HTTP的详细内容)
  • 探索 Plotly:一个强大的交互式数据可视化库
  • Oracle 查询表占用空间(表大小)的方法
  • 机器人国际会议IROS论文latex模板
  • 雪泥鸿爪和屈指可数
  • 2024年度个人总结
  • ChatGPT接口测试用例生成的流程
  • 【读书笔记】《论语别裁》真人和假人
  • JS字符串方法汇总
  • CentOs7使用yum安装docker
  • 蓝桥杯刷题——day8
  • 如何使用 WebAssembly 扩展后端应用
  • BaseCTF_web_week3
  • 模型数据算法概论
  • 什么是3DEXPERIENCE SOLIDWORKS,它有哪些角色和功能?
  • Sigrity System SI Parallel Bus Analysis模式进行DDR3仿真分析-传输线模型
  • MacOS下PostIn安装配置指南
  • 【Leetcode 每日一题】2545. 根据第 K 场考试的分数排序
  • 一文速通 IIC I2C子系统驱动 通信协议原理 硬件 时序 深度剖析
  • HarmonyOS(72)事件拦截处理详解
  • docker(wsl)命令 帮助文档
  • nginx 拦截指定ip访问指定 url
  • git仓库的基本概念和流程以及一些基本命令