2

In activemq I'm doing something that might be wrong. I'm creating a consumer and setting a messageListener (consumer.setMessageListener(MessageListener)) while consuming a message. This causes activeMQ to deadlock.

Here is a code sample of what I am doing:

class DriverClass extends MessageListener 
{
    Destination destination = session.createTopic("Some.Topic");
    MessageListener msgSub = new MyClass(destination);
    Session session = passedInSession;


    MessageConsumer consumer;
    try
    {        
        consumer = session.createConsumer(destination);

        msgSub.setConsumer( consumer );

        if(msgSub.getMessageListener() != null)
        {
            consumer.setMessageListener( msgSub );
        }
    }
    catch (Exception ex)
    {
        LOGGER.error( "Error subscribing to "+msgSub.getTopic()
                +" Error message: ", ex );
    }
}


class MyClass extends MessageListener
{ 
    Destination dest;
    public MyClass(Destination destToUse)
    {
        dest = destToUse;
    }

    @Override
    public void onMessage( final Message message )
    {
        //note here we are using the same topic
    MessageListener msgSub = new MyMessageListener("Some.Topic");

        MessageConsumer consumer;
        try
        {           
            //note here we are using the same dest as above
            consumer = session.createConsumer(dest);

            msgSub.setConsumer( consumer );

            if(msgSub.getMessageListener() != null)
            {
                    //this is where it deadlocks subscribing while consuming a message
                    //on the same topic/destination
                consumer.setMessageListener( msgSub );
            }
        }
        catch (Exception ex)
        {
            LOGGER.error( "Error subscribing to "+msgSub.getTopic()
                    +" Error message: ", ex );
        }
    }
}

Basically, I'm subscribing within a consumer within the ActiveMQ thread. The only reference to this problem can be found here: https://issues.apache.org/jira/browse/AMQ-336 but I can't tell what the workaround is. For now I'm just going to put the subscription call on a worker thread and return control back to ActiveMQ but it seems like there is a better solution. Is the issue that I'm subscribing to a topic/destination while handling a message of the same topic/destination?

darrickc
  • 1,872
  • 6
  • 27
  • 38
  • You need to provide more information like versions, code snippets, stack traces etc. Can't decipher your problem from the limited information given. – Tim Bish May 20 '13 at 22:51
  • Is that enough information? – darrickc May 24 '13 at 13:49
  • What you're trying to do seems pretty wacky. I don't think there's any way to work around the locking other than to spawn a thread. – Tim Bish May 25 '13 at 20:53

0 Answers0