activemq-artemis/examples/jms/delayed-redelivery
Thiago Kronig d48b4f4770 ARTEMIS-129 License header should be a normal comment
To reproduce this commit, apply a replace regex rule using:

    search regex: /\*\*\n \* Licensed
    replace: /\*\n \* Licensed

These files had to be changed manually:

    artemis-selector/src/main/javacc/HyphenatedParser.jj
    artemis-selector/src/main/javacc/StrictParser.jj
    artemis-website/src/main/resources/styles/impact/css/pygmentize.css
    artemis-website/src/main/resources/styles/impact/css/site.css
2015-06-03 10:19:45 -04:00
..
src/main ARTEMIS-129 License header should be a normal comment 2015-06-03 10:19:45 -04:00
pom.xml [maven-release-plugin] prepare for next development iteration 2015-05-21 13:47:45 +01:00
readme.html Ensure all references to the project use ActiveMQ Artemis 2015-05-13 11:51:26 +01:00

readme.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 Artemis Delayed Redelivery 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>Delayed Redelivery Example</h1>
     
     <p>This example demonstrates how ActiveMQ Artemis can be configured to provide a delayed redelivery in the case
     where a message needs to be redelivered.</p>
     <p>Delaying redelivery can often be useful in the case that clients regularly fail or roll-back. Without a delayed
     redelivery, the system can get into a "thrashing" state, with delivery being attempted, the client rolling back, and
     delivery being re-attempted ad infinitum in quick succession, using up valuable CPU and network resources.</p>
     <p>Re-delivery occurs when the session is closed with unacknowledged messages. The unacknowledged messages will
     be redelivered.</p>
     <p>By providing a redelivery delay, it can be specified that a delay of, say, 10 seconds is implemented between rollback
     and redelivery. The specific delay is configurable on both a global and per destination level, by using wild-card
     matching on the address settings.</p>

     <h2>Example setup</h2>
     <p>Redelivery delay is specified in the configuration file <a href="src/main/resources/activemq/server0/broker.xml">broker.xml</a>:</p>
     <p>In this example we set the redelivery delay to 5 seconds for the specific example queue. We could set redelivery delay on
     on multiple queues by specifying a wild-card in the match, e.g. <code>match="jms.#"</code> would apply the settings
     to all JMS queues and topics.</p>
     <p>We then consume a message in a transacted session, and rollback, and note that the message is not redelivered until
     after 5 seconds.</p>
     <pre class="prettyprint">
         <code>&lt;address-setting match="jms.queue.exampleQueue"&gt;           
            &lt;redelivery-delay&gt;5000&lt;/redelivery-delay&gt;
         &lt;/address-setting&gt;
         </code>
     </pre>                 
     <p><i>To run the example, simply type <code>mvn verify -Pexample</code> from this directory</i></p>
     <ol>
        <li>Create an initial context to perform the JNDI lookup.</li>
        <pre class="prettyprint">
           <code>initialContext = getContext(0);</code>
        </pre>

        <li>Perform a lookup on the queue</li>
        <pre class="prettyprint">
           <code>Queue queue = (Queue)initialContext.lookup("/queue/exampleQueue");</code>
        </pre>

        <li>Perform a lookup on the Connection Factory</li>
        <pre class="prettyprint">
           <code>ConnectionFactory cf = (ConnectionFactory)initialContext.lookup("/ConnectionFactory");</code>
        </pre>

        <li>Create a JMS Connection</li>
        <pre class="prettyprint">
           <code>connection = cf.createConnection();</code>
        </pre>

        <li>Create a transacted JMS Session
        <pre class="prettyprint">
           <code>Session session = connection.createSession(true, 0);</code>
        </pre>

        <li>Create a JMS Message Producer</li>
        <pre class="prettyprint">
          <code>MessageProducer producer = session.createProducer(queue);</code>
       </pre>
       
        <li>Create a Text Message</li>
        <pre class="prettyprint">
            <code>TextMessage message = session.createTextMessage("this is a text message");</code>
        </pre>

        <li>Send the Message</li>
        <pre class="prettyprint">
            <code>producer.send(message);</code>
        </pre>
        
       <li>We commit the session to effectively send the message to the queue</li>
        <pre class="prettyprint">
            <code>session.commit();</code>
        </pre>

        <li>We create a JMS message consumer on the queue</li>
        <pre class="prettyprint">
            <code>MessageConsumer messageConsumer = session.createConsumer(queue);</code>
        </pre>
        
        <li>We start the connection. In order for delivery to occur on any consumers or subscribers on a connection, the connection must be started</li>
        <pre class="prettyprint">
           <code>connection.start();</code>
        </pre>
        
        <li>We receive the message...</li>
        <pre class="prettyprint">
            <code>TextMessage messageReceived = (TextMessage)messageConsumer.receive(5000);
            System.out.println("1st delivery from " + queue.getQueueName() + ": " + messageReceived.getText());</code>            
        </pre>
        
        <li>...but we roll back the session. the message returns to the queue, but only after a 
            5 second delay</li>
        <pre class="prettyprint">
            <code>session.rollback();</code>
        </pre>
        
        <li>We try to receive the message but it's being delayed</li>
        <pre class="prettyprint">
            <code>
         messageReceived = (TextMessage)messageConsumer.receive(3000);
         
         if (messageReceived != null)
         {
            return false;
         }
         
         System.out.println("Redelivery has been delayed so received message is " + messageReceived);
            
            </code>
        </pre>
  
       <li>We try and receive the message again, this time we should get it</li>
       <pre class="prettyprint">
           <code>             
         messageReceived = (TextMessage)messageConsumer.receive(3000);
         
         System.out.println("2nd delivery from " + queue.getQueueName() + ": " + messageReceived.getText());
           </code>
       </pre>
              
        <li>We rollback the session again to cause another redelivery, and we time how long this one takes</code>
        <pre class="prettyprint">
           <code>
         long start = System.currentTimeMillis();
         
         session.rollback();
                         
         messageReceived = (TextMessage)messageConsumer.receive(8000);
         
         long end = System.currentTimeMillis();
         
         System.out.println("3nd delivery from " + queue.getQueueName() + ": " + messageReceived.getText() +
                            " after " + (end - start) + " milliseconds.");           
           </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#undelivered-messages">Undelivered Messages chapter</a></li>
     </ul>
    
  </body>
</html>