2

RabbitMQ amqp-client 库是否可以取消一个nextDelivery?我有一个已经调用过的线程,consumer.nextDelivery()这个调用正在等待队列的响应。我想要的是从另一个线程中阻止它。channel.basicCancel只有在当前调用返回后才会取消nextDelivery,这不是我想要的,因为应用程序处于无法对接收到的交付执行任何操作的状态。

我一直在寻找解决这个问题的方法,现在我认为我对 RabbitMQ 有一些误解,因为我没有得到与我的问题相匹配的搜索结果。

4

2 回答 2

1

What I want is to stop this from another thread

Personal advice: don't do it.

From the information gathered in the comments, it appears that you are separating the message receival from its treatment. My suggestion is to merge both; and use an ExecutorService to wrap them both.

When you need to shut the whole thing down, call .shutDown() on the ExecutorService: it means that no other task can be submitted to that execution.

As the receival/treatment process is now merged, it means you can never have a state in which a message is received but cannot be treated anymore.

于 2013-07-07T12:16:34.950 回答
0

我不确定这是否可行,但尝试shutdownNow()使用ExecutorService

private ExecutorService executor = Executors.newSingleThreadExecutor();
executor.submit(new Runnable()
{
    @Override
    public void run()
    {
        // ...
        Message m = consumer.nextDelivery();
        // ...
    }
});
executor.shutdownNow();

ShutdownNow() 的 javadoc状态:

尝试停止所有正在执行的任务,停止等待任务的处理,并返回等待执行的任务列表。此方法不等待主动执行的任务终止。使用 awaitTermination 来做到这一点。

除了尽最大努力停止处理正在执行的任务之外,没有任何保证。例如,典型的实现将通过 Thread.interrupt() 取消,因此任何未能响应中断的任务可能永远不会终止。

于 2013-07-07T10:37:58.160 回答