solon 集成 activemq-client (sdk)

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

xml 复制代码
<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

yaml 复制代码
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 配置器

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:

java 复制代码
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、代码应用

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

java 复制代码
@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"));
    }
}

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

java 复制代码
@Component
public class DemoMessageListener implements MessageListener {
    @Override
    public void onMessage(Message message) {
        System.out.println(message);
        RunUtil.runAndTry(message::acknowledge);
    }
}
相关推荐
熊大如如2 小时前
Java 反射
java·开发语言
猿来入此小猿2 小时前
基于SSM实现的健身房系统功能实现十六
java·毕业设计·ssm·毕业源码·免费学习·猿来入此·健身平台
goTsHgo3 小时前
Spring Boot 自动装配原理详解
java·spring boot
卑微的Coder3 小时前
JMeter同步定时器 模拟多用户并发访问场景
java·jmeter·压力测试
pjx9873 小时前
微服务的“导航系统”:使用Spring Cloud Eureka实现服务注册与发现
java·spring cloud·微服务·eureka
多多*4 小时前
算法竞赛相关 Java 二分模版
java·开发语言·数据结构·数据库·sql·算法·oracle
爱喝酸奶的桃酥4 小时前
MYSQL数据库集群高可用和数据监控平台
java·数据库·mysql
唐僧洗头爱飘柔95275 小时前
【SSM-SSM整合】将Spring、SpringMVC、Mybatis三者进行整合;本文阐述了几个核心原理知识点,附带对应的源码以及描述解析
java·spring·mybatis·springmvc·动态代理·ioc容器·视图控制器
骑牛小道士5 小时前
Java基础 集合框架 Collection接口和抽象类AbstractCollection
java
alden_ygq5 小时前
当java进程内存使用超过jvm设置大小会发生什么?
java·开发语言·jvm