Chetan Sistla Chetan Sistla - 2 months ago 36
Java Question

Iterate over non-persistent activemq expired messages in ActiveMQ.Advisory.Expired.Queue

Iam building an application on activemq where iam sending a message from producer where my delivery mode is NON_PERSISTENT (iam not dealing with PERSISTENT delivery mode and i know it will be stored in DLQ-which is not my design)and have set a time to live for the message using producer.setTimeToLive(2000).As the functionality says the message will expire in 2 seconds.

I see that the expired messages are enqueued in the ActiveMQ.Advisory.Expired.Queue in the topic section of activeMQ admin console i.e. http://localhost:8161/admin/topics.jsp.

My question is how do i iterate over the ActiveMQ.Advisory.Expired.Queue so that i can access the MessageID of the expired message.Any code example would be great.

Answer

A subscription to the destination ActiveMQ.Advisory.Expired.Queue is like any topic and it returns an ActiveMQMessage. There is DataStructure objects (ConsumerInfo, ProducerInfo,ConnectionInfo...) can be retrieve via getDataStructure method of ActiveMQMessage.

doc http://activemq.apache.org/advisory-message.html

example:

Destination advisoryDestination = AdvisorySupport.getExpiredQueueMessageAdvisoryTopic(destination)
MessageConsumer consumer = session.createConsumer(advisoryDestination);
consumer.setMessageListener(this);

public void onMessage(Message msg){
    String messageId =   msg.getJMSMessageID();
    String orignalMessageId =   msg.getStringProperty(org.apache.activemq.advisory.AdvisorySupport.MSG_PROPERTY_MESSAGE_ID);
    if (msg instanceof ActiveMQMessage){
        try {
             ActiveMQMessage aMsg =  (ActiveMQMessage)msg;
             ProducerInfo prod = (ProducerInfo) aMsg.getDataStructure();
        } catch (JMSException e) {
            log.error("Failed to process message: " + msg);
        }
    }
}
Comments