springboot集成ActiveMQ

一、出现以下异常时,需要注意,receive获取消息时不可有返回值,否则循环报此异常

Execution of JMS message listener failed, and no ErrorHandler has been set.

二、如果使用pool,记得设置为false否则报错

#是否用Pooledconnectionfactory代替普通的ConnectionFactory
spring.activemq.pool.enabled=false

ActiveMQ页面列表说明:
Number Of Pending Messages:消息队列中待处理的消息
Number Of Consumers:消费者的数量
Messages Enqueued:累计进入过消息队列的总量
Messages Dequeued:累计消费过的消息总量

定时调度功能:
直接在发送消息类名之上加上@EnableScheduling,
发送消息的方法之上加上注解@Scheduled(fixedDelay = 5000),fixedDelay单位为毫秒

说明:
需要建立两个springboot项目
广播模式:群发
订阅模式:点对点

		<!--消息队列-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-activemq</artifactId>
        </dependency>
        <!--消息队列连接池-->
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-pool</artifactId>
            <version>5.15.0</version>
        </dependency>

发布方配置

server:
  #Tomcat端口
  port: 8099
spring:
  activemq:
    # MQ所在的服务器的地址
    broker-url: tcp://localhost:61616

发布方配置代码

import javax.jms.Queue;
import javax.jms.Topic;

import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class BeanConfig {

    //定义存放消息的队列
    @Bean
    public Queue queue() {
        return new ActiveMQQueue("ActiveMQQueue");
    }

    //定义存放消息的队列
    @Bean
    public Topic topic() {
        return new ActiveMQTopic("ActiveMQTopic");
    }
}

发布方接口代码

import javax.jms.Queue;
import javax.jms.Topic;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class ProviderController {

    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    @Autowired
    private Queue queue;

    @Autowired
    private Topic topic;


    @RequestMapping("/sendqueue")
    public void sendqueue(String msg) {
        // 指定消息发送的目的地及内容
        System.out.println("成功发送订阅消息:" + msg);
        this.jmsMessagingTemplate.convertAndSend(this.queue, msg);
    }

    @RequestMapping("/sendtopic")
    public void sendtopic(String msg) {
        // 指定消息发送的目的地及内容
        System.out.println("成功发送广播消息:" + msg);
        this.jmsMessagingTemplate.convertAndSend(this.topic, msg);
    }
}

消费方配置

server:
  #Tomcat端口
  port: 8077 
spring:
  activemq:
    # MQ所在的服务器的地址
    broker-url: tcp://localhost:61616
  #true开启广播模式,订阅模式无法获取消息
  #false开启订阅模式,广播模式无法获取消息
  jms:
    pub-sub-domain: false

消费方代码

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Component;

import javax.jms.JMSException;
import javax.jms.TextMessage;


@Component
public class ConsumerService {

    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;


    @JmsListener(destination = "ActiveMQQueue")// 使用JmsListener配置消费者监听的队列,destination是队列名称
//    @SendTo("SQueue")// SendTo 会将此方法返回的数据,发送到消息队列 ,否则不设置返回类型-void
    public void receviceQueueMsg(String msg) {
        System.out.println("成功接受订阅消息:" + msg);
    }

    @JmsListener(destination="ActiveMQTopic")
    public void receviceTopicMsg(TextMessage msg) {//注:此处方法不可有返回值
        String str = "";
        try {
            str = msg.getText();
            System.out.println("成功接收广播消息:"+ msg.getText());
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
}
  • 0
    点赞
  • 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、付费专栏及课程。

余额充值