I am trying to consume a message from a RabbitMQ channel using Java code. I invoke the myAction() method two times, and it works as expected only on the first one.
private Channel receiveChannel=//...;
String myAction() {
var wrapper = new Object(){ String message = null; };
final CountDownLatch countDownLatch = new CountDownLatch(1);
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
wrapper.message = new String(delivery.getBody(), "UTF-8");
countDownLatch.countDown();
log.info("Received response '" + wrapper.message + "'");
};
receiveChannel.basicConsume(recvQueueKey, true, deliverCallback, consumerTag -> {});
if(!countDownLatch.await(10000, TimeUnit.MILLISECONDS)) {
fail();
}
return wrapper.message;
}
Basically, my goal is to consume and have myAction()
return the message content. What happens in the second time I invoke myAction()
is that log.info("Received response..
gets printed but the countDownLatch does not get decremented, causing fail()
to be executed. Does anyone have any idea what I am missing? Is line with this, is it the proper way to consume and return the message content? Thank you for your help.