I'm using RabbitMQ which uses LinkedBlockingQueue
by default for consumer.
It has a blocking nextDelivery()
method which basically calls take()
on queue.
But it doesn't catch the interrupt if it was interrupted just before calling that method.
if (Thread.interrupted()) {
throw new InterruptedException();
}
// If interrupted here, call below will not throw interrupted exception
rabbit.nextDelivery();
It only works if interrupt hits while it is waiting -which is what is written in javadoc too- or if hits if block first.
Throws:
InterruptedException - if interrupted while waiting
I actually have a case where interrupt hits just where I marked. It works if I put a sleep to beginning or in between but it's still not safe to assume it will always work.
Is there an alternative BlockingQueue implementation addresses this issue? I don't know if interrupt it consumed or not, maybe there is a race condition in static method it returns false but clears the set value somehow?
Edit: It is not about Thread.interrupted()
call or not setting flags. If you comment out if block, it is same issue again. Queue method doesn't throw InterruptedException
as soon as it enters, it just blocks