2014-10-31 06:20:28 -04:00
|
|
|
|
<?xml version="1.0" encoding="UTF-8"?>
|
|
|
|
|
<!-- ============================================================================= -->
|
|
|
|
|
<!-- Copyright © 2009 Red Hat, Inc. and others. -->
|
|
|
|
|
<!-- -->
|
|
|
|
|
<!-- The text of and illustrations in this document are licensed by Red Hat under -->
|
|
|
|
|
<!-- a Creative Commons Attribution–Share Alike 3.0 Unported license ("CC-BY-SA"). -->
|
|
|
|
|
<!-- -->
|
|
|
|
|
<!-- An explanation of CC-BY-SA is available at -->
|
|
|
|
|
<!-- -->
|
|
|
|
|
<!-- http://creativecommons.org/licenses/by-sa/3.0/. -->
|
|
|
|
|
<!-- -->
|
|
|
|
|
<!-- In accordance with CC-BY-SA, if you distribute this document or an adaptation -->
|
|
|
|
|
<!-- of it, you must provide the URL for the original version. -->
|
|
|
|
|
<!-- -->
|
|
|
|
|
<!-- Red Hat, as the licensor of this document, waives the right to enforce, -->
|
|
|
|
|
<!-- and agrees not to assert, Section 4d of CC-BY-SA to the fullest extent -->
|
|
|
|
|
<!-- permitted by applicable law. -->
|
|
|
|
|
<!-- ============================================================================= -->
|
|
|
|
|
<chapter id="pre-acknowledge">
|
|
|
|
|
<title>预先通知模式(pre-acknowledge)</title>
|
|
|
|
|
<para>JMS 规定了三种消息通知方式</para>
|
|
|
|
|
<itemizedlist>
|
|
|
|
|
<listitem>
|
|
|
|
|
<para><literal>AUTO_ACKNOWLEDGE</literal></para>
|
|
|
|
|
</listitem>
|
|
|
|
|
<listitem>
|
|
|
|
|
<para><literal>CLIENT_ACKNOWLEDGE</literal></para>
|
|
|
|
|
</listitem>
|
|
|
|
|
<listitem>
|
|
|
|
|
<para><literal>DUPS_OK_ACKNOWLEDGE</literal></para>
|
|
|
|
|
</listitem>
|
|
|
|
|
</itemizedlist>
|
|
|
|
|
<para>还有一种情况JMS不支持:应用程序在出现故障时可以容忍消息丢失,这样可以在消息在传递给客户
|
|
|
|
|
端<emphasis>之前</emphasis>就通知服务器。</para>
|
2014-11-19 03:44:57 -05:00
|
|
|
|
<para>ActiveMQ支持这种模式,称为<emphasis>pre-acknowledge</emphasis>。</para>
|
2014-10-31 06:20:28 -04:00
|
|
|
|
<para>这种模式的缺点是消息在通知后,如果系统出现故障时,消息可能丢失。并且在系统重启后该消息
|
|
|
|
|
不能恢复。</para>
|
|
|
|
|
<para>使用<literal>pre-acknowledgement</literal>模式可以节省网络传输和CPU处理资源。</para>
|
|
|
|
|
<para>股票价格更新是一个适用于此模式的例子。如果因为服务器故障丢失了一些消息,等服务器重启后新的
|
|
|
|
|
股票更新消息很快到达,以前丢失的过时的股票消息即使丢失也无关紧要。 </para>
|
|
|
|
|
<note>
|
|
|
|
|
<para>注意如果你使用pre-acknowledge模式,在接收消息端不能支持事务。因为这个模式不是在提交时
|
|
|
|
|
通知消息,是在消息在传递之前就通知了。</para>
|
|
|
|
|
</note>
|
|
|
|
|
<section id="pre-acknowledge.configure">
|
|
|
|
|
<title>使用PRE_ACKNOWLEDGE</title>
|
2014-11-19 14:58:44 -05:00
|
|
|
|
<para>这个模式在<literal>activemq-jms.xml</literal>文件中
|
2014-10-31 06:20:28 -04:00
|
|
|
|
的<literal>connection factory</literal>下配置:</para>
|
|
|
|
|
<programlisting><connection-factory name="ConnectionFactory">
|
|
|
|
|
<connectors>
|
|
|
|
|
<connector-ref connector-name="netty-connector"/>
|
|
|
|
|
</connectors>
|
|
|
|
|
<entries>
|
|
|
|
|
<entry name="ConnectionFactory"/>
|
|
|
|
|
</entries>
|
|
|
|
|
<pre-acknowledge>true</pre-acknowledge>
|
|
|
|
|
</connection-factory></programlisting>
|
|
|
|
|
<para>另一个选择是使用JMS接口来设置pre-acknowledgement模式。只需要在创建JMS会话(session)
|
2014-11-19 03:44:57 -05:00
|
|
|
|
时使用<literal>ActiveMQSession.PRE_ACKNOWLEDGE</literal>常数即可。</para>
|
2014-10-31 06:20:28 -04:00
|
|
|
|
<programlisting>
|
|
|
|
|
// messages will be acknowledge on the server *before* being delivered to the client
|
2014-11-19 03:44:57 -05:00
|
|
|
|
Session session = connection.createSession(false, ActiveMQSession.PRE_ACKNOWLEDGE);
|
2014-10-31 06:20:28 -04:00
|
|
|
|
</programlisting>
|
2014-11-19 03:44:57 -05:00
|
|
|
|
<para>你还可以直接在<literal>ActiveMQConnectionFactory</literal>实例上设置该模式。</para>
|
2014-10-31 06:20:28 -04:00
|
|
|
|
<para>另外,如果使用核心接口,则在<literal>ClientSessionFactory</literal>实例上直接
|
|
|
|
|
设置该模式。</para>
|
|
|
|
|
</section>
|
|
|
|
|
<section>
|
|
|
|
|
<title>例子</title>
|
|
|
|
|
<para>参见<xref linkend="examples.pre-acknowledge"/>。这是一个使用JMS的例子。</para>
|
|
|
|
|
</section>
|
|
|
|
|
</chapter>
|