Chapter 29. 预先通知模式(pre-acknowledge)

JMS 规定了三种消息通知方式

还有一种情况JMS不支持:应用程序在出现故障时可以容忍消息丢失,这样可以在消息在传递给客户 端之前就通知服务器。

HornetQ支持这种模式,称为pre-acknowledge

这种模式的缺点是消息在通知后,如果系统出现故障时,消息可能丢失。并且在系统重启后该消息 不能恢复。

使用pre-acknowledgement模式可以节省网络传输和CPU处理资源。

股票价格更新是一个适用于此模式的例子。如果因为服务器故障丢失了一些消息,等服务器重启后新的 股票更新消息很快到达,以前丢失的过时的股票消息即使丢失也无关紧要。

Note

注意如果你使用pre-acknowledge模式,在接收消息端不能支持事务。因为这个模式不是在提交时 通知消息,是在消息在传递之前就通知了。

29.1. 使用PRE_ACKNOWLEDGE

这个模式在hornetq-jms.xml文件中 的connection factory下配置:

<connection-factory name="ConnectionFactory">
      <connectors>
         <connector-ref connector-name="netty-connector"/>
      </connectors>
      <entries>
         <entry name="ConnectionFactory"/>
      </entries>
      <pre-acknowledge>true</pre-acknowledge>
</connection-factory>

另一个选择是使用JMS接口来设置pre-acknowledgement模式。只需要在创建JMS会话(session) 时使用HornetQSession.PRE_ACKNOWLEDGE常数即可。

// messages will be acknowledge on the server *before* being delivered to the client
Session session = connection.createSession(false, HornetQSession.PRE_ACKNOWLEDGE);
      

你还可以直接在HornetQConnectionFactory实例上设置该模式。

另外,如果使用核心接口,则在ClientSessionFactory实例上直接 设置该模式。

29.2. 例子

参见Section 11.1.35, “预先通知”。这是一个使用JMS的例子。