Chetan Sistla Chetan Sistla - 1 year ago 156
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 Source

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.



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

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);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download