从activeMQ获取所有队列
问题描述:
我是 activeMQ 的新手.我需要编写代码以获取所有队列并阅读消息. 我没有找到任何获得所有队列的API. 如何从 ActiveMQ 读取队列.如果可能,一些示例会有所帮助.
I am new to activeMQ. I need to write code to get all the Queues and read the messages. I did not find any API like get all Queues. How can I read the Queues from ActiveMQ.If possible some example will be helpful.
答
在Java中获取ActiveMQ中的所有队列.
Get all Queues in ActiveMQ in java.
在 pom.xml
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-spring</artifactId>
<version>5.14.0</version>
</dependency>
您可以将正在运行activemq服务的 tcp://localhost:61616/更改为 tcp://180.50.50.10:61616/.
You can change tcp://localhost:61616/ to tcp://180.50.50.10:61616/ where activemq service is running.
Java代码:
try {
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616/");
ActiveMQConnection connection = (ActiveMQConnection) connectionFactory.createConnection();
DestinationSource ds = connection.getDestinationSource();
connection.start();
Set<ActiveMQQueue> queues = ds.getQueues();
for (ActiveMQQueue activeMQQueue : queues) {
try {
System.out.println(activeMQQueue.getQueueName());
} catch (JMSException e) {
e.printStackTrace();
}
}
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
控制台输出:
HtmlQueue
emaildewsgmc
pdfdirectinpirepscli
pdfdirectinpirecli
InQueue
ReceiveQueue
NormalPriorityQueue
emaildirecthp
pdfdewsgmc
pdfdirecthp
Send2Recv
SaveQueue
LowPriorityQueue
emaildewshp
HighPriorityQueue
PdfQueue
AnotherDest
pdfdewshp
emaildirectgmc