SpringBoot整合ActiveMQ消费者未启动问题解析与解决方案

一、摘要

新建SpringBoot项目集成ActiveMQ后,应用启动即退出。原因为未配置消息监听器,导致无持续任务维持运行。通过添加@JmsListener注解的监听器类并确保组件扫描,使消费者正常监听队列,应用保持运行。验证消息收发功能正常,问题解决。

二、问题描述

新建的SpringBoot项目,依赖只有 spring-boot-starter-activemq,直接启动,发现消费者没有在后台运行,启动后就退出了。

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/

 :: Spring Boot ::                (v3.4.4)

2025-04-06T17:34:44.670+08:00  INFO 12848 --- [hello-activemq] [           main] c.e.h.activemq.HelloActivemqApplication  : Starting HelloActivemqApplication using Java 21.0.1 with PID 12848 (E:\hello-world\hello-activemq\target\classes started by SongGuanxun in E:\hello-world\hello-activemq)
2025-04-06T17:34:44.677+08:00  INFO 12848 --- [hello-activemq] [           main] c.e.h.activemq.HelloActivemqApplication  : No active profile set, falling back to 1 default profile: "default"
2025-04-06T17:34:47.034+08:00  INFO 12848 --- [hello-activemq] [           main] c.e.h.activemq.HelloActivemqApplication  : Started HelloActivemqApplication in 3.821 seconds (process running for 4.932)

进程已结束,退出代码为 0

三、环境与问题复现

1. 项目依赖、环境配置与核心代码

POM

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.4.4</version>
        <relativePath/>
    </parent>

    <groupId>com.example</groupId>
    <artifactId>hello-activemq</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>hello-activemq</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>21</java.version>
    </properties>

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

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </path>
                    </annotationProcessorPaths>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

YAML

spring:
  application:
    name: hello-activemq
  active-mq:
    broker-url: tcp://localhost:61616
    user: admin
    password: admin

Application

package com.example.hello.activemq;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class HelloActivemqApplication {

    public static void main(String[] args) {
        SpringApplication.run(HelloActivemqApplication.class, args);
    }

}

2. 异常现象(应用启动后退出)

四、原因分析

根据你的描述和日志信息,问题可能出在消费者配置不正确或未被Spring Boot正确识别,导致应用启动后没有需要持续运行的任务,从而退出。以下是详细分析和解决方案:

1. 可能原因分析

  1. 缺少消息监听器配置

    • 没有在代码中定义带有@JmsListener注解的消费者类,导致Spring Boot无法识别需要监听的消息队列。
    • 监听器类未被Spring管理(如未添加@Component@Service注解)。
  2. 应用未保持运行

    • 如果应用启动后没有需要持续运行的任务(如HTTP服务器、定时任务或消息监听器),Spring Boot会立即退出。

2. 解决方案步骤

(1)创建消息监听器

创建一个监听器类,使用@JmsListener注解监听队列:

import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

@Component
public class MessageConsumer {

    @JmsListener(destination = "myQueue")
    public void receiveMessage(String message) {
        System.out.println("Received message: " + message);
    }
}

(2)确保监听器被Spring扫描到

  • 监听器类必须位于Spring Boot应用的主类(带有@SpringBootApplication注解的类)的包或子包中。
  • 如果监听器在其他包中,请通过@ComponentScan显式指定包路径:
    @SpringBootApplication
    @ComponentScan(basePackages = {"com.example.activemq", "com.example.listener"})
    public class HelloActivemqApplication {
        // ...
    }
    

3. 预期结果

  • 启动应用后,控制台应显示“Started HelloActivemqApplication”并保持运行。
  • 发送消息到myQueue时,监听器会打印接收的消息。

五、解决方案实现

1. 创建消息监听器

创建一个监听器类,使用@JmsListener注解监听队列:

package com.example.hello.activemq.consumer;

import lombok.extern.slf4j.Slf4j;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class QueueConsumer {

    @JmsListener(destination = "your-queue-name")
    private void receive(String message) {
        log.info("队列消费者,接收消息: {}", message);
    }

}

2. 确保监听器被Spring扫描到

监听器类 QueueConsumer 位于Spring Boot应用的主类 HelloActivemqApplication 的子包中。

六、功能验证

1. 启动成功并保持运行

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/

 :: Spring Boot ::                (v3.4.4)

2025-04-06T19:15:11.131+08:00  INFO 4132 --- [hello-activemq] [           main] c.e.h.activemq.HelloActivemqApplication  : Starting HelloActivemqApplication using Java 21.0.1 with PID 4132 (E:\hello-world\hello-activemq\target\classes started by SongGuanxun in E:\hello-world\hello-activemq)
2025-04-06T19:15:11.137+08:00  INFO 4132 --- [hello-activemq] [           main] c.e.h.activemq.HelloActivemqApplication  : No active profile set, falling back to 1 default profile: "default"
2025-04-06T19:15:13.448+08:00  INFO 4132 --- [hello-activemq] [           main] c.e.h.activemq.HelloActivemqApplication  : Started HelloActivemqApplication in 3.861 seconds (process running for 5.316)

2. 消息发送与消费

(1)通过ActiveMQ控制台发送消息

(2)服务接收到消息

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/

 :: Spring Boot ::                (v3.4.4)

2025-04-06T19:15:11.131+08:00  INFO 4132 --- [hello-activemq] [           main] c.e.h.activemq.HelloActivemqApplication  : Starting HelloActivemqApplication using Java 21.0.1 with PID 4132 (E:\hello-world\hello-activemq\target\classes started by SongGuanxun in E:\hello-world\hello-activemq)
2025-04-06T19:15:11.137+08:00  INFO 4132 --- [hello-activemq] [           main] c.e.h.activemq.HelloActivemqApplication  : No active profile set, falling back to 1 default profile: "default"
2025-04-06T19:15:13.448+08:00  INFO 4132 --- [hello-activemq] [           main] c.e.h.activemq.HelloActivemqApplication  : Started HelloActivemqApplication in 3.861 seconds (process running for 5.316)
2025-04-06T19:26:28.000+08:00  INFO 4132 --- [hello-activemq] [ntContainer#0-1] c.e.h.activemq.consumer.QueueConsumer    : 队列消费者,接收消息: {
	"name": "张三"
}

3. ActiveMQ控制台监控验证

通过ActiveMQ控制台查看消息的消费情况

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

宋冠巡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值