Spring AMQP:MessageListener 未收到任何消息

Spring AMQP : MessageListener not receiving any messages

目前我正在 Spring AMQP 项目之上构建自定义库。我已经到了想要实现消息侦听器以便能够异步接收消息的地步。在进一步阅读项目指定的文档后,我发现实现自己的消息监听器应该很容易。只需实施 MessageListener class 并将其配置为在传入消息时触发。

这就是我所做的:

public class ReceiveController implements MessageListener
{
    @Override
    public void onMessage(Message message)
    {
        System.out.println("Received a message!");
    }
}

接下来我是这样配置的:

private SimpleMessageListenerContainer configureMessageListener()
{
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
    container.setConnectionFactory(connection);
    container.setQueueNames("test.queue");
    container.setMessageListener(this);
    return container;
}

这两段代码位于同一个 class 中,名为 'ReceiveController'。

Hence the fact that I'm not using any context (annotations or xml). I'm not sure if this is mandatory for the project to function as I can just create instances of the classes myself.

当 运行 一些代码使用了我的库时

由于某种原因,消费者没有通过它的侦听器接收到任何消息。这可能与队列是使用 'amq.direct' 交换和 'test.route' 路由键创建的事实有关吗?还是其他原因?

手动构建容器时(在 Spring 应用程序上下文之外),您需要调用 afterPropertiesSet()start().

此外,如果您的侦听器实现了 MessageListenerChannelAwareMessageListener,则您不需要适配器。