java线程和mq,RabbitMQ和渠道Java线程安全

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

解决方案

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));

}

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();

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值