spring集成消息代理ActiveMQ

spring集成消息代理ActiveMQ

使用ActiveMQ可以实现可靠的异步消息传递,它是一个流行的开源消息代理(Message Broker),实现了JMS(Java Message Service)规范。下面是基本的步骤和示例,展示如何在Java应用程序中使用ActiveMQ。

下载和安装ActiveMQ

首先,需要下载和安装ActiveMQ。可以从官方网站下载最新版本的ActiveMQ:(https://activemq.apache.org/).

启动ActiveMQ

安装完成后,通过命令行或启动脚本启动ActiveMQ。在命令行中导航到ActiveMQ安装目录的bin文件夹,然后执行以下命令:

activemq start

启动后可以在浏览器输入:http://127.0.0.1:8161/ 打开ActiveMQ的web控制台。

账户/密码都是:admin

停止 ActiveMQ 服务

如果是在 Windows 系统上,可以进入 ActiveMQ 的安装目录的bin文件夹,然后执行以下命令:

activemq stop

spring中集成ActiveMQ:

引入依赖:
<!-- Spring Core 和 Spring JMS 相关依赖 -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jms</artifactId>
    <version>5.3.24</version> <!-- 版本号根据你的实际需求和Spring版本进行调整 -->
</dependency>
<!-- ActiveMQ 相关依赖 -->
<dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>activemq-all</artifactId>
    <version>5.18.5</version> <!-- 版本号根据你的实际需求和ActiveMQ版本进行调整 -->
</dependency>
 <!--   Apache 的日志核心库     -->
 <dependency>
     <groupId>org.apache.logging.log4j</groupId>
     <artifactId>log4j-core</artifactId>
     <version>2.17.1</version> <!-- 根据需要选择合适的版本 -->
 </dependency>
配置类:

JMS (Java Message Service) 支持两种主要的消息传递模式:

  1. 点对点(Queue)模式
    • 生产者将消息发送到一个队列
    • 消费者从队列中接收消息。
    • 消息被发送到一个特定的队列中,只能由一个消费者接收,确保消息的独占处理。
  2. 发布/订阅(Topic)模式
    • 生产者将消息发布到一个主题
    • 多个消费者可以订阅这个主题并接收消息。
    • 消息被广播到所有订阅了该主题的消费者,确保消息的广播处理。

这两种模式各有不同的适用场景,根据需求选择合适的模式可以优化消息传递的效率和灵活性。

import org.apache.activemq.ActiveMQConnectionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;
import org.springframework.jms.core.JmsTemplate;
import javax.jms.ConnectionFactory;

@Configuration
@EnableJms //在 Spring 应用程序中启用 JMS(Java Message Service)相关的功能和配置
@ComponentScan(basePackages = {"com.yc.zy.jms"}) // 扫包
public class JmsConfig {

    // 配置连接工厂
    @Bean
    public ConnectionFactory connectionFactory() {
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
        connectionFactory.setBrokerURL("tcp://localhost:61616");// ActiveMQ 代理地址
        return  connectionFactory;
    }

    // 配置 JmsTemplate jms模板:消息发送 消息接收  消息转换 连接和会话管理
    @Bean
    public JmsTemplate jmsTemplate(ConnectionFactory connectionFactory) {
        JmsTemplate jmsTemplate =  new JmsTemplate();
        jmsTemplate.setConnectionFactory(connectionFactory);
//        jmsTemplate.setPubSubDomain(true); // 默认为 点对点(Queue)模式  设置为true表示发布/订阅(Topic)模式
        return jmsTemplate;
    }

    // 配置消息监听器容器工厂
    @Bean
    public JmsListenerContainerFactory<?> jmsListenerContainerFactory(ConnectionFactory connectionFactory) {
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
//        factory.setPubSubDomain(true); // 默认为点对点(Queue)模式  设置为true表示发布/订阅(Topic)模式
        return factory;
    }
}
消息生产者:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Component;

@Component
public class MessageProducer {

    @Autowired
    private JmsTemplate jmsTemplate; // 注入 JmsTemplate 实例

    public void sendMessage(String message) {
        jmsTemplate.convertAndSend("myQueue", message);// 发送消息到队列
        System.out.println("发送的消息为: " + message);
    }
}
消息消费者:
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

@Component
public class MyMessageListener {

    @JmsListener(destination = "myQueue") // 监听myQueue消息队列
    public void receiveMessage(String message) {
        System.out.println("用户接收到的消息: " + message);
    }
}
测试:
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class App {
    public static void main(String[] args) {
        ApplicationContext ac = new AnnotationConfigApplicationContext(JmsConfig.class);
        MessageProducer mp = (MessageProducer) ac.getBean("messageProducer");
        mp.sendMessage("Hello, World!");
    }
}

发布/订阅(Topic)模式:
在这里插入图片描述
在这里插入图片描述

总结

集成 ActiveMQ 与 Spring 结合使用,可以显著提高消息处理的效率和简化开发过程,同时利用 Spring 提供的功能增强应用程序的灵活性和可靠性。这种集成不仅提升了消息传递的功能,还简化了配置和管理。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值