javajmsactivemq-artemis

Forward a message to an arbitrary list of addresses in ActiveMQ Artemis


I want to create a fan-out on ActiveMQ Artemis.

This is the scenario:

  1. The producer sends a message on the dedicated address (example: inputMsg).
  2. Artemis forwards the message to all consumers that respect a specific pattern (e.g.: bc.* such as bc.service1, bc.client2, ...). The divert does not know the complete list of the consumers because can change dynamically.

I saw that the composite-divert should be good for me but it requires to know the complete list of consumers.

I tried to use wildcard-syntax as filter and forwarding-address with the following configuration on the broker.xml but it seems to not work properly:

<divert name="fan-out-divert">
    <address>inputMsg</address>
    <forwarding-address>bc.*</forwarding-address>
    <filter string="route='broadcast'"/>
    <exclusive>false</exclusive>
</divert>
<divert name="wildcard-divert">
    <address>inputMsg</address>
    <forwarding-address>wildcardconsumer</forwarding-address>
    <filter string="route='bc.#'"/>
    <exclusive>false</exclusive>
</divert>

Is there a way to configure this behavior?


Solution

  • The internal ActiveMQ Artemis address model is based on addresses, queues and routing types. When a message is sent to an address it is routed to one or more of its queues based on the configured routing type. The messages are consumed from a queue that is bound to an address.

    You can use the multicast routing type and the FQQNs to send messages to a single queue or all the queues bounded to an address, i.e.

    <address name="test">
       <multicast>
          <queue name="q1" />
          <queue name="q2" />
          <queue name="q3" />
       </multicast>
    </address>
    
    ./broker/bin/artemis producer --verbose --destination topic://test::q1 --user admin --password admin --protocol core --message-count 1
    
    |NAME              |ADDRESS           |CONSUMER|MESSAGE|MESSAGES|DELIVERING|MESSAGES|SCHEDULED| ROUTING |INTERNAL|
    |                  |                  | COUNT  | COUNT | ADDED  |  COUNT   | ACKED  |  COUNT  |  TYPE   |        |
    |q1                |test              |   0    |   1   |   2    |    0     |   0    |    0    |MULTICAST| false  |
    |q2                |test              |   0    |   0   |   1    |    0     |   0    |    0    |MULTICAST| false  |
    |q3                |test              |   0    |   0   |   1    |    0     |   0    |    0    |MULTICAST| false  |
    
    ./broker/bin/artemis producer --verbose --destination topic://test --user admin --password admin --protocol core --message-count 1
    
    |NAME              |ADDRESS           |CONSUMER|MESSAGE|MESSAGES|DELIVERING|MESSAGES|SCHEDULED| ROUTING |INTERNAL|
    |                  |                  | COUNT  | COUNT | ADDED  |  COUNT   | ACKED  |  COUNT  |  TYPE   |        |
    |q1                |test              |   0    |   2   |   1    |    0     |   0    |    0    |MULTICAST| false  |
    |q2                |test              |   0    |   1   |   1    |    0     |   0    |    0    |MULTICAST| false  |
    |q3                |test              |   0    |   1   |   1    |    0     |   0    |    0    |MULTICAST| false  |
    
    ./broker/bin/artemis consumer --verbose --destination queue://test::q1 --user admin --password admin --protocol core --message-count 1
    
    |NAME              |ADDRESS           |CONSUMER|MESSAGE|MESSAGES|DELIVERING|MESSAGES|SCHEDULED| ROUTING |INTERNAL|
    |                  |                  | COUNT  | COUNT | ADDED  |  COUNT   | ACKED  |  COUNT  |  TYPE   |        |
    |q1                |test              |   0    |   1   |   2    |    0     |   1    |    0    |MULTICAST| false  |
    |q2                |test              |   0    |   1   |   1    |    0     |   0    |    0    |MULTICAST| false  |
    |q3                |test              |   0    |   1   |   1    |    0     |   0    |    0    |MULTICAST| false  |
    
    ./broker/bin/artemis consumer --verbose --destination queue://test::q2 --user admin --password admin --protocol core --message-count 1
    
    |NAME              |ADDRESS           |CONSUMER|MESSAGE|MESSAGES|DELIVERING|MESSAGES|SCHEDULED| ROUTING |INTERNAL|
    |                  |                  | COUNT  | COUNT | ADDED  |  COUNT   | ACKED  |  COUNT  |  TYPE   |        |
    |q1                |test              |   0    |   1   |   2    |    0     |   1    |    0    |MULTICAST| false  |
    |q2                |test              |   0    |   0   |   1    |    0     |   0    |    0    |MULTICAST| false  |
    |q3                |test              |   0    |   1   |   1    |    0     |   0    |    0    |MULTICAST| false  |