|
| 1 | +# Testing JMS Queues with omq |
| 2 | + |
| 3 | +## Prerequisites |
| 4 | + |
| 5 | +- Tanzu RabbitMQ 4.3+ with the `rabbitmq_jms` plugin enabled |
| 6 | +- `omq` 0.45.0 or newer |
| 7 | + |
| 8 | +## Declaring JMS Queues |
| 9 | + |
| 10 | +Use `--queues jms` to declare a JMS queue. The `x-selector-fields` argument controls |
| 11 | +which message properties are held in memory for selector evaluation. Use `*` for all |
| 12 | +application properties. |
| 13 | + |
| 14 | +```bash |
| 15 | +# Declare a JMS queue with all application properties indexed |
| 16 | +omq amqp --queues jms \ |
| 17 | + --queue-args 'x-selector-fields=*' \ |
| 18 | + -t /queues/my-jms-queue -T /queues/my-jms-queue \ |
| 19 | + -x 1 -C 10 -y 0 |
| 20 | + |
| 21 | +# Declare with specific fields indexed (comma-separated) |
| 22 | +omq amqp --queues jms \ |
| 23 | + --queue-args 'x-selector-fields=color,region,priority' \ |
| 24 | + -t /queues/my-jms-queue -T /queues/my-jms-queue \ |
| 25 | + -x 1 -C 10 -y 0 |
| 26 | + |
| 27 | +# Declare with selector field max bytes limit |
| 28 | +omq amqp --queues jms \ |
| 29 | + --queue-args 'x-selector-fields=*,x-selector-field-max-bytes=128' \ |
| 30 | + -t /queues/my-jms-queue -T /queues/my-jms-queue \ |
| 31 | + -x 1 -C 10 -y 0 |
| 32 | +``` |
| 33 | + |
| 34 | +**Important**: `x-selector-fields` must be a list. When using `--queues jms`, omq |
| 35 | +automatically converts the comma-separated string to a list. If you declare the queue |
| 36 | +via other means, ensure this argument is a proper list (e.g. `["*"]`), not a plain string. |
| 37 | + |
| 38 | +## Publishing Messages with Application Properties |
| 39 | + |
| 40 | +Use `--amqp-app-property` to set application properties on messages. |
| 41 | +Values are cycled through comma-separated entries. |
| 42 | + |
| 43 | +```bash |
| 44 | +# Publish messages with different color property values |
| 45 | +omq amqp --queues predeclared \ |
| 46 | + -t /queues/my-jms-queue \ |
| 47 | + -x 1 -C 10 -y 0 \ |
| 48 | + --amqp-app-property 'color=red,blue,green,yellow' |
| 49 | + |
| 50 | +# Publish with multiple properties |
| 51 | +omq amqp --queues predeclared \ |
| 52 | + -t /queues/my-jms-queue \ |
| 53 | + -x 1 -C 10 -y 0 \ |
| 54 | + --amqp-app-property 'color=red,blue,green' \ |
| 55 | + --amqp-app-property 'size=small,large' |
| 56 | +``` |
| 57 | + |
| 58 | +## Consuming with JMS Message Selectors |
| 59 | + |
| 60 | +Use `--amqp-jms-selector` to filter messages using selector expressions. |
| 61 | + |
| 62 | +### Basic Equality |
| 63 | + |
| 64 | +```bash |
| 65 | +# Consume only red messages |
| 66 | +omq amqp --queues predeclared \ |
| 67 | + -T /queues/my-jms-queue \ |
| 68 | + -x 0 -y 1 -D 100 -z 10s \ |
| 69 | + --amqp-jms-selector "color = 'red'" |
| 70 | +``` |
| 71 | + |
| 72 | +### IS NULL / IS NOT NULL |
| 73 | + |
| 74 | +```bash |
| 75 | +# Consume messages where a property is set |
| 76 | +omq amqp --queues predeclared \ |
| 77 | + -T /queues/my-jms-queue \ |
| 78 | + -x 0 -y 1 -D 100 -z 10s \ |
| 79 | + --amqp-jms-selector "color IS NOT NULL" |
| 80 | +``` |
| 81 | + |
| 82 | +## Message Priority |
| 83 | + |
| 84 | +JMS queues support two priority levels: normal (0-4) and expedited (5-9). |
| 85 | +Higher priority messages are delivered first. |
| 86 | + |
| 87 | +```bash |
| 88 | +# Publish with mixed priorities |
| 89 | +omq amqp --queues jms \ |
| 90 | + --queue-args 'x-selector-fields=*' \ |
| 91 | + -t /queues/jms-priority -T /queues/jms-priority \ |
| 92 | + -C 100 -y 0 --detect-out-of-order-messages \ |
| 93 | + --message-priority '{{ randInt 0 10 }}' |
| 94 | + |
| 95 | +# Consume and observe priority ordering |
| 96 | +omq amqp --queues predeclared \ |
| 97 | + -T /queues/jms-priority \ |
| 98 | + -x 0 -D 100 -z 10s --log-level debug --detect-out-of-order-messages |
| 99 | +``` |
| 100 | + |
| 101 | +Note: if you want to use `--detect-out-of-order-messages` when consuming, it also |
| 102 | +has to be specified when publishing, since it sets annotations used for detecting |
| 103 | +out-of-sequence messages. |
| 104 | + |
| 105 | +Alternatively, you can skip this flag altogether and just look at the output - with |
| 106 | +`--log-level debug` omq prints the priority of messages it received. |
| 107 | + |
| 108 | +## Consumer Priority |
| 109 | + |
| 110 | +JMS queues support three consumer priority levels: -1, 0, and 1. |
| 111 | +Higher priority consumers receive messages first. |
| 112 | + |
| 113 | +```bash |
| 114 | +# Start priority 0 consumer |
| 115 | +omq amqp --queues jms -x 0 \ |
| 116 | + -T /queues/jms-consumer-priority \ |
| 117 | + --consumer-priority 0 |
| 118 | + |
| 119 | +# Start a publisher |
| 120 | +omq amqp --queues jms -y 0 \ |
| 121 | + -t /queues/jms-consumer-priority --rate 1 |
| 122 | + |
| 123 | +# Start a higher priority consumer - you should see that the previous consumer |
| 124 | +# no longer receives messages, while this higher-priority consumer takes over |
| 125 | +omq amqp --queues jms -x 0 \ |
| 126 | + -T /queues/jms-consumer-priority \ |
| 127 | + --consumer-priority 1 |
| 128 | +``` |
| 129 | + |
| 130 | +## Queue Overflow (reject-publish) |
| 131 | + |
| 132 | +JMS queues support `x-max-length` with `reject-publish` overflow strategy. |
| 133 | + |
| 134 | +```bash |
| 135 | +# Declare queue with max length 5 and reject-publish |
| 136 | +omq amqp --queues jms \ |
| 137 | + --queue-args 'x-selector-fields=*,x-max-length=5,x-overflow=reject-publish' \ |
| 138 | + -t /queues/jms-overflow -T /queues/jms-overflow \ |
| 139 | + -x 1 -C 20 -y 0 |
| 140 | +``` |
| 141 | + |
| 142 | +## AMQP 0-9-1 Interoperability |
| 143 | + |
| 144 | +Messages published via AMQP 0-9-1 with headers can be consumed via AMQP 1.0 |
| 145 | +with JMS selectors. AMQP 0-9-1 headers are exposed as application properties |
| 146 | +to AMQP 1.0 consumers. |
| 147 | + |
| 148 | +```bash |
| 149 | +# Publish with AMQP 0-9-1, consume with AMQP 1.0 JMS selector |
| 150 | +omq amqp091-amqp --queues jms \ |
| 151 | + --queue-args 'x-selector-fields=*' \ |
| 152 | + -t /queues/jms-interop -T /queues/jms-interop \ |
| 153 | + -x 1 --amqp091-header color=red,blue,green,yellow \ |
| 154 | + -y 1 --amqp-jms-selector "color = 'red'" |
| 155 | +``` |
| 156 | + |
| 157 | +The consumption rate should be roughly 25% of the publishing rate, |
| 158 | +since the consumer only consumes messages with one of the 4 color values. |
| 159 | + |
| 160 | +## Delayed Messages |
| 161 | + |
| 162 | +Publish messages with a 30 second delay: |
| 163 | +``` |
| 164 | +omq amqp --queues jms \ |
| 165 | + --queue-args 'x-selector-fields=*' \ |
| 166 | + -t /queues/jms-delay -T /queues/jms-delay \ |
| 167 | + -x 1 -C 100 -y 0 \ |
| 168 | + --amqp-msg-annotation 'x-opt-delivery-time={{ now | date_modify "+30s" | unixEpoch | mul 1000 }}' |
| 169 | +``` |
| 170 | + |
| 171 | +The `x-opt-delivery-time` value must be in **milliseconds** since the Unix epoch. |
| 172 | +`unixEpoch` sprig function returns seconds, so `| mul 1000` is required. |
| 173 | +Alternative expression to delay by 30s would be `{{ now | unixEpoch | add 30 | mul 1000 }}`. |
| 174 | + |
0 commit comments