rabbitmq 以相同顺序将队列消息从一个消息移动到另一个消息

hiz5n14c  于 2022-11-29  发布在  RabbitMQ
关注(0)|答案(1)|浏览(209)

假设在我的RabbitMQ服务器上,我有一个包含5条消息的Queue1,然后,我创建一个新的Queue2,并将其绑定到一个特定的交换,但我还想从Queue1中获取消息,并将它们移动到Queue2(按照它们在Queue1中的相同顺序),然后我想删除Queue1。
有什么可行的想法和方法吗?

ulmd4ohb

ulmd4ohb1#

请参阅RabbitTemplate API:

/**
 * Invoke the callback and run all operations on the template argument in a dedicated
 * thread-bound channel and reliably close the channel afterwards.
 * @param action the call back.
 * @param <T> the return type.
 * @return the result from the
 * {@link OperationsCallback#doInRabbit(RabbitOperations operations)}.
 * @throws AmqpException if one occurs.
 * @since 2.0
 */
@Nullable
default <T> T invoke(OperationsCallback<T> action) throws AmqpException {

在其impl中,您将在循环中调用operations.receive(),直到null

/**
 * Receive a message from a specific queue, waiting up to the specified wait time if
 * necessary for a message to become available.
 *
 * @param queueName the queue to receive from
 * @param timeoutMillis how long to wait before giving up. Zero value means the method
 * will return {@code null} immediately if there is no message available. Negative
 * value makes method wait for a message indefinitely.
 * @return a message or null if the time expires
 * @throws AmqpException if there is a problem
 * @since 1.6
 */
@Nullable
Message receive(String queueName, long timeoutMillis) throws AmqpException;

从一个队列中提取消息。
然后道:

/**
 * Send a message to a default exchange with a specific routing key.
 *
 * @param routingKey the routing key
 * @param message a message to send
 * @throws AmqpException if there is a problem
 */
void send(String routingKey, Message message) throws AmqpException;

若要发布到另一个队列,请执行以下操作。
由于您在循环(receive-n-send)中按顺序执行所有操作,因此队列抽象定义保证了消息的顺序。

相关问题