2014-12-11 07:17:29 -05:00
|
|
|
# Diverting and Splitting Message Flows
|
2014-12-04 10:25:29 -05:00
|
|
|
|
2015-04-27 17:32:30 -04:00
|
|
|
Apache ActiveMQ Artemis allows you to configure objects called *diverts* with some
|
2014-12-04 10:25:29 -05:00
|
|
|
simple server configuration.
|
|
|
|
|
|
|
|
Diverts allow you to transparently divert messages routed to one address
|
|
|
|
to some other address, without making any changes to any client
|
|
|
|
application logic.
|
|
|
|
|
|
|
|
Diverts can be *exclusive*, meaning that the message is diverted to the
|
|
|
|
new address, and does not go to the old address at all, or they can be
|
|
|
|
*non-exclusive* which means the message continues to go the old address,
|
|
|
|
and a *copy* of it is also sent to the new address. Non-exclusive
|
|
|
|
diverts can therefore be used for *splitting* message flows, e.g. there
|
|
|
|
may be a requirement to monitor every order sent to an order queue.
|
|
|
|
|
2016-03-05 05:57:09 -05:00
|
|
|
When an address has both exclusive and non-exclusive diverts configured,
|
|
|
|
the exclusive ones are processed first. If any of the exclusive diverts
|
|
|
|
diverted the message, the non-exclusive ones are not processed.
|
|
|
|
|
2018-11-19 14:21:51 -05:00
|
|
|
Diverts can also be configured to have an optional message filter. If
|
|
|
|
specified then only messages that match the filter will be diverted.
|
|
|
|
|
|
|
|
Diverts can apply a particular routing-type to the message, strip the
|
|
|
|
existing routing type, or simply pass the existing routing-type through.
|
|
|
|
This is useful in situations where the message may have its routing-type
|
|
|
|
set but you want to divert it to an address using a different routing-type.
|
|
|
|
It's important to keep in mind that a message with the `anycast`
|
|
|
|
routing-type will not actually be routed to queues using `multicast` and
|
|
|
|
vice-versa. By configuring the `routing-type` of the divert you have the
|
|
|
|
flexibility to deal with any situation. Valid values are `ANYCAST`,
|
|
|
|
`MULTICAST`, `PASS`, & `STRIP`. The default is `STRIP`.
|
|
|
|
|
2019-10-25 12:56:40 -04:00
|
|
|
Diverts can also be configured to apply a [`Transformer`](transformers.md).
|
|
|
|
If specified, all diverted messages will have the opportunity of being
|
|
|
|
transformed by the `Transformer`. When an address has multiple diverts
|
|
|
|
configured, all of them receive the same, original message. This means that
|
|
|
|
the results of a transformer on a message are not directly available for
|
|
|
|
other diverts or their filters on the same address.
|
2014-12-04 10:25:29 -05:00
|
|
|
|
2018-01-22 15:47:53 -05:00
|
|
|
See the documentation on [adding runtime dependencies](using-server.md) to
|
|
|
|
understand how to make your transformer available to the broker.
|
|
|
|
|
2014-12-04 10:25:29 -05:00
|
|
|
A divert will only divert a message to an address on the *same server*,
|
|
|
|
however, if you want to divert to an address on a different server, a
|
|
|
|
common pattern would be to divert to a local store-and-forward queue,
|
|
|
|
then set up a bridge which consumes from that queue and forwards to an
|
|
|
|
address on a different server.
|
|
|
|
|
|
|
|
Diverts are therefore a very sophisticated concept, which when combined
|
|
|
|
with bridges can be used to create interesting and complex routings. The
|
|
|
|
set of diverts on a server can be thought of as a type of routing table
|
|
|
|
for messages. Combining diverts with bridges allows you to create a
|
|
|
|
distributed network of reliable routing connections between multiple
|
|
|
|
geographically distributed servers, creating your global messaging mesh.
|
|
|
|
|
2017-10-12 03:19:30 -04:00
|
|
|
Diverts are defined as xml in the `broker.xml` file at the `core` attribute level.
|
2014-12-04 10:25:29 -05:00
|
|
|
There can be zero or more diverts in the file.
|
|
|
|
|
2017-12-25 15:17:04 -05:00
|
|
|
Diverted message gets a new message ID, and its address is set to a forward
|
|
|
|
address. To access original values, use message properties: original destination
|
|
|
|
is stored in a String property `_AMQ_ORIG_ADDRESS` (`Message.HDR_ORIGINAL_ADDRESS`
|
|
|
|
constant from the Core API), and the original message ID in a Long property
|
|
|
|
`_AMQ_ORIG_MESSAGE_ID` (`Message.HDR_ORIG_MESSAGE_ID` constant from the
|
|
|
|
Core API).
|
|
|
|
|
2015-01-21 13:27:19 -05:00
|
|
|
Please see the examples for a full working example showing you how to
|
|
|
|
configure and use diverts.
|
2014-12-04 10:25:29 -05:00
|
|
|
|
|
|
|
Let's take a look at some divert examples:
|
|
|
|
|
2014-12-11 07:17:29 -05:00
|
|
|
## Exclusive Divert
|
2014-12-04 10:25:29 -05:00
|
|
|
|
|
|
|
Let's take a look at an exclusive divert. An exclusive divert diverts
|
|
|
|
all matching messages that are routed to the old address to the new
|
|
|
|
address. Matching messages do not get routed to the old address.
|
|
|
|
|
|
|
|
Here's some example xml configuration for an exclusive divert, it's
|
|
|
|
taken from the divert example:
|
|
|
|
|
2018-03-08 15:46:38 -05:00
|
|
|
```xml
|
|
|
|
<divert name="prices-divert">
|
|
|
|
<address>priceUpdates</address>
|
|
|
|
<forwarding-address>priceForwarding</forwarding-address>
|
|
|
|
<filter string="office='New York'"/>
|
|
|
|
<transformer-class-name>
|
|
|
|
org.apache.activemq.artemis.jms.example.AddForwardingTimeTransformer
|
|
|
|
</transformer-class-name>
|
|
|
|
<exclusive>true</exclusive>
|
|
|
|
</divert>
|
|
|
|
```
|
2014-12-04 10:25:29 -05:00
|
|
|
|
2017-08-28 22:36:10 -04:00
|
|
|
We define a divert called `prices-divert` that will divert any
|
|
|
|
messages sent to the address `priceUpdates` to another local address
|
|
|
|
`priceForwarding`.
|
2014-12-04 10:25:29 -05:00
|
|
|
|
|
|
|
We also specify a message filter string so only messages with the
|
|
|
|
message property `office` with value `New York` will get diverted, all
|
|
|
|
other messages will continue to be routed to the normal address. The
|
|
|
|
filter string is optional, if not specified then all messages will be
|
|
|
|
considered matched.
|
|
|
|
|
2019-10-25 12:56:40 -04:00
|
|
|
In this example a transformer class is specified without any configuration
|
|
|
|
properties. Again this is optional, and if specified the transformer will
|
|
|
|
be executed for each matching message. This allows you to change the
|
|
|
|
messages body or properties before it is diverted. In this example the
|
|
|
|
transformer simply adds a header that records the time the divert happened.
|
|
|
|
See the [transformer chapter](transformers.md) for more details about
|
|
|
|
transformer-specific configuration.
|
2014-12-04 10:25:29 -05:00
|
|
|
|
|
|
|
This example is actually diverting messages to a local store and forward
|
|
|
|
queue, which is configured with a bridge which forwards the message to
|
2019-10-25 12:56:40 -04:00
|
|
|
an address on another ActiveMQ Artemis server. Please see the example for
|
|
|
|
more details.
|
2014-12-04 10:25:29 -05:00
|
|
|
|
2014-12-11 07:17:29 -05:00
|
|
|
## Non-exclusive Divert
|
2014-12-04 10:25:29 -05:00
|
|
|
|
|
|
|
Now we'll take a look at a non-exclusive divert. Non exclusive diverts
|
|
|
|
are the same as exclusive diverts, but they only forward a *copy* of the
|
|
|
|
message to the new address. The original message continues to the old
|
|
|
|
address
|
|
|
|
|
|
|
|
You can therefore think of non-exclusive diverts as *splitting* a
|
|
|
|
message flow.
|
|
|
|
|
|
|
|
Non exclusive diverts can be configured in the same way as exclusive
|
|
|
|
diverts with an optional filter and transformer, here's an example
|
|
|
|
non-exclusive divert, again from the divert example:
|
|
|
|
|
2018-03-08 15:46:38 -05:00
|
|
|
```xml
|
|
|
|
<divert name="order-divert">
|
2018-03-09 10:07:38 -05:00
|
|
|
<address>orders</address>
|
|
|
|
<forwarding-address>spyTopic</forwarding-address>
|
|
|
|
<exclusive>false</exclusive>
|
2018-03-08 15:46:38 -05:00
|
|
|
</divert>
|
|
|
|
```
|
2014-12-04 10:25:29 -05:00
|
|
|
|
|
|
|
The above divert example takes a copy of every message sent to the
|
2017-08-28 22:36:10 -04:00
|
|
|
address '`orders`' and sends it to a local address called
|
|
|
|
'`spyTopic`'.
|