blob: f9af3a1652273237bf8b3cc575d07e8fd18cd00c [file] [log] [blame]
<div class="wiki-content maincontent"><p>Sometimes it can be useful to ensure that every topic consumer sees messages arriving on the topic in exactly the same order. Normally the broker will guarantee the order of all messages sent by the same producer. However, owing to the broker's use of multiple threads and asynchronous processing, messages from different producers could arrive in different consumers in different orders.</p><p>For example, if we have producers&#160;<strong><code>P</code></strong> and&#160;<strong><code>Q</code></strong> sending messages such that at about the same time&#160;<strong><code>P</code></strong> sends <strong><code>P1</code>, <code>P2</code></strong>,&#160;<strong><code>P3</code></strong> and&#160;<strong><code>Q</code></strong> sends <strong><code>Q1</code>, <code>Q2</code></strong>. Therefore, two different consumers <em>could</em> see messages arrive in the following order:</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
<pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">consumer1: P1 P2 Q1 P3 Q2
consumer2: P1 Q1 Q2 P2 P3
</pre>
</div></div><p>In this example each producer's messages are in self-relative order. However, the streams of messages across producers can get intermixed.</p><p><em>Total Ordering</em> of a destination in ActiveMQ ensures that each consumer will see the same total order on that topic. This has a performance cost, since greater synchronization is required. This can be useful, particularly when very fast optimistic transactions are required. With total ordering the messages would arrive like this:</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
<pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">consumer1: P1 P2 Q1 P3 Q2
consumer2: P1 P2 Q1 P3 Q2
</pre>
</div></div><h2 id="TotalOrdering-ConfiguringTotalOrdering">Configuring Total Ordering</h2><p>Enable the&#160;<strong><code>&lt;strictOrderDispatchPolicy/&gt;</code></strong> on the <a shape="rect" href="per-destination-policies.html">Per Destination Policies</a>. Here's an example</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeContent panelContent pdl">
<pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">&lt;destinationPolicy&gt;
&lt;policyMap&gt;
&lt;policyEntries&gt;
&lt;policyEntry topic="&gt;"&gt;
&lt;dispatchPolicy&gt;
&lt;strictOrderDispatchPolicy/&gt;
&lt;/dispatchPolicy&gt;
&lt;/policyEntry&gt;
&lt;/policyEntries&gt;
&lt;/policyMap&gt;
&lt;/destinationPolicy&gt;</pre>
</div></div></div>