Content-based consumer priority in ActiveMQ - activemq

I'm working with consumers that need to load heavy resources to handle messages' content. Messages in the same family require the same resources.
I tried using message grouping to make sure messages in the same family are given to a consumer where the resources are already loaded. However I want the consumer have priority on messages in the same family, not exclusivity (loading resources takes time, treating messages takes even longer). For example, if consumers 1 and 2 are available then messages with ID group1 should always be given to consumer 1 and messages with ID group2 should always be given to consumer 2. If only consumer 2 is available, it should consume any message. That way, consumer 1 has priority on messages group1, but not exclusivity. Likewise consumer 2 has priority on messages group2, but not exclusivity.
Is there a way for message grouping in AMQ to not give exclusivity to consumer ? Or a way for consumer priority to be set dynamically based on content?
I'm using ActiveMQ 5.16.2, but I can switch to Artemis if that is preferable.

In this use case, I suggest filtering over message groups. I would contend this you are seeking to partition the message flow, where the competing consumer pattern provided by message groups is not meeting your need. If you require these flows to be handled by a specific consumer, it is practical that you would eventually want to have that consumer have a HA-standby or parallel consumer-- which would be an additional driver to partition or filter the traffic.
There are two approaches-- you can do server-driven config using Virtual Destinations (via CompositeQueue w/ filtering, etc) or client-driven config using Virtual Topics and selectors.
Here is the client-driven approach.
Publish to a Virtual Topic (ie. topic://VT.ORDER.EVENT)
Consume from Virtual Consumer queue(s) (ie. queue://VQ.APP1.VT.ORDER.EVENT, queue://VQ.APP2.VT.ORDER.EVENT)
Have consumers connect with selector
Each group of messages will arrive to their own queue, and you can have 1 .. n consumers on each queue.
Bonus: You can still have consumers monitor the full traffic flow by registering a wildcard selector or subscribing directly to the topic.
<destinationInterceptors>
<virtualDestinationInterceptor>
<virtualDestinations>
<virtualTopic name="VT.>" prefix="VQ.*." selectorAware="true"/>
</virtualDestinations>
</virtualDestinationInterceptor>
</destinationInterceptors>
Persist selectors:
<plugins>
<virtualSelectorCacheBrokerPlugin persistFile="<some path>${activemq.data}/virtual-topic-selectorcache.data" />
</plugins>
ref: Virtual Topics

The whole point of message grouping is to serialize the consumption of messages. The simplest way to do that is to that is to ensure all the messages in the group are dispatched to the same consumer. Therefore, there is no way for message grouping to not give exclusivity to consumer as that would defeat the purpose of grouping in the first place. That said, a single consumer can receive messages from multiple groups concurrently. This is happens when there are more groups than consumers.
It's also worth noting that you can't control which consumer consumes which group. The point of grouping is just that one consumer gets the messages from the group, but not which one.
Consumer priority is set by the consumer itself as the documentation describes. It is not based on the content of the message being dispatched.
You might try setting a property on the messages which reflect what "family" they are in and then your consumers can use selectors to consume the messages they care about.
Keep in mind that you can always have multiple consumers in your client application. Each client could potentially have two consumers - a high priority consumer with a selector and then a lower priority consumer without a selector. The broker would prioritize the consumers with selectors but if there was no match it would dispatch messages to the consumers without selectors.

Related

How do I achieve one by one message delivery for each user individually (in parallel)?

I need a queue/message broker that would allow me to receive by 1 message for each user.
Previously I've been using FIFO SQS and each user has its own message group id, which allows me to have 1 message inflight for each user.
But I've ended up with a problem when I have more than 20.000 messages in the queue, because A FIFO queue looks through the first 20k messages to determine available message groups, which makes all messages above the 20.000 limit to be unavailable for processing.
Do you guys have any advice for a message broker/queue that would allow me to achieve the same behavior without having a limit of 20.000 messages?
I was thinking about using Redis Lists (where each user has its own list and I do an RPOP for each existent list)
A couple options to overcome the consumer window problem you describe in ActiveMQ:
Use Virtual Topics and have consumers register a selector. The broker will filter messages to each consumer into their own queue. This does not require any broker changes and is completely dynamic via the client setting the selector.
Use Composite Destinations to filter messages to separate queues. This is static broker routing and requires server-side config change for any new routing rules. The clients do not need to be changed
Both approaches documented here:
ref: https://activemq.apache.org/virtual-destinations

When message will be erased from queue?

Let's suppose we have one producer, one queue and some consumers which are subscribed on queue.
Producer -> Queue -> Consumers
Queues contains messages about life events. These messages should receive all consumers.
When queue will be erased?
When all consumers get message?
Or when one of consumers confirm message with flag ack (true)?
And how to manage priority, who from consumers must to get message first/last (don't confuse with message priority).
As instance I have 10 consumers and I want that the fifth consumer get message first, remaining consumers later after specified time.
Be careful: when there are many consumers on one queue, only one of them will receive a given message, provided that it is consumed and acked properly. You need to bind as many queues as consumers to an exchange to have all consumers receive the message.
For your priority question, there is no built-in mecanism to have consumers receive the same message with a notion of priority: consumer priority exists (see https://www.rabbitmq.com/consumer-priority.html), but it is made to have consumer receive a given message before the others on a given queue, so the other consumers won't receive this message. It you need to orchestrate the delivery of your messages, you have to think of a more complex system (maybe a saga or a resequencer?).
Note that you can delay messages using this pattern. Again, this requires having multiple queues.
Finally, there are many scenarios when a queue is deleted. Take a look at the documentation, these are well explained.

Rabbitmq : Prioritize consuming messages from multiple queues

If I have two queues from which I want to consume messages, and I use a single SimpleMessageQueueListenerContainer for it, in which order would the listeners be invoked/messages consumed when both queues have messages?
I will try to be more specific of the problem I am working on:
I have a consumer application which needs to consume messages from 2 queues – say regular-jobs-queue and infrequent-jobs-queue. If there are any messages in ‘infrequent-jobs-queue’ I want to consume those before consuming messages from ‘regular-jobs-queue’. I might not be able to combine these and put all messages into a single rabbitmq level priority queue and assign higher priority to infrequent-job message because of some upcoming use-cases like purging regular-jobs without affecting infrequent-jobs and others.
I am aware that RabbitMQ has support for consumer priority but I am not very sure if it will be applicable here. I want all instances of my consumer application to first consume messages of infrequent-jobs-queue if any and not prioritize amongst these consumers.
Or should I like have 2 containers, with dedicated consumer thread(s) per queue and have an internal priority-queue data structure into which I can put messages as and when consumed from rabbitmq queue.
Any help would be really appreciated. Thanks.
~Rashida
You can't do what you want; messages will be delivered with equal priority.
Moving them to an internal in-memory queue will risk message loss.
You might want to consider using one of the RabbitTemplate.receive() or receiveAndConvert() methods instead of a message-driven container.
That way you have complete control.

RabbitMQ distributing messages unevenly to consumers

We're seeing an issue where consumers of our message queues are picking up messages from queues at the top of the alphabetical range. We have two applications: a producer, and a subscriber. We're using RabbitMQ 3.6.1.
Let's say that the message queues are setup like so:
Our first application, the producer, puts say 100 messages/second onto each queue:
Our second application, the subscriber, has five unique consumer methods that can deal with messages on each respective queue. Each method binds to it's respective queue. A subscriber has a prefetch of 1 meaning it can only hold one message at a time, regardless of queue. We may run numerous instances of the subscriber like so:
So the situation is thus: each queue is receiving 100 msg/sec, and we have four instances of subscriber consuming these messages, so each queue has four consumers. Let's say that the consumer methods can deal with 25 msg/sec each.
What happens is that instead of all the queues being consumed equally, the alphabetically higher queues instead get priority. It's seems as though when the subscriber becomes ready, RabbitMQ looks down the list of queues that this particular ready channel is bound to, and picks the first queue with pending messages.
In our situation, A_QUEUE will have every message consumed. B_QUEUE may have some consumed in certain race conditions, but C_QUEUE/D_QUEUE and especially E_QUEUE will rarely get touched.
If we turn off the publisher, the queues will eventually drain, top to bottom.
Is it possible to configure either RabbitMQ itself or possibly even the channel to use some sort of round robin distribution policy or maybe even random policy so that when a channel has numerous bound queues, all with messages pending, the distribution is even?
to clarify: you have a single subscriber application with multiple consumers in it, right?
I'm guessing you're using a single RabbitMQ Connection within the subscriber app.
Are you also re-using a single RabbitMQ Channel for all of your consumers? If so, that would be a problem. Be sure to use a new Channel for each consumer you start.
Maybe the picture is wrong, but if it's not then your setup is wrong. You don't need 4 queues if you are going to have subscribers that listen to each and every queue. You'd just need one queue, that has multiple instances of the same subscriber consuming from it.
Now to answer, yes (but no need to configure, as long as prefetch is 1), actually rabbitmq does distribute messages evenly. You can find about about that here, and on the same place actually how your setup should look like. Here is a quote from the link.
RabbitMQ just dispatches a message when the message enters the queue.
It doesn't look at the number of unacknowledged messages for a
consumer. It just blindly dispatches every n-th message to the n-th
consumer.

How do I work with Message Groups in ActiveMQ

I am attempting to use ActiveMQ 5.8.0 message groups in my application, and am not getting the results that I expected after reading the documentation.
I start two (or more) consumers for a particular queue, and then I send messages to the queue. In the producer's createMessage method, I am using:
message.setStringProperty("JMSXGroupID", "foo");
to set the GroupID. Note that for testing purposes, I am hard-coding "foo". It will eventually be a string set by the producer.
Since I only have a single message group being set in my messages, I expected to see that one consumer would become active and consume all of the messages in the queue, while the other one would remain quiescent.
Instead, I see the first message get processed by the first consumer, and then the second message get processed by the second consumer after the first consumer is finished. The consumers continue to take turns in this manner until all of the messages are consumed.
Is this the expected behavior, or is there some additional configuration that I need to do on either ActiveMQ or my producer or consumer to make sure that each GroupID gets associated to a single consumer.
AMQ message groups just guarantees that a single consumer will be active for a given group ("foo", etc) at a time...it doesn't bind that group to a specific consumer, so AMQ's internal consumer load balancing is likely just alternating between consumers.
regardless, this shouldn't be an issue when you are using dynamic JMSXGroupID values with multiple consumers, etc.