160 lines
7.1 KiB
HTML
160 lines
7.1 KiB
HTML
<!--
|
|
Licensed to the Apache Software Foundation (ASF) under one
|
|
or more contributor license agreements. See the NOTICE file
|
|
distributed with this work for additional information
|
|
regarding copyright ownership. The ASF licenses this file
|
|
to you under the Apache License, Version 2.0 (the
|
|
"License"); you may not use this file except in compliance
|
|
with the License. You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing,
|
|
software distributed under the License is distributed on an
|
|
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
KIND, either express or implied. See the License for the
|
|
specific language governing permissions and limitations
|
|
under the License.
|
|
-->
|
|
|
|
<html>
|
|
<head>
|
|
<title>ActiveMQ Message Group Example</title>
|
|
<link rel="stylesheet" type="text/css" href="../common/common.css" />
|
|
<link rel="stylesheet" type="text/css" href="../common/prettify.css" />
|
|
<script type="text/javascript" src="../common/prettify.js"></script>
|
|
</head>
|
|
<body onload="prettyPrint()">
|
|
<h1>Message Group Example</h1>
|
|
|
|
<p>This example shows you how to configure and use message groups via a connection factory with ActiveMQ.</p>
|
|
|
|
<p>Message groups are sets of messages that has the following characteristics: </p>
|
|
<li>Messages in a message group share the same group id, i.e. they have same JMSXGroupID string property values.</li>
|
|
<li>Messages in a message group will be all delivered to no more than one of the queue's consumers. The consumer that receives the
|
|
first message of a group will receive all the messages that belongs to the group.</li>
|
|
|
|
<p>You can make any message belong to a message group by setting a 'group-id' on the connection factory. All producers created via this connection factory will set that group id on its messages.
|
|
In this example we set the group id 'Group-0'on a connection factory and send messages via 2 different producers and check that only 1 consumer receives them. </p>
|
|
|
|
<p>Alternatively, ActiveMQ's connection factories can be configured to <em>auto group</em> messages. By setting <code>autogroup</code> to </code>true</code> on the <code>ActiveMQConnectionFactory</code>
|
|
(or setting <code><autogroup>true</autogroup></code> in <code>activemq-jms.xml</code>'s connection factory settings), a random unique id
|
|
will be picked to create a message group. <em>Every messages</em> sent by a producer created from this connection factory will automatically
|
|
be part of this message group.</p>
|
|
|
|
<h2>Example step-by-step</h2>
|
|
<p><i>To run the example, simply type <code>mvn verify</code> from this directory</i></p>
|
|
<ol>
|
|
<li>First we need to get an initial context so we can look-up the JMS connection factory and destination objects from JNDI. This initial context will get it's properties from the <code>client-jndi.properties</code> file in the directory <code>../common/config</code></li>
|
|
<pre class="prettyprint">
|
|
<code>InitialContext initialContext = getContext();</code>
|
|
</pre>
|
|
|
|
<li>We look-up the JMS queue object from JNDI</li>
|
|
<pre class="prettyprint">
|
|
<code>Queue queue = (Queue) initialContext.lookup("/queue/exampleQueue");</code>
|
|
</pre>
|
|
|
|
<li>We look-up the JMS connection factory object from JNDI</li>
|
|
<pre class="prettyprint">
|
|
<code>ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("/ConnectionFactory");</code>
|
|
</pre>
|
|
|
|
<li>We create a JMS connection</li>
|
|
<pre class="prettyprint">
|
|
<code>connection = cf.createConnection();</code>
|
|
</pre>
|
|
|
|
<li>We create a JMS session. The session is created as non transacted and will auto acknowledge messages.</li>
|
|
<pre class="prettyprint">
|
|
<code>Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);</code>
|
|
</pre>
|
|
|
|
<li>We create 2 JMS message producers on the session. This will be used to send the messages.</li>
|
|
<pre class="prettyprint">
|
|
<code>
|
|
MessageProducer producer1 = session.createProducer(queue);
|
|
|
|
MessageProducer producer2 = session.createProducer(queue);</code>
|
|
</pre>
|
|
|
|
<li>We create two consumers.</li>
|
|
<pre class="prettyprint">
|
|
<code>
|
|
MessageConsumer consumer1 = session.createConsumer(queue);
|
|
consumer1.setMessageListener(new SimpleMessageListener("consumer-1"));
|
|
MessageConsumer consumer2 = session.createConsumer(queue);
|
|
consumer2.setMessageListener(new SimpleMessageListener("consumer-2"));
|
|
</code>
|
|
</pre>
|
|
|
|
<li>We create and send 10 text messages using each producer</li>
|
|
<pre class="prettyprint">
|
|
<code>
|
|
int msgCount = 10;
|
|
for (int i = 0; i < msgCount; i++)
|
|
{
|
|
TextMessage m = session.createTextMessage("producer1 message " + i);
|
|
producer1.send(m);
|
|
System.out.println("Sent message: " + m.getText());
|
|
TextMessage m2 = session.createTextMessage("producer2 message " + i);
|
|
producer2.send(m2);
|
|
System.out.println("Sent message: " + m2.getText());
|
|
}
|
|
</code>
|
|
</pre>
|
|
|
|
<li>We start the connection.</li>
|
|
<pre class="prettyprint">
|
|
<code>connection.start();</code>
|
|
</pre>
|
|
|
|
<li>We check the group messages are received by only one consumer</li>
|
|
<pre class="prettyprint">
|
|
<code>
|
|
String trueReceiver = messageReceiverMap.get("producer1 message " + 0);
|
|
for (int i = 0; i < msgCount; i++)
|
|
{
|
|
String receiver = messageReceiverMap.get("producer1 message " + i);
|
|
if (!trueReceiver.equals(receiver))
|
|
{
|
|
System.out.println("Group message [producer1 message " + i + "] went to wrong receiver: " + receiver);
|
|
result = false;
|
|
}
|
|
receiver = messageReceiverMap.get("producer2 message " + i);
|
|
if (!trueReceiver.equals(receiver))
|
|
{
|
|
System.out.println("Group message [producer2 message " + i + "] went to wrong receiver: " + receiver);
|
|
result = false;
|
|
}
|
|
}
|
|
|
|
</code>
|
|
</pre>
|
|
|
|
<li>And finally, <b>always</b> remember to close your JMS connections and resources after use, in a <code>finally</code> block. Closing a JMS connection will automatically close all of its sessions, consumers, producer and browser objects</li>
|
|
|
|
<pre class="prettyprint">
|
|
<code>finally
|
|
{
|
|
if (initialContext != null)
|
|
{
|
|
initialContext.close();
|
|
}
|
|
if (connection != null)
|
|
{
|
|
connection.close();
|
|
}
|
|
}</code>
|
|
</pre>
|
|
</ol>
|
|
|
|
<h2>More information</h2>
|
|
|
|
<ul>
|
|
<li>User Manual's <a href="../../../docs/user-manual/en/html_single/index.html#message-grouping2">Message Grouping chapter</a></li>
|
|
</ul>
|
|
|
|
</body>
|
|
</html>
|