2

Can we call basicGet to get message from RabbitMQ, then process it asynchronously and call basicAck in another thread?

Eugene To
  • 1,890
  • 2
  • 19
  • 30

2 Answers2

0

Yes you can and it is also a common practise.

Gabriele Santomaggio
  • 21,656
  • 4
  • 52
  • 52
  • Am I right that I can't use channel.basicConsume("queueName", false, consumer); and send ack in thread different to the consumer thread? I've read everywhere that channel represends a Unit-of-Work and "it is highly recommended to use a different channel for each thread" – Eugene To Dec 21 '15 at 06:11
  • People don't write much about basicGet. In your book you are only saying that it can be used to peek the queue. Could you tell in what other cases it's worth to use basicGet? – Eugene To Dec 21 '15 at 06:21
  • 1
    To clarify, we're talking about threads, not channels, right? I think if the question said "channel" instead of "thread", the answer might be different? – Ryan Hoegg Dec 21 '15 at 16:10
  • Yes, we are talking about Threads. I can explain my case. I want to read messages from a queue and then process them asynchronously in my own thread pool, consumer will send messages further via async http client. So I'll get response in another thread (not the same thread that I used to read the message from the channel). And after I get async http reply I want to send Ack. What I'm afraid is that it can lead to some unpredictable errors, because everybody insists on using Channels binded to one constant thread. – Eugene To Dec 23 '15 at 05:55
  • 1
    @EugeneTo In your case it is better to use `basic_consume`. you case use the same channel to send the ack between the threads. please read here: http://stackoverflow.com/questions/30695375/rabbitmq-and-channels-java-thread-safety – Gabriele Santomaggio Dec 23 '15 at 07:42
  • this doesn't work well (sometimes some racing conditions makes connection to be closed) – dimzon Aug 08 '18 at 13:35
  • @dimzon no, it works. The connection is closed for some bug in the code or problem in the network, not for the basicAck. – Gabriele Santomaggio Aug 09 '18 at 17:12
0

Yes you can but you have to pass the channel reference, otherwise it won't work

Louis F.
  • 2,018
  • 19
  • 22