SpringBoot集成 ActiveMQ —— 队列模型实战

介绍SpringBoot集成ActiveMQ的使用。

整个会放在下载区(无需积分)
下载地址SpringBoot集成 ActiveMQ

配置文件

pom.xml

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- 依赖于starter, 以使用自动配置 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-activemq</artifactId>
        </dependency>

        <!-- 依赖于连接池, 以启用JMS 连接池 -->
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-pool</artifactId>
        </dependency>

    </dependencies>

application.yml

spring:
  # activemq的配置
  activemq:
    # 服务器地址
    broker-url: tcp://localhost:61616
    # 账户名、密码
    user: admin
    password: admin
    pool:
      # 连接池最大连接数
      max-connections: 50
      # 是否启动连接池
      enabled: true
  # jms的规范
  jms:
    # false为队列模式(默认)、true是发布订阅模式
    pub-sub-domain: false

# 自定义队列
tohandle-queue: tohandle-queue
handled_queue: handled_queue

源代码

消费者代码

package com.jsy.demoactivemqconsumer.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;

@Service
public class MessageConsumer {

    // 注入有Springboot生成的 jmsTemplate
    @Autowired
    JmsTemplate jmsTemplate;

    @Value("${handled_queue}")
    String queue;

    @JmsListener(destination = "${tohandle-queue}")
    public void receive(String message) {
        System.out.println("消费者受到消息,现在是 ");
        System.out.println("消息内容是" + message);
        System.out.println();
        // 再发送给消费者
        sendMessage("来自消费者的回声 "+message);
    }

    public void sendMessage(String message) {
        jmsTemplate.convertAndSend(queue,message);
    }


}

生产者代码

package com.jsy.demoactivemq.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;

@Service
public class MessageProducer {
    @Autowired
    JmsTemplate jmsTemplate;

    @Value("${tohandle-queue}")
    String queue;

    public void send(String message){
        jmsTemplate.convertAndSend(queue,message);
    }


    @JmsListener(destination = "${handled_queue}")
    public void receive(String message) {
        System.out.println(message);
    }

}

定时任务(启动类上加上@EnableScheduling注解)

package com.jsy.demoactivemq.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;


@Service
public class ScheduledService {

    int cnt = 0;

    @Autowired
    MessageProducer messageProducer;

//    开启定时任务
    @Async
    @Scheduled(fixedRate = 1000)
    public void job() {
        System.out.println("我是生产者:  我要给消息队列发送一条消息");
        messageProducer.send("来自生产者的消息  id:" + (++cnt));
    }
}

执行效果就是一个生产者轮流给三个消费者发消息,然后,消费者在返回给生产者。

测试结果

消费者的输出:

消费者受到消息,现在是 
消息内容是来自生产者的消息  id:51

消费者受到消息,现在是 
消息内容是来自生产者的消息  id:54

消费者受到消息,现在是 
消息内容是来自生产者的消息  id:57

消费者受到消息,现在是 
消息内容是来自生产者的消息  id:60

消费者受到消息,现在是 
消息内容是来自生产者的消息  id:63
.......

生产者的输出

我是生产者:  我要给消息队列发送一条消息
来自消费者的回声 来自生产者的消息  id:46
我是生产者:  我要给消息队列发送一条消息
来自消费者的回声 来自生产者的消息  id:47
我是生产者:  我要给消息队列发送一条消息
来自消费者的回声 来自生产者的消息  id:48
.....

有一点是值得关注的。
由于有三个消费者,所以,生产者生产的消息将会被按模3循环分发给三个消费者。

Web管理端。
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Spring Boot 集成 ActiveMQ简单,只需要在项目的 pom.xml 文件中添加 ActiveMQ 的依赖,然后在配置文件 application.properties 中配置 ActiveMQ 的地址即可。具体步骤如下: 1. 在 pom.xml 文件中添加 ActiveMQ 的依赖: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> </dependency> ``` 2. 在 application.properties 文件中配置 ActiveMQ 的地址,例如: ``` spring.activemq.broker-url=tcp://localhost:61616 ``` 3. 在启动类中添加 @EnableJms 注解,表示启用 JMS。 4. 创建一个 JmsTemplate 对象来发送和接收消息。 完成以上步骤即可在 Spring Boot 中使用 ActiveMQ。 ### 回答2: Spring Boot是Spring Framework的一个用于简化Spring应用程序开发和部署的框架。它提供了一种简单的方式来开发、构建和运行Spring应用程序,同时也能方便地集成其他框架和技术。 ActiveMQ是一个流行的开源消息代理,它支持多种协议和编程语言,包括Java、C#、C++、Python等。它提供了可靠的消息传递机制,可以用于构建分布式系统和异步消息处理应用。 Spring Boot提供了对ActiveMQ集成支持,可以通过在pom.xml文件中添加相关依赖和配置,实现快速集成和使用ActiveMQ。 下面是详细的步骤: 1. 在pom.xml文件中添加ActiveMQ的依赖: ``` <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> </dependency> </dependencies> ``` 2. 在application.properties文件中添加ActiveMQ的配置: ``` spring.activemq.broker-url=tcp://localhost:61616 spring.activemq.user=admin spring.activemq.password=admin ``` 其中broker-url是ActiveMQ的连接地址,user和password是登录信息。 3. 在代码中使用JmsTemplate发送和接收消息: ``` @Service public class MessageService { @Autowired private JmsTemplate jmsTemplate; public void sendMessage(String message) { jmsTemplate.send("test.queue", new MessageCreator() { public Message createMessage(Session session) throws JMSException { return session.createTextMessage(message); } }); } @JmsListener(destination = "test.queue") public void receiveMessage(String message) { System.out.println("Received message: " + message); } } ``` 上面的代码定义了一个MessageService类,里面包含了一个sendMesssge方法和一个receiveMessage方法。sendMessage方法将消息发送到名为“test.queue”的队列中,而receiveMessage方法则监听该队列并接收消息。 在使用JmsTemplate发送消息时,需要提供消息的目的地和消息内容。这里使用createTextMessage方法创建一个文本消息,并将其发送到指定的队列中。 在使用@JmsListener注解接收消息时,需要指定监听的队列名称和接收消息的方法。接收到消息后,将会调用该方法并输出消息内容。 4. 运行应用程序并测试消息发送和接收功能。 启动应用程序后,调用MessageService的sendMessage方法即可发送消息到ActiveMQ,同时在控制台输出了接收到的消息。这样,就成功地实现了Spring Boot和ActiveMQ集成。 总之,通过以上步骤,我们可以很容易地将Spring Boot和ActiveMQ进行集成,在开发分布式系统和消息处理应用时,能够方便地使用ActiveMQ提供的可靠消息传递机制。 ### 回答3: Spring Boot是一种基于Spring框架的快速开发框架,它提供了很多便捷快捷的特性。ActiveMQ则是一种高性能、开源的消息传递中间件。Spring Boot和ActiveMQ的结合,能够搭建一个可插拔、可扩展、可重复利用的消息解决方案。下面将介绍如何在Spring Boot中集成ActiveMQ。 1、引入所需依赖 首先需要在项目中引入ActiveMQ相关的依赖: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> </dependency> 2、配置ActiveMQ 在application.properties文件中配置ActiveMQ,如下所示: #ActiveMQ configuration spring.activemq.broker-url=tcp://localhost:61616 spring.activemq.user=admin spring.activemq.password=admin spring.jms.template.default-destination=test-queue spring.jms.listener.auto-startup=true 其中,spring.activemq.broker-url指定了ActiveMQ服务器的地址;spring.activemq.user和spring.activemq.password是登录ActiveMQ服务器的用户名和密码;spring.jms.template.default-destination设置了默认的目的地队列;spring.jms.listener.auto-startup设置消息监听器的自动启动。 3、编写消息生产者代码 @Inject private JmsMessagingTemplate jmsMessagingTemplate; public void produceMessage(String message) { jmsMessagingTemplate.convertAndSend("test-queue", message); } 首先在类中注入JmsMessagingTemplate,它是Spring Boot提供的用于发送消息的工具类。然后编写produceMessage()方法,将消息发送到目的地队列test-queue中。 4、编写消息消费者代码 @JmsListener(destination="test-queue") public void receiveMessage(String message) { System.out.println("Received message: " + message); } 使用@JmsListener注解,在方法上指定监听的目的地队列为test-queue。当有消息到达test-queue时,就会自动调用receiveMessage()方法,进行消息处理。 通过上述步骤,就可以在Spring Boot中集成ActiveMQ,实现消息生产者和消息消费者的功能。当然,还可以通过其他方式来实现集成,例如使用ConnectionFactory或者JmsTemplate等Spring集成方式。这样就能够应用在更多的项目中,提高应用的可靠性和可扩展性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值