1、Maven依赖
<dependency>
<groupId>org.hornetq</groupId>
<artifactId>hornetq-jms-client</artifactId>
<version>2.4.7.Final</version>
</dependency>
<dependency>
<groupId>jboss</groupId>
<artifactId>jnp-client</artifactId>
<version>4.2.2.GA</version>
</dependency>
2、代码
package com.lynch.hornetq.demo;
import java.util.Properties;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class HornetqTextMessageDemo {
public static void main(String[] args) {
// 设置HornetQ连接信息
Properties p = new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
p.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
p.put(Context.PROVIDER_URL, "jnp://192.168.137.239:1099");
Context context = null;
ConnectionFactory factory = null;
Queue queue = null;
// 初始化连接信息,并指定连接工厂和消息队列
try {
context = new InitialContext(p);
factory = (ConnectionFactory) context.lookup("/ConnectionFactory");
queue = (Queue) context.lookup("queue/DLQ");
} catch (NamingException e) {
e.printStackTrace();
}
// 建立连接和会话,进而发送和接收消息
try (Connection connection = factory.createConnection(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);) {
connection.start();
produceMessage(session, queue, "Hello world!");
consumeMessage(session, queue);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void produceMessage(Session session, Queue queue, String text) {
try (MessageProducer producer = session.createProducer(queue)) {
TextMessage message = session.createTextMessage(text);
producer.send(message);
System.out.println("Send message:" + text);
} catch (JMSException e) {
e.printStackTrace();
}
}
public static void consumeMessage(Session session, Queue queue) {
try (MessageConsumer consumer = session.createConsumer(queue)) {
TextMessage message = (TextMessage) consumer.receive(5000);
System.out.println("Received message:" + message.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
}
网友评论