EAI Patterns with Actor Model: Message Expiration

If it is possible for a given message to become obsolete or in some way invalid due to a time laps, use a Message Expiration to control the timeout. While we have already dealt with the process timeouts in the Scatter-Gather implementation, this is different. A Message Expiration is used to determine when a single message has expired, rather than setting a limit on the completion of a larger process.

When using message-based middleware, it is possible to ask the messaging system itself to expire a message before it is ever delivered. Currently Akka does not support a mailbox that automatically detects expired messages. No worries, we can accomplish that on our own quite easily. We could create a custom mailbox type, or just place the expiration behavior on the message itself. There are advantages to both. I may implement a custom mailbox later (as suggested by Roland Kuhn in the comments), but for now will explain how to do this using a trait for messages. Whether or not the mailbox supports expiring messages, the message itself much supply some parts of the solution.

. . .

The full text is now available as part of my book Reactive Enterprise with Actor Model on Safari Books Online.


  1. Roland Kuhn (@rolandkuhn) says:

    While Akka does not come with such an implementation built-in you can also put the expiration checking into a custom mailbox implementation. It would make sense then to have a “trait ExpiringMessage { def isExpired: Boolean }” and the mailbox would then check during dequeue() and discard expired messages. When it comes to timestamps I would recommend using System.currentTimeMillis(), since that is defined to mean the same thing everywhere.

    • Thanks for your comments, Roland. I may augment the post later with a custom mailbox implementation. For now I have taken your advice to create a trait, but have currently applied it to messages themselves. Much appreciated.