Bu örnekte ActiveMQ Armetis'de kuyrukta bulunan mesajları bilgi amaçlı alıyoruz
ActiveMQ Artmesin için aşağıdaki Maven tanımı eklenir:
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>artemis-jms-client</artifactId>
<version>2.6.3</version>
</dependency>
Eklenen mesajlar aşağıdaki uygulama ile listenelebilir. Bu kod çalıştığında kuyruktaki mesajlar silinmez ve sadece bilgi amaçlı gösterilir:
package com.thy.mercury.activemq.client;
import java.util.Enumeration;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.QueueBrowser;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory;
import org.apache.activemq.artemis.jms.client.ActiveMQQueue;
public class ArtemisMonitor {
public static void main(String[] args) {
Connection connection = null;
Session session = null;
try {
ConnectionFactory cf =
new ActiveMQConnectionFactory("tcp://activemqtest.fibiler.com:61616");
connection = cf.createConnection();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue activeMQQueue = new ActiveMQQueue("queue.FIBILER::TEST");
QueueBrowser browser = session.createBrowser(activeMQQueue);
Enumeration e = browser.getEnumeration();
while (e.hasMoreElements()) {
TextMessage message = (TextMessage) e.nextElement();
System.out.println("Browse [" + message.getText() + "]");
}
browser.close();
} catch (JMSException e) {
e.printStackTrace();
} finally {
if (session != null) {
try {
session.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
}
}
Factory olarak ActiveMQConnectionFactory sınıfını kullanıyoruz. Connection ve Session yaratma JMS standarlarına göre yapılır. Kuyruk olarak ActiveMQQueue sınıfı kullanılmalıdır. Daha sonra QueueBrowser nesnesi yaratılır ve içindeki tüm mesajlar listelenir. Örneğin aşağıdaki gibi bir çıktı olabilir:
Browse [Test 3]
Browse [Test 7]
Browse [Test 11]
Browse [Test 15]
Browse [Test 19]