Spring Boot应用不消耗队列消息

发布于 2025-01-30 05:57:54 字数 2256 浏览 2 评论 0原文

我有兔子MQ经纪人在服务之间进行异步传达。 服务正在将消息发送到队列。我检查了队列,服务A的消息到达:

我正在尝试在服务B 中创建一个听众,以消耗服务A。它似乎已成功联系在一起。

问题是服务B成功开始,但它正在接收兔子MQ的消息。

以下是侦听器的实现:

@Slf4j
@Component
public class EventListener {

    public static final String QUEUE_NAME = "events";

    @RabbitListener(
            bindings = {
                    @QueueBinding(
                            value = @Queue(QUEUE_NAME),
                            exchange = @Exchange("exchange")
                    )

            }
    )
    public void handleTaskPayload(@Payload String payload) {
        System.out.println(payload);
    }
}

我验证了兔子MQ中的队列和交换信息,它们是正确的。

一切正常工作,并且在服务A或服务B中没有错误的错误,这使此问题更难调试。

我试图从RabbitMQ的队列GetMessage中检索消息,如下所示:

{"id":"1",:"name:"Test","created":null}

感谢您对解决此问题的任何帮助或指导。

此致, 兰多。

ps

i创建了一个新的测试队列,如以下内容,并发布了一些消息:

”在此处输入图像描述”

如下所示,仍然无法触发侦听器来收听侦听器队列事件:

@Slf4j
@Component
public class RobotRunEventListener {

    public static final String QUEUE_NAME = "test";

    @RabbitListener(
            bindings = {
                    @QueueBinding(
                            value = @Queue(QUEUE_NAME),
                            key = "test",
                            exchange = @Exchange("default")
                    )

            }
    )
    public void handleTaskPayload(@Payload String payload) {
        System.out.println(payload);
    }

I have the Rabbit MQ broker for communicating asynchronously between services. Service A is sending messages to the queue. I checked the queue and the messages from Service A have arrived:
enter image description here

I am trying to create a listener in the Service B in order to consume the messages produced by Service A. I verified like below to check if Service B is connected with RabbitMQ and it seems to be connected successfully.

enter image description here

The problem is that Service B started successfully but it is receiving messages from Rabbit MQ.

Below is the implementation of the listener:

@Slf4j
@Component
public class EventListener {

    public static final String QUEUE_NAME = "events";

    @RabbitListener(
            bindings = {
                    @QueueBinding(
                            value = @Queue(QUEUE_NAME),
                            exchange = @Exchange("exchange")
                    )

            }
    )
    public void handleTaskPayload(@Payload String payload) {
        System.out.println(payload);
    }
}

I verified the queue and exchange information in the Rabbit MQ and they are correct.

enter image description here

Everything is working correctly and there is no error thrown in service A or service B which makes this problem much harder to debug.

I tried to retrieve the message from the queue getMessage of RabbitMQ the message is like the below:

{"id":"1",:"name:"Test","created":null}

I will appreciate any help or guidance towards the solution of this problem.

Best Regards,
Rando.

P.S

I created a new test queue like the below and published some messages:

enter image description here

enter image description here

Modified the listener code like below and still wasn't able to trigger listener to listen to the queue events:

@Slf4j
@Component
public class RobotRunEventListener {

    public static final String QUEUE_NAME = "test";

    @RabbitListener(
            bindings = {
                    @QueueBinding(
                            value = @Queue(QUEUE_NAME),
                            key = "test",
                            exchange = @Exchange("default")
                    )

            }
    )
    public void handleTaskPayload(@Payload String payload) {
        System.out.println(payload);
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

攒一口袋星星 2025-02-06 05:57:54

尝试这种方法:

 @RabbitListener(queues = "test")
 public void receive(String in, @Headers Map<String, Object> headers) throws IOException  {
 }

Try this approach:

 @RabbitListener(queues = "test")
 public void receive(String in, @Headers Map<String, Object> headers) throws IOException  {
 }
他不在意 2025-02-06 05:57:54

问题是我正在使用的Spring Boot应用程序具有@conditional(config.class),该应用程序阻止了下面的BEAN创建:

@Slf4j
@Conditional(Config.class)
@EnableRabbit
public class InternalRabbitBootstrapConfiguration {
    @Bean
    public RabbitListenerContainerFactory rabbitListenerContainerFactory(ConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setMaxConcurrentConsumers(5);
        return factory;
    }
...

这导致Spring Boot应用程序未听兔子MQ事件。 config.class需要一个特定的配置文件,以使应用程序能够收听兔子MQ事件。

public class DexiModeCondition implements Condition {

    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {

        String[] activeProfiles = context.getEnvironment().getActiveProfiles();

        return activeProfiles[0].equalsIgnoreCase(mode);
    }
}

The problem was that the spring boot app that I was working on had a @Conditional(Config.class) that prevented the creation of the bean below:

@Slf4j
@Conditional(Config.class)
@EnableRabbit
public class InternalRabbitBootstrapConfiguration {
    @Bean
    public RabbitListenerContainerFactory rabbitListenerContainerFactory(ConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setMaxConcurrentConsumers(5);
        return factory;
    }
...

which resulted in the spring boot app not listening to Rabbit MQ events. The Config.class required a specific profile in order to enable the app to listen to Rabbit MQ events.

public class DexiModeCondition implements Condition {

    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {

        String[] activeProfiles = context.getEnvironment().getActiveProfiles();

        return activeProfiles[0].equalsIgnoreCase(mode);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文