调用consumer.setMessageListener(MessageListener)时遇到死锁

时间:2013-05-20 20:39:34

标签: java activemq

在activemq中,我正在做一些可能出错的事情。我正在创建一个使用者并在使用消息时设置messageListener(consumer.setMessageListener(MessageListener))。这会导致activeMQ死锁。

以下是我正在做的代码示例:

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 );
        }
    }
}

基本上,我在ActiveMQ线程中的消费者中订阅。可以在此处找到对此问题的唯一引用:https://issues.apache.org/jira/browse/AMQ-336但我无法确定解决方法是什么。现在我只是将订阅调用放在工作线程上并将控制权返回给ActiveMQ,但似乎有更好的解决方案。在处理同一主题/目的地的消息时,我是否正在订阅主题/目的地的问题?

0 个答案:

没有答案