原始状态的 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: 5000
backOffMultiplier: 2
useExponentialBackOff: true
maximumRedeliveries: -1
maximumRedeliveryDelay: 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;
} @Bean
public IProducer producer(Connection connection) throws Exception {
return new IProducer(connection);
} @Bean
public 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));
} @FunctionalInterface
public static interface MessageBuilder {
Message build(Session session) throws JMSException;
}
}

3、代码应用

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

@Controller
public class DemoController {
@Inject
private 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 {
@Override
public void onMessage(Message message) {
System.out.println(message);
RunUtil.runAndTry(message::acknowledge);
}
}

solon 集成 activemq-client (sdk)的更多相关文章

  1. 《连载 | 物联网框架ServerSuperIO教程》- 18.集成OPC Client,及使用步骤

    1.C#跨平台物联网通讯框架ServerSuperIO(SSIO)介绍 <连载 | 物联网框架ServerSuperIO教程>1.4种通讯模式机制. <连载 | 物联网框架Serve ...

  2. 从零开始学 Java - Spring 集成 ActiveMQ 配置(一)

    你家小区下面有没有快递柜 近两年来,我们收取快递的方式好像变了,变得我们其实并不需要见到快递小哥也能拿到自己的快递了.对,我说的就是类似快递柜.菜鸟驿站这类的代收点的出现,把我们原来快递小哥必须拿着快 ...

  3. 从零开始学 Java - Spring 集成 ActiveMQ 配置(二)

    从上一篇开始说起 上一篇从零开始学 Java - Spring 集成 ActiveMQ 配置(一)文章中讲了我关于消息队列的思考过程,现在这一篇会讲到 ActivMQ 与 Spring 框架的整合配置 ...

  4. 在Spring下集成ActiveMQ

    1.参考文献 Spring集成ActiveMQ配置 Spring JMS异步发收消息 ActiveMQ 2.环境 在前面的一篇ActiveMQ入门实例中我们实现了消息的异步传送,这篇博文将如何在spr ...

  5. 在spring环境下集成ActiveMQ

    1.参考文献 Spring集成ActiveMQ配置 Spring JMS异步发收消息 ActiveMQ 2.环境 在前面的一篇ActiveMQ入门实例中我们实现了消息的异步传送,这篇博文将如何在spr ...

  6. SpringBoot集成ActiveMQ

    前面提到了原生API访问ActiveMQ和Spring集成ActiveMQ.今天讲一下SpringBoot集成ActiveMQ.SpringBoot就是为了解决我们的Maven配置烦恼而生,因此使用S ...

  7. Android Studio快速集成讯飞SDK实现文字朗读功能

    今天,我们来学习一下怎么在Android Studio快速集成讯飞SDK实现文字朗读功能,先看一下效果图: 第一步 :了解TTS语音服务 TTS的全称为Text To Speech,即“从文本到语音” ...

  8. 【ActiveMQ】Spring Jms集成ActiveMQ学习记录

    Spring Jms集成ActiveMQ学习记录. 引入依赖包 无论生产者还是消费者均引入这些包: <properties> <spring.version>3.0.5.REL ...

  9. Unity 下集成第三方原生 SDK,以极光厂商通道为例

    Unity中集成三方SDK有两种方式: Unity 项目开发中时常有集成 Android 第三方 SDK 的需求,比如接入第三方推送,分享等功能.而第三方 SDK 的集成文档提到的往往是基于原生 An ...

  10. Spring集成ActiveMQ配置 --转

    转自:http://suhuanzheng7784877.iteye.com/blog/969865 集成环境 Spring采用2.5.6版本,ActiveMQ使用的是5.4.2,从apache站点可 ...

随机推荐

  1. Springboot 读取 resource 目录下的Excel文件并下载

    如果 inputStream 为null ,或者提示 文件路径不存在,执行 mvn clean 并 重启项目,查看target 目录下是否存在该文件 @GetMapping("/downlo ...

  2. kali系统安装和CVE-2017-12615测试

    1 安装kali系统 1.1 下载VMware压缩包 kali-linux-2022.1 默认的用户和密码是kali 1.2 初始化系统 sudo apt update -y #kali sudo a ...

  3. 更新预警(bushi)

    一回首,上次更新已经是将近3个月前了.但是博主不是似了,也不是逍遥快活游山玩水纸醉金迷乐不思蜀,而是上班太忙还是单休,所以没什么时间更新博客.但是今天我要开始忏悔了!预计更新以下几个专题(不一定真的会 ...

  4. .Net 反射的学习

    // 反射 // 一切从 type 开始 2 种拿到 type 的方式 // 作用:动态的操作对象 获取属性 方法 特性 // 1. 拿到对象的 type // typeof(类); // 2. 拿到 ...

  5. 云原生周刊:Karmada 成为 CNCF 孵化项目 | 2023.12.25

    开源项目推荐 kubernetes-reflector Reflector 是一个 Kubernetes 的插件,旨在监视资源(secrets 和 configmaps)的变化,并将这些变化反映到同一 ...

  6. ansible开局配置-openEuler

    ansible干啥用的就不多介绍了,这篇文章主要在说ansible的安装.开局配置.免密登录. ansible安装 查看系统版本 cat /etc/openEuler-latest 输出内容如下: o ...

  7. pwn V8入门

    V8入门 && StarCTF oob 搭建环境的步骤如下: 环境搭建 #depot_tools git clone https://chromium.googlesource.com ...

  8. ConsulManager应用场景1:如何优雅的基于Consul自动同步ECS主机监控

    [ConsulManager介绍] Consul字段设计说明 服务首次启动时会创建一个随机秘钥,存放到consul_kv的/ConsulManager/assets/secret/skey,该秘钥用于 ...

  9. Flink Checkpoint & Savepoint

    Flink checkpoint Checkpoint是Flink实现容错机制最核心的功能,能够根据配置周期性地基于Stream中各个Operator的状态来生成Snapshot,从而将这些状态数据定 ...

  10. cmu15545-数据存储(Database Storage)

    蓝图 数据库自己管理磁盘数据和缓冲区,而不是通过操作系统管理(Os is not your friend.). 三层视图 数据库以页(page)为存储数据的基本单位,文件(file)是一系列页的集合, ...