core.messaging / MessagingService

# MessagingService

`interface MessagingService`

A MessagingService sits at the boundary between a message routing / networking layer and the core platform code.

A messaging system must provide the ability to send 1:many messages, potentially to an abstract "group", the membership of which is defined elsewhere. Messages are atomic and the system guarantees that a sent message <emph>eventually</emph> will arrive in the exact form it was sent, however, messages can be arbitrarily re-ordered or delayed.

Example implementations might be a custom P2P layer, Akka, Apache Kafka, etc. It is assumed that the message layer is <emph>reliable</emph> and as such messages may be stored to disk once queued.

### Properties

myAddress | `abstract val myAddress: SingleMessageRecipient` Returns an address that refers to this node.

### Functions

addMessageHandler | `abstract fun addMessageHandler(topic: String = "", executor: Executor? = null, callback: (Message, MessageHandlerRegistration) -> Unit): MessageHandlerRegistration` The provided function will be invoked for each received message whose topic matches the given string, on the given executor. The topic can be the empty string to match all messages.
createMessage | `abstract fun createMessage(topic: String, data: ByteArray): Message` Returns an initialised Message with the current time, etc, already filled in.
removeMessageHandler | `abstract fun removeMessageHandler(registration: MessageHandlerRegistration): Unit` Removes a handler given the object returned from addMessageHandler. The callback will no longer be invoked once this method has returned, although executions that are currently in flight will not be interrupted.

send

`abstract fun send(message: Message, target: MessageRecipients): Unit`

Sends a message to the given receiver. The details of how receivers are identified is up to the messaging implementation: the type system provides an opaque high level view, with more fine grained control being available via type casting. Once this function returns the message is queued for delivery but not necessarily delivered: if the recipients are offline then the message could be queued hours or days later.

stop | `abstract fun stop(): Unit`

### Extension Functions

runOnNextMessage

`fun MessagingService.runOnNextMessage(topic: String = "", executor: Executor? = null, callback: (Message) -> Unit): Unit`

Registers a handler for the given topic that runs the given callback with the message and then removes itself. This is useful for one-shot handlers that arent supposed to stick around permanently. Note that this callback doesnt take the registration object, unlike the callback to MessagingService.addMessageHandler.

send | `fun MessagingService.send(topic: String, obj: Any, to: MessageRecipients): Unit`

### Inheritors

ArtemisMessagingService

`class ArtemisMessagingService : MessagingService`

This class implements the MessagingService API using Apache Artemis, the successor to their ActiveMQ product. Artemis is a message queue broker and here, we embed the entire server inside our own process. Nodes communicate with each other using (by default) an Artemis specific protocol, but it supports other protocols like AQMP/1.0 as well.

InMemoryMessaging | `inner class InMemoryMessaging : MessagingService` An InMemoryMessaging provides a MessagingService that isnt backed by any kind of network or disk storage system, but just uses regular queues on the heap instead. It is intended for unit testing and developer convenience when all entities on the network are being simulated in-process.