`
holdbelief
  • 浏览: 696120 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Apache ActiveMQ教程(二)

    博客分类:
  • JMS
阅读更多

Apache ActiveMQ教程(二)
2008-06-03 14:25

五、Point-to-Point (点对点)消息模式开发流程

1、生产者(producer)开发流程(ProducerTool.java):

1.1 创建Connection

根据urluserpassword创建一个jms Connection

1.2 创建Session

connection的基础上创建一个session,同时设置是否支持事务和ACKNOWLEDGE标识。

1.3 创建Destination对象:

需指定其对应的主题(subject)名称,producerconsumer将根据subject来发送/接收对应的消息。

1.4 创建MessageProducer

根据Destination创建MessageProducer对象,同时设置其持久模式。

1.5 发送消息到队列(Queue):

封装TextMessage消息,使用MessageProducersend方法将消息发送出去。

2、消费者(consumer)开发流程(ConsumerTool.java):

2.1 实现MessageListener接口:

消费者类必须实现MessageListener接口,然后在onMessage()方法中监听消息的到达并处理。

2.2 创建Connection

根据urluserpassword创建一个jms Connection,如果是durable模式,还需要给connection设置一个clientId

2.3 创建SessionDestination

ProducerTool.java中的流程类似,不再赘述。

2.4创建replyProducer【可选】:

可以用来将消息处理结果发送给producer

2.5 创建MessageConsumer

根据Destination创建MessageConsumer对象。

2.6 消费message

       onMessage()方法中接收producer发送过来的消息进行处理,并可以通过replyProducer反馈信息给producer

 

if (message.getJMSReplyTo() != null) {

replyProducer.send(message.getJMSReplyTo(),

session.createTextMessage("Reply: " + message.getJMSMessageID()));

}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics