You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@qpid.apache.org by Aleš Trček <Al...@halcom.si> on 2013/04/02 17:55:50 UTC

Java JMS client eating InterruptedException

Hi all!

I am implementing proper stopping (with ExecutorService.shutdownNow()) of a few worker threads I have, that are consuming messages from the queue all the time, and if there were no messages the threads wouldn't stop... Thread.currentThread().isInterrupted() is always false. So after a little bit of debugging I discovered that the problem lies in the fact that receive method on MessageConsumer eats up the InterruptedException, which clears isInterrupted flag.
Relevant code from BasicMessageConsumer.java:
public Message receive(long l) throws JMSException
{
...
    catch (InterruptedException e)
    {
        _logger.warn("Interrupted acquire: " + e);
        if (isClosed())
        {
            return null;
        }
    }
...
    catch (InterruptedException e)
    {
        _logger.warn("Interrupted: " + e);

        return null;
    }
...
}

Even though the JMS standard does not specify how to handle such interrupt (JMSException or returning null message), I'm sure the implementation shouldn't just ignore it (which is basically what happens). When catching InterruptedException, the interrupted flag should immediately be set back (Thread.currentThread().interrupt()), so in the calling code we can still detect such situation.
Any other point of view on this subject, or should I file a bug? :)

Btw: I got around this by not using timeout in receive() call, but I would be more comfortable if the Consumer would wait for messages for a while, instead of me looping like crazy in my code...

P.S.: Sorry if this should go to qpid-dev.


Regards,
Ales

Re: Java JMS client eating InterruptedException

Posted by Robbie Gemmell <ro...@gmail.com>.
Hi Ales,

It seems reasonable to raise a JIRA

Robbie


On 2 April 2013 16:55, Aleš Trček <Al...@halcom.si> wrote:

> Hi all!
>
> I am implementing proper stopping (with ExecutorService.shutdownNow()) of
> a few worker threads I have, that are consuming messages from the queue all
> the time, and if there were no messages the threads wouldn't stop...
> Thread.currentThread().isInterrupted() is always false. So after a little
> bit of debugging I discovered that the problem lies in the fact that
> receive method on MessageConsumer eats up the InterruptedException, which
> clears isInterrupted flag.
> Relevant code from BasicMessageConsumer.java:
> public Message receive(long l) throws JMSException
> {
> ...
>     catch (InterruptedException e)
>     {
>         _logger.warn("Interrupted acquire: " + e);
>         if (isClosed())
>         {
>             return null;
>         }
>     }
> ...
>     catch (InterruptedException e)
>     {
>         _logger.warn("Interrupted: " + e);
>
>         return null;
>     }
> ...
> }
>
> Even though the JMS standard does not specify how to handle such interrupt
> (JMSException or returning null message), I'm sure the implementation
> shouldn't just ignore it (which is basically what happens). When catching
> InterruptedException, the interrupted flag should immediately be set back
> (Thread.currentThread().interrupt()), so in the calling code we can still
> detect such situation.
> Any other point of view on this subject, or should I file a bug? :)
>
> Btw: I got around this by not using timeout in receive() call, but I would
> be more comfortable if the Consumer would wait for messages for a while,
> instead of me looping like crazy in my code...
>
> P.S.: Sorry if this should go to qpid-dev.
>
>
> Regards,
> Ales
>