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?