Package convex.net

Class MessageReceiver

java.lang.Object
convex.net.MessageReceiver

public class MessageReceiver extends Object
Class responsible for buffered accumulation of data received over a connection. ByteBuffers received must be passed in via @receiveFromChannel Passes any successfully received objects to a specified Consumer, using the same thread on which the MessageReceiver was called.

"There are only two hard problems in distributed systems: 2. Exactly-once delivery 1. Guaranteed order of messages 2. Exactly-once delivery"

- attributed to Mathias Verraes
  • Field Details

  • Constructor Details

  • Method Details

    • getReceiceAction

      public Consumer<Message> getReceiceAction()
    • getReceivedCount

      public long getReceivedCount()
      Get the number of messages received in total by this Receiver
      Returns:
      Count of messages received
    • receiveFromChannel

      public int receiveFromChannel(ReadableByteChannel chan) throws IOException, BadFormatException
      Handles receipt of bytes from a channel. Should be called with a ReadableByteChannel containing bytes received. May be called multiple times during receipt of a single message, i.e. can handle partial message receipt. Will consume enough bytes from channel to handle exactly one message. Bytes will be left unconsumed on the channel if more are available. This hopefully creates sufficient backpressure on clients sending a lot of messages.
      Parameters:
      chan - Byte channel
      Returns:
      The number of bytes read from the channel, or -1 if EOS
      Throws:
      IOException - If IO error occurs
      BadFormatException - If a bad encoding is received
    • setHook

      public void setHook(Consumer<Message> hook)
      Sets an optional additional message receiver hook (for debugging / observability purposes)
      Parameters:
      hook - Hook to call when a message is received