157 lines
6.9 KiB
HTML
157 lines
6.9 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 JMS Message Priority 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>JMS Message Priority Example</h1>
|
|
|
|
<p>This example shows how messages with different priorities are delivered in different orders.</p>
|
|
|
|
<p>The Message Priority property carries the delivery preference of sent messages. It can be set by the message's
|
|
standard header field 'JMSPriority' as defined in JMS specification version 1.1. The value is of type
|
|
integer, ranging from 0 (the lowest) to 9 (the highest). When messages are being delivered, their priorities
|
|
will effect their order of delivery. Messages of higher priorities will likely be delivered before those
|
|
of lower priorities. Messages of equal priorities are delivered in the natural order of their arrival at
|
|
their destinations. Please consult the JMS 1.1 specification for full details.</p>
|
|
|
|
<p>In this example, three messages are sent to a queue with different priorities. The first message is sent
|
|
with default priority (4), the second is sent with a higher priority (5), and the third has the highest
|
|
priority (9). At the receiving end, we will show the order of receiving of the three messages. You will
|
|
see that the third message, though last sent, will 'jump' forward to be the first one received. The second
|
|
is also received ahead of the message first sent, but behind the third message. The first message, regardless
|
|
of its being sent first, arrives last.</p>
|
|
|
|
<h2>Example step-by-step</h2>
|
|
<p><i>To run the example, simply type <code>mvn verify -Pexample</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 a JMS message producer on the session. This will be used to send the messages.</li>
|
|
<pre class="prettyprint">
|
|
<code>MessageProducer messageProducer = session.createProducer(topic);</code>
|
|
</pre>
|
|
|
|
<li>We Create a JMS Message Consumer.</li>
|
|
<pre class="prettyprint">
|
|
<code>
|
|
MessageConsumer redConsumer = session.createConsumer(queue);
|
|
redConsumer.setMessageListener(new SimpleMessageListener());
|
|
</code>
|
|
</pre>
|
|
|
|
<li>We Create three messages.</li>
|
|
<pre class="prettyprint">
|
|
<code>
|
|
TextMessage[] sentMessages = new TextMessage[3];
|
|
sentMessages[0] = session.createTextMessage("first message");
|
|
sentMessages[1] = session.createTextMessage("second message");
|
|
sentMessages[2] = session.createTextMessage("third message");
|
|
</code>
|
|
</pre>
|
|
|
|
<li>Send the Messages, each has a different priority.</li>
|
|
<pre class="prettyprint">
|
|
<code>
|
|
producer.send(sentMessages[0]);
|
|
System.out.println("Message sent: " + sentMessages[0].getText() + " with priority: " + sentMessages[0].getJMSPriority());
|
|
producer.send(sentMessages[1], DeliveryMode.NON_PERSISTENT, 5, 0);
|
|
System.out.println("Message sent: " + sentMessages[1].getText() + "with priority: " + sentMessages[1].getJMSPriority());
|
|
producer.send(sentMessages[2], DeliveryMode.NON_PERSISTENT, 9, 0);
|
|
System.out.println("Message sent: " + sentMessages[2].getText() + "with priority: " + sentMessages[2].getJMSPriority());
|
|
</code>
|
|
</pre>
|
|
|
|
<li>We start the connection now.</li>
|
|
<pre class="prettyprint">
|
|
<code>
|
|
connection.start();
|
|
</code>
|
|
</pre>
|
|
|
|
<li>We wait for message delivery completion</li>
|
|
<pre class="prettyprint">
|
|
<code>
|
|
Thread.sleep(5000);
|
|
</code>
|
|
</pre>
|
|
|
|
<li>We wait for message delivery completion</li>
|
|
<pre class="prettyprint">
|
|
<code>
|
|
for (int i = 0; i < 3; i++)
|
|
{
|
|
TextMessage rm = msgReceived.get(i);
|
|
if (!rm.getText().equals(sentMessages[2-i].getText()))
|
|
{
|
|
System.err.println("Priority is broken!");
|
|
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>
|
|
</body>
|
|
</html>
|