Reputation: 1716
I am looking for an optimal way to browse/check the last message inserted in a particular queue.
So far I am thinking of ActiveMQQueueBrowser
. See the code below:
ActiveMQQueueBrowser browser = (ActiveMQQueueBrowser) session.createBrowser(queue);
Enumeration e = browser.getEnumeration();
TextMessage message = null;
while (e.hasMoreElements()) {
message = (TextMessage) e.nextElement();
}
But this code will need to traverse all the messages in queue, untill it gives last message.
plz suggest if there is any other way to do so.
Upvotes: 1
Views: 1081
Reputation: 35123
There's no way to fetch the last message in the queue without traversing all the other messages with a browser (or something like a browser).
Keep in mind that a queue is not like a database table. You can't just pick out only the messages you want.
Upvotes: 0
Reputation: 3913
is JMX possible ?? see below example, you can order the messages as you want after loading them into a list.
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.management.MBeanServerConnection;
import javax.management.MBeanServerInvocationHandler;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import org.apache.activemq.broker.jmx.BrokerViewMBean;
import org.apache.activemq.broker.jmx.QueueViewMBean;
public class JMXGetDestinationInfos {
public static void main(String[] args) throws Exception {
JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://host:1099/jmxrmi");
Map<String, String[]> env = new HashMap<>();
String[] creds = { "admin", "admin" };
env.put(JMXConnector.CREDENTIALS, creds);
JMXConnector jmxc = JMXConnectorFactory.connect(url, env);
MBeanServerConnection conn = jmxc.getMBeanServerConnection();
ObjectName activeMq = new ObjectName("org.apache.activemq:type=Broker,brokerName=localhost");
BrokerViewMBean mbean = MBeanServerInvocationHandler.newProxyInstance(conn, activeMq, BrokerViewMBean.class,
true);
for (ObjectName name : mbean.getQueues()) {
if (("TEST".equals(name.getKeyProperty("destinationName")))) {
QueueViewMBean queueMbean = MBeanServerInvocationHandler.newProxyInstance(conn, name,
QueueViewMBean.class, true);
List<?> messages = queueMbean.browseMessages();
System.out.println(messages.get(messages.size() -1));
System.out.println(queueMbean.browseAsTable());
System.out.println(queueMbean.browseMessages());
}
}
}
}
another option if your counter is always available and connected is to use http://activemq.apache.org/advisory-message.html
ActiveMQ.Advisory.MessageDelivered.Queue Message delivered to the broker.
ActiveMQ.Advisory.MessageDelivered.Topic Message delivered to the broker.
Upvotes: 0