`
sony-soft
  • 浏览: 1019256 次
文章分类
社区版块
存档分类
最新评论

ActiveMQ: Point-to-Point (点对点)消息模式开发流程

 
阅读更多

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

1.1 创建 Connection

根据 url user password 创建一个 jms Connection

1.2 创建 Session

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

1.3 创建 Destination 对象:

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

1.4 创建 MessageProducer

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

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

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

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

2.1 实 现 MessageListener 接口:

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

2.2 创 建 Connection

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

2.3 创 建 Session Destination

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