hornetq是将jms的核心组件以jndi的形式发布出来,因此客户端在调用时通过jndi查找对应的服务即可
以下步骤描述的是创建一个queue,一个生产者往queue里放消息,一个消费者从里面取,在开始前必须在hornetq服务器端添加该queue,我们的queue的名字叫exampleQueue
找到hornetq-2.2.14.Final/config/stand-alone/non-clustered/hornetq-jms.xml文件,添加一下配置
<queue name="exampleQueue"><!--这里表示该queue的名字叫exmapleQueue -->
<entry name="/queue/exampleQueue"/><!--表示该queue的JNDI名字 -->
</queue>
一个基于jndi的jms收发基本分为一下几个步骤
1.初始化jndi链接参数InitialContext
需要设置jndi的链接地址,链接的工厂
Properties properties = new Properties();
properties.put("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
properties.put("java.naming.factory.url.pkgs","org.jboss.naming:org.jnp.interfaces");
properties.put("java.naming.provider.url", "jnp://localhost:1099");
InitialContext initialContext = new InitialContext(properties);
2.获取ConnectionFactory链接工厂对象
ConnectionFactory connectionFactory = (ConnectionFactory) initialContext.lookup("/ConnectionFactory");
3.获取链接对象
connection = connectionFactory.createConnection();
4.获取Queue/Topic对象
Queue queue = (Queue)initialContext.lookup("/queue/exampleQueue");
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);//打开一个session
5.创建消息的生产者
MessageProducer producer = session.createProducer(queue)
6.创建消息并发送
TextMessage message = session.createTextMessage("Kevin JMS");//这里发送一个普通的文本消息
producer.send(message);//利用生产者发送出去
7.创建消费者接受消息
MessageConsumer messageConsumer = session.createConsumer(queue);
connection.start();
TextMessage recivedMessage = (TextMessage)messageConsumer.receive(5000);
System.out.println("Recived message:"+recivedMessage.getText());
8.关闭链接
initialContext.close();
connection.close();
这样一个jms的消息发送接受就完成了,看一下我们完整的例子,这里我们设置了一个command基类,用于将通用的链接jndi,查找链接工厂,释放资源等操作都封装起来了,这样创建不同的测试时,只需要重写具体的逻辑即可
基类
package com.crazycoder2010.hornetq.jms; import java.util.Properties; import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.naming.InitialContext; import javax.naming.NamingException; public abstract class HornetQCommand { protected InitialContext initialContext; protected Connection connection; protected InitialContext createInitialContext() throws NamingException { InitialContext initialContext = new InitialContext(createJndiProperties()); return initialContext; } protected Properties createJndiProperties() { Properties properties = new Properties(); properties.put("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory"); properties.put("java.naming.factory.url.pkgs","org.jboss.naming:org.jnp.interfaces"); properties.put("java.naming.provider.url", "jnp://localhost:1099"); return properties; } public void run() throws Exception { try { initialContext = createInitialContext(); ConnectionFactory connectionFactory = (ConnectionFactory) initialContext.lookup("/ConnectionFactory"); connection = connectionFactory.createConnection(); execute(); } finally { if(initialContext != null){ initialContext.close(); } if(connection != null){ connection.close(); } } } public abstract void execute() throws Exception; }一个测试topic的小例子
package com.crazycoder2010.hornetq.jms; import javax.jms.MessageConsumer; import javax.jms.MessageProducer; import javax.jms.Queue; import javax.jms.Session; import javax.jms.TextMessage; public class QueueExample extends HornetQCommand { @Override public void execute() throws Exception{ Queue queue = (Queue)initialContext.lookup("/queue/exampleQueue"); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageProducer producer = session.createProducer(queue); TextMessage message = session.createTextMessage("Kevin JMS"); System.out.println("Sent message:"+message.getText()); producer.send(message); MessageConsumer messageConsumer = session.createConsumer(queue); connection.start(); TextMessage recivedMessage = (TextMessage)messageConsumer.receive(5000); System.out.println("Recived message:"+recivedMessage.getText()); } }junit测试程序这里为了简化重复代码,我们对测试类也做了封装处理-因为所有的类都是继承自Command,因此我们只需要传入类明来简化测试
package com.crazycoder2010.hornetq.jms; import org.junit.Test; public class JmsTestCase { @Test public void testRun()throws Exception{ //runTest(QueueExample.class); runTest(TopicExample.class); //runTest(QueueBrowserExample.class); //runTest(MessagePriorityExample.class); //runTest(OrderQueueExample.class); } private void runTest(Class<? extends HornetQCommand> clazz)throws Exception{ System.out.println("----------"+clazz.getSimpleName()+"----------"); HornetQCommand command = clazz.newInstance(); command.run(); } }运行测试用例后控制台输出:
----------QueueExample---------- Sent message:Kevin JMS Recived message:Kevin JMS