DD.
DD.

Reputation: 21971

RabbitMQ messaging - initializing consumer

I want to use RabbitMQ to broadcast the state of an object continuously to any consumers which maybe listening. I want to set it up so when a consumer subscribes it will pick up the last available state... Is this possible?

Upvotes: 4

Views: 4687

Answers (2)

DD.
DD.

Reputation: 21971

Use a custom last value cache exchange: e.g. https://github.com/squaremo/rabbitmq-lvc-plugin

Last value caching exchange:

This is a pretty simple implementation of a last value cache using RabbitMQ's pluggable exchange types feature.

The last value cache is intended to solve problems like the following: say I am using messaging to send notifications of some changing values to clients; now, when a new client connects, it won't know the value until it changes.

The last value exchange acts like a direct exchange (binding keys are compared for equality with routing keys); but, it also keeps track of the last value that was published with each routing key, and when a queue is bound, it automatically enqueues the last value for the binding key.

Upvotes: 6

robthewolf
robthewolf

Reputation: 7624

It is possible with the Recent History Custom Exchange. It says that it will put the last 20 messages in the queue, so if it configurable you may be able to change that to the last 1 message and you are done.

If that doesn't work, ie the number is fixed at 20, then you may have to process the first 19 messages off the queue and take the status from the 20th. This is a bit of an annoying work around but as you know the parameter is always 20 this should be fine.

Finally if this doesn't suit you perhaps you will set you consumer to wait until the first status is receive, presuming that the status is broadcast reasonably frequently. Once the first status is received then start the rest of the application. I am assuming here that you need the status before doing something else.

Upvotes: 1

Related Questions