You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用RabbitMQ tut1教程时,Sender无法输出消息且收发无通信

问题:Spring AMQP RabbitMQ Tut1教程中Sender与Receiver无通信、无终端输出

我正在学习RabbitMQ官方的Spring AMQP tut1教程,启动Sender和Receiver后,两者之间无通信,终端没有任何输出。RabbitMQ服务已正常运行,但RabbitMQ UI中也无相关记录。

启动命令

  • Sender:java -Dserver.port=8889 -jar target/rabbitmq4-0.0.1-SNAPSHOT.jar --spring.profiles.active=hello-world,sender
  • Receiver:java -Dserver.port=8888 -jar target/rabbitmq4-0.0.1-SNAPSHOT.jar --spring.profiles.active=hello-world,receiver

启动后程序显示就绪且激活了对应Profile,但无任何输出。我怀疑问题和@Profile注解有关,在Tut1Config的Sender Bean方法中添加了打印日志,但未看到输出。

相关代码

Tut1Config.java

package com.example.rabbitmq4.tut1;

import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.scheduling.annotation.EnableScheduling;

@Profile({"tut1","hello-world"})
@Configuration
public class Tut1Config {

    @Bean
    public Queue hello(){
        return new Queue("hello");
    }

    @Profile("receiver")
    @Bean
    public Tut1Receiver receiver(){
        return new Tut1Receiver();
    }

    @Profile("sender")
    @Bean
    public Tut1Sender sender(){
        System.out.println("this shit working for sender?");
        return new Tut1Sender();
    }
}

RabbitAmqpTutorialsRunner.java

package com.example.rabbitmq4;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ConfigurableApplicationContext;

public class RabbitAmqpTutorialsRunner implements CommandLineRunner {

    @Value("${tutorial.client.duration:0}")
    private int duration;

    @Autowired
    private ConfigurableApplicationContext ctx;

    @Override
    public void run(String... arg0) throws Exception {
        System.out.println("Ready ... running for " + duration + "ms");
        Thread.sleep(duration);
        ctx.close();
    }
}

Tut1Sender.java

import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

public class Tut1Sender {
    @Autowired
    private RabbitTemplate template;

    @Autowired
    private Queue queue;

    @Scheduled(fixedDelay = 1000, initialDelay = 500)
    public void send() {
        String message = "Hello World!";
        this.template.convertAndSend(queue.getName(), message);
        System.out.println(" [x] Sent '" + message + "'");
    }
}

问题排查与解决思路

  1. 核心问题:程序启动后立即退出
    RabbitAmqpTutorialsRunner中duration默认值为0,启动后会立刻执行Thread.sleep(0)然后关闭Spring上下文,导致定时任务(@Scheduled)根本没有机会执行。必须设置足够长的运行时长,让程序有时间处理消息。

  2. 修复步骤
    修改启动命令,添加tutorial.client.duration参数指定运行时长:

    • Sender启动命令:java -Dserver.port=8889 -jar target/rabbitmq4-0.0.1-SNAPSHOT.jar --spring.profiles.active=hello-world,sender --tutorial.client.duration=10000
    • Receiver启动命令:java -Dserver.port=8888 -jar target/rabbitmq4-0.0.1-SNAPSHOT.jar --spring.profiles.active=hello-world,receiver --tutorial.client.duration=10000
      (上述命令设置程序运行10秒后退出,可根据需求调整时长)
  3. 额外验证点

    • 确认Tut1Sender的@Component注解生效:虽然通过@Bean注册了实例,但@Scheduled需要Spring正确识别该Bean的定时任务配置,可检查类上是否保留@Component注解。
    • 开启Spring上下文调试日志:添加--logging.level.org.springframework.context.annotation=DEBUG到启动命令,验证Profile是否正确加载了对应的Bean。

内容的提问来源于stack exchange,提问作者Luminati

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 04:45:14