JMS is a form of asynchronous messaging, and so publishers and subscribers are design-decoupled. This means that there is no mechanism for doing what you ask. For subscribers who are active at the time of publication, they will use the message without any chance of receiving a deletion message in time to act on it. If the subscriber is offline, they will, but asynchronous messages must be atomic. If you start developing the response of another respondent (create a message about deleting and ask to reconnect consumers to read the entire queue that requires deleting messages), then you will create a situation in which the behavior of the system will differ depending on whether the subscriber was online or not when a particular message / deletion combination was published. There is also a race condition in which the subscriber finishes reading the stored messages immediately before the publisher sends the delete message. This means that you must put significant logic in the subscribers to agree on these conditions and even more to agree on the state of the race.
The accepted way to do this is through so-called “countervailing transactions”. In any system where the manufacturer and the consumer do not share a single work unit or do not have a common state (for example, using the same database to store the state), a second transaction is required to return or correct the previous transaction, which changes the first. Of course, the consumer must correctly apply the compensation transaction. When this template is used, the result is that all subscribers exhibit the same behavior regardless of whether messages are consumed in real time or in a batch after a consumer reboot.
Please note that the compensation transaction is different from the “delete message”. The delete message proposed in the response of another respondent is a form of command and control that affects the flow of messages. On the other hand, countervailing transactions affect the state of the system through transactional updates to the state of the system.
As a rule, you never want to control the state of the system by controlling the flow of messages using the management and management functions. It is fragile, susceptible to attack and very difficult to audit or debug. Instead, create a system to deliver each message depending on the quality of service and processing of all messages. Changes in the state of the pens (including a change in the previous action) are completely in the application.
As an example, in banking, where transactions cause secondary effects, such as overdraft fees, the general procedure is to “replace” transactions throughout the day, and then sort and apply them in batches after the bank closes. This allows you to put up with the error before it leads to the collection of an overdraft. More recently, transactions are applied in real time, but triggers are held until the day books are closed, and this achieves the same result.
T.Rob source share