activemq-artemis/examples/jms/producer-rate-limit
Martyn Taylor 97210427df [maven-release-plugin] prepare for next development iteration 2015-05-20 11:26:38 +01:00
..
src/main fixed examples and some docs after Artemis renaming 2015-04-30 10:44:16 +01:00
pom.xml [maven-release-plugin] prepare for next development iteration 2015-05-20 11:26:38 +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 JMS Message Producer Rate Limiting</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 Producer Rate Limiting</h1>

     <p>With ActiveMQ Artemis you can specify a maximum send rate at which a JMS MessageProducer will send messages.
     This can be specified when creating or deploying the connection factory. See <code>activemq-jms.xml</code></p>
     <p>If this value is specified then ActiveMQ Artemis will ensure that messages are never produced at a rate higher than
     specified. This is a form of producer <i>throttling</i>.</p>     
     <h2>Example step-by-step</h2>
     <p>In this example we specify a <code>producer-max-rate</code> of <code>50</code> messages per second in the <code>activemq-jms.xml</code>
     file when deploying the connection factory:</p>
     <pre class="prettyprint">
     <code>
   &lt;connection-factory name="ConnectionFactory"&gt;
      &lt;connector-ref connector-name="netty-connector"/&gt;
      &lt;entries&gt;
         &lt;entry name="ConnectionFactory"/&gt;       
      &lt;/entries&gt;
      
      &lt;!-- We limit producers created on this connection factory to produce messages at a maximum rate
      of 50 messages per sec --&gt;
      &lt;producer-max-rate&gt;50&lt;/producer-max-rate&gt;
      
   &lt;/connection-factory&gt;
     </code>
     </pre>
     <p>We then simply send as many messages as we can in 10 seconds and note how many messages are actually sent.</p>
     <p>We note that the number of messages sent per second never exceeds the specified value of <code>50</code> messages per second.</p>

     <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>Perfom 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 JMS Session</li>
        <pre class="prettyprint">
           <code>Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);</code>
        </pre>

        <li>Create a JMS Message Producer</li>
        <pre class="prettyprint">
          <code>MessageProducer producer = session.createProducer(queue);</code>
        </pre>

        <li>Send as many messages as we can in 10 seconds</li>
        <pre class="prettyprint">
           <code>
        final long duration = 10000;

        int i = 0;

        long start = System.currentTimeMillis();

        while (System.currentTimeMillis() - start <= duration)
        {
           TextMessage message = session.createTextMessage("This is text message: " + i++);

           producer.send(message);
        }

        long end = System.currentTimeMillis();

        double rate = 1000 * (double)i / (end - start);

        System.out.println("We sent " + i + " messages in " + (end - start) + " milliseconds");

        System.out.println("Actual send rate was " + rate + " messages per second");                      
           </code>
        </pre>
        
        <li>We note that the sending rate doesn't exceed 50 messages per second. Here's some example output from a real
        run</li>
        
        <pre class="prettyprint">
           <code>
     [java] Will now send as many messages as we can in 10 seconds...
     [java] We sent 500 messages in 10072 milliseconds
     [java] Actual send rate was 49.64257347100874 messages per second
           </code>
        </pre>
           

        <li>For good measure we consumer the messages we produced.</li>
        <pre class="prettyprint">
           <code>
        MessageConsumer messageConsumer = session.createConsumer(queue);

        connection.start();

        System.out.println("Now consuming the messages...");

        i = 0;
        while (true)
        {
           TextMessage messageReceived = (TextMessage)messageConsumer.receive(5000);

           if (messageReceived == null)
           {
              break;
           }

           i++;
        }

        System.out.println("Received " + i + " messages");           
           
           </code>
        </pre>

        <li>Be sure to close our resources!</li>

        <pre class="prettyprint">
           <code>
           finally
           {
              if (initialContext != null)
              {
                initialContext.close();
              }
              
              if (connection != null)
              {
                 connection.close();
              }
           }</code>
        </pre>



     </ol>
  </body>
</html>