Вопрос

Is it possible to configure message queue not to accept messages if there are no currently registered listeners?

Solution I came up with consists of querying queue info via API and based on that deciding what to do. This solution seems to have a flaw, as it would not be transactional (edge cases exist in which message is published into empty queue, or I decide not to publish it, even though at the time of publishing consumer is already connected).

Это было полезно?

Решение

You can achieve this behavior by writing your own AcitveMQ Plugin. It gives you access to the current broker state, so that should eliminate the edge cases you stated.

However, I would suggest refraining from any such actions (no matter if they are client-controlled as you suggested, or server-controlled with a plugin). The reason is that a message to a queue without any consumers is not forwarded anyway. So at some point you have to pay the cost of detecting that there are no listeners (in fact, a dedicated client-request is probably more expensive than simply dropping the message off to the server).

If you're using durable queues, you're even violating their semantics.

If you just want to avoid clients receiving multiple messages upon connect, use topics with only one consumer per topic (and non-durable messages).

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top