Class InMemoryMailSink

  • All Implemented Interfaces:
    MailSink

    public final class InMemoryMailSink
    extends Object
    implements MailSink
    An in-memory implementation of MailSink that places messages into a list. This implementation is intended for testing purposes; it can be used for in-process testing by injecting this sink into the test case and examining the sent messages directly, or it can be used in integration testing by attaching an HTTP REST or similar endpoint to the output.
    Author:
    Christopher Smith
    • Constructor Detail

      • InMemoryMailSink

        public InMemoryMailSink()
    • Method Detail

      • getQueue

        public List<MailMessage> getQueue()
        Provide a reference to the queue. Access to this list is synchronized, but no guarantees are made about the visibility of specific messages. In particular, code reading the queue might miss messages that are delivered after a subsequent call to drainQueue(), and messages might be appended to this list after it is returned (potentially resulting in ConcurrentModificationException). Applications that need consistent ordering should generally drain the list and, if needed, append it to another data structure.
        Returns:
        a synchronized reference to the queue list
      • drainQueue

        public List<MailMessage> drainQueue()
        Atomically replace the queue with a new one and return the previous queue. This method is guaranteed to return all messages exactly once, and it is synchronized with the deliver(MailMessage) method.
        Returns:
        the queue as it appeared immediately before the call to this method