Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

multithreading - RabbitMQ and channels Java thread safety

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

Channels and Concurrency Considerations (Thread Safety)

Channel instances must not be shared between threads. Applications should prefer using a Channel per thread instead of sharing the same Channel across multiple threads. While some operations on channels are safe to invoke concurrently, some are not and will result in incorrect frame interleaving on the wire. Sharing channels between threads will also interfere with * Publisher Confirms.

Thread safety is very important so I tried to be as diligent as possible, but here's the problem:

I have this application that receives messages from Rabbit. When a message is received, it processes it and then acks when it's done. The application can process just 2 items at the same time in a fixed thread pool with 2 threads. The QOS prefetch for Rabbit is set to 2, because I don't want to feed the app with more than it can handle in a time frame.

Now, my consumer's handleDelivery does the following:

Task run = new Task(JSON.parse(message));    
service.execute(new TestWrapperThread(getChannel(),run,envelope.getDeliveryTag()));

At this point, you already figured out that TestWrapperThread does the channel.basicAck(deliveryTag, false); call as last operation.

By my understanding of the documentation, this is incorrect and potentially harmful because channel is not thread safe and this behavior could screw things up. But how I am supposed to do then? I mean, I have a few ideas but they would def make everything more complex and I'd like to figure it out if it's really necessary or not.

Thanks in advance

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...