How to see Dequeued messages in ActiveMQ

半城伤御伤魂 提交于 2019-12-06 19:15:28

Once broker get acknowledgment from consumer, it removes message from persistence store [KahaDB/database as per configuration] of broker.

Hence if you have sent all messages to another queue or broker from your queue, you can resend those messages to your original queue. However all depends what you did with messages. If you have consumed it using MDB/java code etc, you will not be able to place them again to original queue.

Dequeued messages are not designed can be seen in activemq, you need your own logic to save them elsewhere.

ActivemMQ also give some functions to make that easier, like Mirrored Queues(http://activemq.apache.org/mirrored-queues.html), or the log plugin.

But still you need to save messages elsewhere and backup them by yourself.

to backup enqueued messages when you need the body of messages : you can add this to your activemq.xml this will save a copy of messages to a file under ${activemq.base}/bin/data/activemq/ a file by day and queue

<bean  id="ActiveMQVMConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
    <property name="brokerURL" value="vm://localhost?create=false&amp;waitForStart=10000"/>
    <property name="userName" value="${activemq.username}"/>
    <property name="password" value="${activemq.password}"/>
</bean>
<bean id="activemq" class="org.apache.activemq.camel.component.ActiveMQComponent" >
    <property name="connectionFactory" ref="ActiveMQVMConnectionFactory"/>
</bean>


<camelContext id="camel" trace="false" xmlns="http://camel.apache.org/schema/spring">
    <route>
        <from uri="activemq:queue:*?mapJmsMessage=false&amp;selector=CamelFileNameProduced IS NULL" />
        <choice>
            <when>
                <simple>${in.headers.CamelFileNameProduced} == null</simple>
                <setHeader headerName="CamelJmsDestinationName">
                    <simple>${in.header.JMSDestination.physicalName}</simple>
                </setHeader>
                <transform>
                    <simple>${in.body}\n</simple>
                </transform> 
                <to uri="file://data/activemq/?fileExist=Append&amp;fileName=routeMessages-${in.header.JMSDestination.physicalName}-${date:now:yyyyMMdd}.txt" />
                <to uri="activemq:dummy" />
            </when>
        </choice>
    </route> 
</camelContext>

If you only need metadata :

        Destination advisoryDestination = session.createTopic("ActiveMQ.Advisory.MessageConsumed.>");
        MessageConsumer consumer = session.createConsumer(advisoryDestination);
        consumer.setMessageListener(new MessageListener() {
            @Override
            public void onMessage(Message msg) {
                System.out.println(msg);
                System.out.println(((ActiveMQMessage) msg).getMessageId());
            }
        });

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!