RabbitMQ and channels Java thread safety

后端 未结 1 557
无人及你
无人及你 2020-12-17 15:37

in this guide https://www.rabbitmq.com/api-guide.html RabbitMQ guys state:

Channels and Concurrency Considerations (Thread Safety)

Channel instanc

相关标签:
1条回答
  • 2020-12-17 16:24

    I suppose you are using Channel only for your consumer and not for other operations like publish etc..

    In your case the only potential problem is here:

    channel.basicAck(deliveryTag, false);
    

    because you call this across two thread, btw this operation is safe, if you see the java code:

    the class ChannelN.java calls:

    public void basicAck(long deliveryTag, boolean multiple)
       throws IOException
    {
       transmit(new Basic.Ack(deliveryTag, multiple));
    }
    

    see github code for ChannelN.java

    the transmit method inside AMQChannel uses:

    public void transmit(Method m) throws IOException {
       synchronized (_channelMutex) {
           transmit(new AMQCommand(m));
       }
    }
    

    _channelMutex is a protected final Object _channelMutex = new Object();

    created with the class. see github code for AMQChannel.java

    EDIT

    As you can read on the official documentation, "some" operations are thread-safe, now it is not clear which ones. I studied the code, an I think there are not problems to call the ACK across more threads.

    Hope it helps.

    EDIT2 I add also Nicolas's comment:

    Note that consuming (basicConsume) and acking from more than one thread is a common rabbitmq pattern that is already used by the java client.

    So you can use it safe.

    0 讨论(0)
提交回复
热议问题