SpringBoot整合ActiveMq

先了解什么是Mq

消息队列,是基础数据结构中“先进先出”的一种数据结构。指把要传输的数据(消息)放在队列中,用队列机制来实现消息传递——生产者产生消息并把消息放入队列,然后由消费者去处理。消费者可以到指定队列拉取消息,或者订阅相应的队列,由MQ服务端给其推送消息。

Mq的优势

(1)异步。调用者无需等待。
(2)解耦。解决了系统之间耦合调用的问题。
(3)消峰。抵御洪峰流量,保护了主业务。

Mq的落地产品

在这里插入图片描述

本次主要做的实验是SpringBoot整合ActiveMq实现消息队列的生产消费。以及主题的生产消费。

第一步添加ActiveMq与SpringBoot的整合依赖

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-activemq</artifactId>
            <version>2.7.8</version>
 </dependency>

第一步实现消息队列模式
配置yml配置文件

spring:
  #配置ActiveMq的服务器地址
  activemq:
    broker-url: tcp://localhost:61616
  jms:
#    订阅与发布模式打开/关闭
    pub-sub-domain: false

写一个生产者的接口

我在service层中注入了

    @Autowired
    private JmsMessagingTemplate messagingTemplate;

方法是

  @Override
    public void sendMessage(int random) {
        System.out.println("发送消息随机数"+random);
        ActiveMQQueue activeMQQueue = new ActiveMQQueue("money.id");
        try {
            messagingTemplate.convertAndSend(activeMQQueue,random);
        }catch (Exception e){
            throw new RuntimeException(e.getMessage());
        }
    }

在控制器中调用

   /**
     * MQ生产者生产队列消息
     */
    @RequestMapping("/product")
    public AjaxResult product(){
        Random random = new Random();
        carfeeService.sendMessage(random.nextInt(100));
       return AjaxResult.success("生产成功");
    }

配置消费者

@Component
public class MessageListerner {

    @JmsListener(destination = "${queue}")
    public void receive1(String id){
        System.out.println("消费队列1 接收到mq的信息"+id);
    }
    @JmsListener(destination = "${queue}")
    public void receive2(String id){
        System.out.println("消费队列2 接收到mq的信息"+id);
    }
 }

配置了两个消费者进行消费消息。执行结果在这里插入图片描述
订阅与发布模式
需要修改Yml配置文件 pub-sub-domain: true

spring:
  #配置ActiveMq的服务器地址
  activemq:
    broker-url: tcp://localhost:61616
  jms:
#    订阅与发布模式打开/关闭
    pub-sub-domain: true

然后生产者核心代码

 @Override
    public void subProduct(String message) {
        System.out.println("发送订阅与发布 "+message);
        ActiveMQTopic activeMQTopic = new ActiveMQTopic("topic");
        messagingTemplate.convertAndSend(activeMQTopic,message);
    }

在控制器中的调用

    @RequestMapping("subProduct")
    public AjaxResult subProduct(){
        Random random = new Random();
        try{
            String[] messages=new String[]{"你好世界","鸡你太美","111","222"};
            carfeeService.subProduct(messages[random.nextInt(messages.length)]);
        }catch (Exception e){
          logger.info(e.getMessage());
        }
        return AjaxResult.success();
    }

配置订阅与发布的消费者
因为是主题 索引几个消费者订阅 者几个消费者都会收到消息。类似微信公众号。

@Component
public class MessageListerner {
    @JmsListener(destination = "topic")
    public void receiveSub1(String message){
        System.out.println("消费主题1 接收到mq的信息"+message);
    }
    @JmsListener(destination = "topic")
    public void receiveSub2(String message){
        System.out.println("消费主题2 接收到mq的信息"+message);
    }
    @JmsListener(destination = "topic")
    public void receiveSub3(String message){
        System.out.println("消费主题3 接收到mq的信息"+message);
    }
    @JmsListener(destination = "topic")
    public void receiveSub4(String message){
        System.out.println("消费主题4 接收到mq的信息"+message);
    }
}

测试结果 可以实现。
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Spring Boot是一种轻量级的应用程序框架,可以将主要精力集中在开发业务逻辑上,而不必担心处理框架导致的繁琐情况。而ActiveMQ是一种可靠的、开源的JMS消息队列系统,能够保证传输的消息在多个客户端之间的可靠性。 Spring Boot可以与ActiveMQ轻松集成,使应用程序能够轻松地使用消息中间件来处理消息队列。要在Spring Boot集成ActiveMQ,需要使用官方提供的ActiveMQ Starter依赖库,该库提供了必要的配置和依赖项。 首先,在pom.xml中添加以下依赖项: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> </dependency> ``` 接下来,在application.properties文件中添加以下配置项: ``` spring.activemq.broker-url=tcp://localhost:61616 spring.activemq.user=admin spring.activemq.password=admin ``` 这将配置ActiveMQ连接到本地主机的61616端口,并设置默认的用户名和密码。 最后,可以使用Spring的JMS组件来创建JMS连接和消息接收器,以便接收和处理消息。以下是一个简单的示例: ``` @Component public class MessageReceiver { @JmsListener(destination = "myQueue") public void receiveMessage(String message) { System.out.println("Received message: " + message); } } ``` 该类使用Spring的@JmsListener注释来指定要监听的队列,然后使用Spring DI自动注入消息。 综上所述,通过使用Spring BootActiveMQ集成,可以轻松快捷地构建应用程序的消息队列功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值