要在上面添加埃里克森的答案:
这是获取和浏览 JMS 队列的示例:( 使用 javax.jms-api 2.x)
// Set up the connection to the queue:
Properties env = new Properties();
env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
env.put(Context.PROVIDER_URL, "http-remoting://<host>:<port>");
Context namingContext = new InitialContext(env);
ConnectionFactory connectionFactory = (ConnectionFactory) namingContext.lookup("jms/RemoteConnectionFactory");
JMSContext context = connectionFactory.createContext("jms_user", "pwd");
// Get the JMS Queue:
Queue queue = (Queue) namingContext.lookup("jms/queue/exampleQueue");
// Create the JMS Browser:
QueueBrowser browser = context.createBrowser(queue);
// Browse the messages:
Enumeration<Message> e = browser.getEnumeration();
while (e.hasMoreElements()) {
Message message = (Message) e.nextElement();
log.debug(message.getBody(String.class) + " with priority: " + message.getJMSPriority());
}
...
确保使用这些 Maven 依赖项:
<dependency>
<groupId>javax.jms</groupId>
<artifactId>javax.jms-api</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>org.wildfly</groupId>
<artifactId>wildfly-jms-client-bom</artifactId>
<version>10.0.0.Final</version>
<type>pom</type>
</dependency>