HornetQ使用手册

来源:互联网 发布:ubuntu lamp的安装 编辑:程序博客网 时间:2024/05/17 23:26

下载hornetq并解压,启动bin目录下边的run.bat启动hornetQ服务。

demo中的"springIntegration" 通过listener的方式实现了异步模式。

demo中的"topic" C端通过receive()方法得到消息,实现了同步模式.

一、下面根据这两种模式的例子做一下配置说明:

1topic  (同步):

Step1. 创建一个初始上下文执行目录

initialContext = getContext(0)

Step2. 执行一个查询的Topic

Topic topic = (Topic)initialContext.lookup("/topic/MesDemoTopic");

"/topic/MesDemoTopic":要与服务器配置的jndi一样.

Step3. 执行一个查询连接工厂

ConnectionFactory cf = (ConnectionFactory)initialContext.lookup("/ConnectionFactory");

Step4.创建一个连接以及session

connection = cf.createConnection();

Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

第一个参数是是否使用事务,第二个参数是消费者向发送者确认消息已经接收的方式

确认消息的方式有如下三种:

AUTO_ACKNOWLEDGE(自动通知)

CLIENT_ACKNOWLEDGE(客户端自行决定通知时机)

DUPS_OK_ACKNOWLEDGE(延时//批量通知)

Step5.创建一个消息的发送者producer

MessageProducer producer = session.createProducer(topic);

Step 6.创建一个Consumer

MessageConsumer messageConsumer1 = session.createConsumer(topic);

Step7.创建一个消息

TextMessage message = session.createTextMessage("This is a text message");

message还包括:

1)字节数组类型:BytesMessage

2MAP类型:MapMessage

3)多个原始数据类型:StreamMessage

4)对象类型:ObjectMessage

Step8. Send the Message

producer.send(message);

Step9. Receive the message

TextMessage messageReceived = (TextMessage)messageConsumer1.receive();

Step10.关闭相关连接与服务

if (connection != null)

         {

            connection.close();

         }

if (initialContext != null)

         {

            initialContext.close();

         }

2springIntegration(异步)模式:

首先这个例子是与spring集成的.

Step1. 根据配置文件创建bean工厂

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"spring-jms-beans.xml"});(具体配置文件请看demo

Step2.取得消息发送者MessageSender

MessageSender sender = (MessageSender)context.getBean("MessageSender");

Step3.发送一个消息

sender.send("Hello world");

这个方法中会创建连接以及SESSIONproducer,和topic的例子一样.

Step4.C端收到消息。

p端发送消息到broker的时候,C端的监听的queuetopic通道发生变化的时候,MessageListener 就会取到queuetopic中的消息。