Spring AMQP 中交换器和队列的声明

发布于 2024-09-26 19:23:02 字数 393 浏览 0 评论 0原文

我正在使用 RabbitMQ 并尝试重构我当前的本机 java 实现以使用 Spring AMQP 抽象。

使用 Spring 库声明交换器、队列及其绑定是通过 AMQPAdmin 接口进行的,但我不确定何时应该进行这种配置。

我有一个使用 Rabbit 来生成消息的 Web 应用程序。另一个使用这些消息的应用程序。令人震惊:)

但是什么时候显示交换/队列的声明发生?

我是否可以将 AMQPAdmin 与 Web 应用程序一起部署,并在生产者和消费者的构造函数中进行交换/队列管理?

这些事情的声明是一次性的,破产者不需要再次了解它们,因此任何代码在后续执行中都将是 NOOP。

我是否需要创建一个单独的应用程序来管理经纪商?

目前的想法或最佳实践是什么?

I'm using RabbitMQ and trying to refactor my current native java implementation to using the Spring AMQP abstraction.

Declaration of exchanges, queues and their binding using the Spring library is via the AMQPAdmin interface, but I'm not sure when this sort of configuration should happen.

I have a web application that uses Rabbit to produce messages. And another app that consumes these messages. Shocker :)

But when show the declaration of the exchanges/queues take place?

Do I deploy the AMQPAdmin with the web applications and do exchange/queue administration within constructors of producers and consumers?

Declaration of these things are a one off, the broke doesn't need to know about them again, so any code would be a NOOP on subsequent executions.

Do I create a separate application for administration of the broker?

What is the current thinking or best practices here?

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

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

发布评论

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

评论(1

感情废物 2024-10-03 19:23:02

看起来很少有人使用 Spring 的 AMQP M1 版本,所以我将用我所做的来回答我自己的问题。

在生产者的构造函数中,我声明了交换。然后在 RabbitTemplate 上设置交换。我还将 RabbitTemplate 上的路由键设置为队列名称,但这不是必需的,但它是我将使用的路由。

@Service("userService")
public class UserService {
    private final RabbitTemplate rabbitTemplate;

    @Autowired
    public UserService(final RabbitAdmin rabbitAdmin,
                       final Exchange exchange,
                       final Queue queue,
                       @Qualifier("appRabbitTemplate") final RabbitTemplate rabbitTemplate) {

        this.rabbitTemplate = rabbitTemplate;

        rabbitAdmin.declareExchange(exchange);
        rabbitTemplate.setExchange(exchange.getName());
        rabbitTemplate.setRoutingKey(queue.getName());  
    }


    public void createAccount(final UserAccount userAccount) {
        rabbitTemplate.convertAndSend("Hello message sent at " + new DateTime());
    }
}

在消费者的构造函数中,我声明队列并创建绑定。

public class Consumer implements ChannelAwareMessageListener<Message> {

    public Consumer(final RabbitAdmin rabbitAdmin, final Exchange exchange, final Queue queue) {
        rabbitAdmin.declareQueue(queue);

        rabbitAdmin.declareBinding(BindingBuilder.from(queue).to((DirectExchange) exchange).withQueueName());
    }

    @Override
    public void onMessage(Message message, Channel channel) throws Exception {

        System.out.println(new String(message.getBody()));

        channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
    }
}

尽管构造函数可能会运行多次,但 RabbitMQ 仅声明交换、队列和绑定一次。

如果您需要这个小示例项目的完整源代码,请询问,我会将其放在某个地方为您提供。

It would appear that very few people are using Spring's AMQP M1 release, so I will answer my own question with what I've done.

In the producer's constructor I declare the exchange. Then set the exchange on the RabbitTemplate. I also set the routing key on the RabbitTemplate as the queue name, but that isn't required, but it was the route I would be using.

@Service("userService")
public class UserService {
    private final RabbitTemplate rabbitTemplate;

    @Autowired
    public UserService(final RabbitAdmin rabbitAdmin,
                       final Exchange exchange,
                       final Queue queue,
                       @Qualifier("appRabbitTemplate") final RabbitTemplate rabbitTemplate) {

        this.rabbitTemplate = rabbitTemplate;

        rabbitAdmin.declareExchange(exchange);
        rabbitTemplate.setExchange(exchange.getName());
        rabbitTemplate.setRoutingKey(queue.getName());  
    }


    public void createAccount(final UserAccount userAccount) {
        rabbitTemplate.convertAndSend("Hello message sent at " + new DateTime());
    }
}

In the consumer's constructor I declare the queue and create the binding.

public class Consumer implements ChannelAwareMessageListener<Message> {

    public Consumer(final RabbitAdmin rabbitAdmin, final Exchange exchange, final Queue queue) {
        rabbitAdmin.declareQueue(queue);

        rabbitAdmin.declareBinding(BindingBuilder.from(queue).to((DirectExchange) exchange).withQueueName());
    }

    @Override
    public void onMessage(Message message, Channel channel) throws Exception {

        System.out.println(new String(message.getBody()));

        channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
    }
}

Although the constructors may be run many times, RabbitMQ only declares the exchange, queue and bindings once.

If you need the whole source for this little example project, ask, and I'll put it up somewhere for you.

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