activemq-artemis/examples/features/standard/divert
Martyn Taylor 221039e353 [maven-release-plugin] prepare release 1.3.0 2016-06-09 12:49:13 +01:00
..
src/main Add missing @Override annotations 2015-12-06 01:27:35 +02:00
pom.xml [maven-release-plugin] prepare release 1.3.0 2016-06-09 12:49:13 +01:00
readme.html renaming broker-features -> features on examples 2015-08-13 00:11:56 -04: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 Divert 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>Divert Example</h1>
     <pre>To run the example, simply type <b>mvn verify</b> from this directory, <br>or <b>mvn -PnoServer verify</b> if you want to start and create the server manually.</pre>

     <p>ActiveMQ Artemis diverts allow messages to be transparently "diverted" from one address to another
     with just some simple configuration defined on the server side.</p>
     <p>Diverts can be defined to be <b>exclusive</b> or <b>non-exclusive</b>.</p>
     <p>With an <b>exclusive</b> divert the message is intercepted and does not get sent to the queues originally
     bound to that address - it only gets diverted.</p>
     <p>With a <b>non-exclusive</b> divert the message continues to go to the queues bound to the address,
     but also a <b>copy</b> of the message gets sent to the address specified in the divert. Consequently non-exclusive
     diverts can be used to "snoop" on another address</p>
     <p>Diverts can also be configured to have an optional filter. If specified then only matching messages
     will be diverted.</p>
     <p>Diverts can be configured to apply a Transformer. If specified, all diverted messages will have the
     opportunity of being transformed by the Transformer.</p>
     <p>Diverts are a very sophisticated concept, which when combined with bridges can be used to create
     interesting and complex routings. The set of diverts can be thought of as a type of <i>routing table</i>
     for messages.</p>

     <h2>Example step-by-step</h2>
     <p>In this example we will imagine a fictitious company which has two offices; one in London and another in New York.</p>
     <p>The company accepts orders for it's products only at it's London office, and also generates price-updates
     for it's products, also only from it's London office. However only the New York office is interested in receiving
     price updates for New York products. Any prices for New York products need to be forwarded to the New York office.</p>
     <p>There is an unreliable WAN linking the London and New York offices.</p>
     <p>The company also requires a copy of any order received to be available to be inspected by management.</p>
     <p>In order to achieve this, we will create a queue <code>orderQueue</code> on the London server in to which orders arrive.</p>
     <p>We will create a topic, <code>spyTopic</code> on the London server, and there will be two subscribers both in London.</p>
     <p>We will create a <i>non-exclusive</i> divert on the London server which will siphon off a copy of each order
     received to the topic <code>spyTopic</code>.</p>
     <p>Here's the xml config for that divert, from <code>broker.xml</code></p>
     <pre class="prettyprint">
        <code>
     &lt;divert name="order-divert"&gt;
         &lt;address&gt;jms.queue.orders&lt;/address&gt;
         &lt;forwarding-address&gt;jms.topic.spyTopic&lt;/forwarding-address&gt;
         &lt;exclusive&gt;false&lt;/exclusive&gt;
      &lt;/divert&gt;
         </code>
     </pre>
     <p>For the prices we will create a topic on the London server, <code>priceUpdates</code> to which all price updates
     are sent. We will create another topic on the New York server <code>newYorkPriceUpdates</code> to which all New York
     price updates need to be forwarded.</p>
     <p>Diverts can only be used to divert messages from one <b>local</b> address to another <b>local</b> address
     so we cannot divert directly to an address on another server.</p>
     <p>Instead we divert to a local <i>store and forward queue</i> they we define in the configuration. This is just a normal queue
     that we use for storing messages before forwarding to another node.</p>
     <p>Here's the configuration for it:</p>
     <pre class="prettyprint">
        <code>
     &lt;queues&gt;
        &lt;queue name="jms.queue.priceForwarding"&gt;
           &lt;address&gt;jms.queue.priceForwarding&lt;/address&gt;
        &lt;/queue&gt;
     &lt;/queues&gt;
         </code>
      </pre>
     <p>Here's the configuration for the divert:</p>
     <pre class="prettyprint">
        <code>
     &lt;divert name="prices-divert"&gt;
	     &lt;address&gt;jms.topic.priceUpdates&lt;/address&gt;
	     &lt;forwarding-address&gt;jms.queue.priceForwarding&lt;/forwarding-address&gt;
	     &lt;filter string="office='New York'"/&gt;
	     &lt;transformer-class-name&gt;org.apache.activemq.artemis.jms.example.AddForwardingTimeTransformer&lt;/transformer-class-name&gt;
	     &lt;exclusive&gt;true&lt;/exclusive&gt;
	  &lt;/divert&gt;
	     </code>
	  </pre>
	  <p>Note we specify a filter in the divert, so only New York prices get diverted. We also specify a Transformer class
	  since we are going to insert a header in the message at divert time, recording the time the diversion happened.</p>
	  <p>And finally we define a bridge that moves messages from the local queue to the address on the New York server.
	  Bridges move messages from queues to remote addresses and are ideal to use when the target server may be stopped and
	  started independently, and/or the network might be unreliable. Bridges guarantee once and only once delivery
	  of messages from their source queues to their target addresses.</p>
	  <p>Here is the bridge configuration: </p>
	  <pre class="prettyprint">
	     <code>
	  &lt;bridges&gt;
	     &lt;bridge name="price-forward-bridge"&gt;
	        &lt;queue-name&gt;jms.queue.priceForwarding&lt;/queue-name&gt;
	        &lt;forwarding-address&gt;jms.topic.newYorkPriceUpdates&lt;/forwarding-address&gt;
	        &lt;reconnect-attempts&gt;-1&lt;/reconnect-attempts&gt;
          &lt;static-connectors>
             &lt;connector-ref>newyork-connector&lt;/connector-ref>
          &lt;/static-connectors>
	     &lt;/bridge&gt;
      &lt;/bridges&gt;
         </code>
     </pre>
  </body>
</html>