package amqp
Scala wrapper for interacting with AMQP, the aim is for convenient: - scala style usage - RPC calls - event hooks - reconnection strategies - message creation and extraction for common message types
Overview
Build connections with io.leonard.amqp.ConnectionHolder.builder
Create a connection:
val connection = ConnectionHolder.builder("amqps://guest:password@host:port") .eventHooks(EventHooks(eventListener)) .reconnectionStrategy(ReconnectionStrategy.JavaClientFixedReconnectDelay(1 second)) .build()
Create a channel:
val channel = connection.newChannel()
Create an RPC server listening on queue "queue.name", expecting a String and echoing it back:
def rpcHandler(request: Message): Future[Message] = request match { case Message.String(string) => Future(Message.String(string)) } val queue = QueueDeclare(Some("queue.name")) val rpcServerCloser = channel.rpcServer(queue, AckOnHandled)(rpcHandler)
Create an RPC client method which sends requests to the queue "queue.name" with a response timeout of 10 seconds :
val rpcClient = RPCClient(channel) val rpcMethod = rpcClient.newMethod(Exchange.Default.route("queue.name"), 10 second)
Create a consumer on "queue.name" printing out strings sent to it:
def consumer(request: Message): Unit = request match { case Message.String(string) => println(string) } val queue = QueueDeclare(Some("queue.name")) channel.addConsumer(queue, consumer)
Send a message to "queue.name":
channel.send(Exchange.Default.route("queue.name"), Message.String("message")
- Alphabetic
- By Inheritance
- amqp
- AnyRef
- Any
- Hide All
- Show All
- Public
- All
Type Members
-
class
ByteArray extends Traversable[Byte]
Wrapped immutable array of Bytes, produces a defensive copy of the passed array
-
trait
ChannelOwner extends AnyRef
Operation to perform on an amqp channel, the underlying connection may fail and be replaced by a new one with the same parameters
-
trait
Closeable extends AnyRef
Closeable for a handler of RPCs, close to stop the handler from being called
-
trait
ConnectionHolder extends AnyRef
Holds a connection, the underlying connection may be replaced if it fails
- trait ConnectionHolderBuilder extends AnyRef
- sealed abstract class DeliveryMode extends AnyRef
-
case class
Envelope(exchange: String, routingKey: String, message: Message) extends Product with Serializable
The message and any routing information that is included when it is received from a queue.
- sealed trait Event extends AnyRef
- trait EventHooks extends AnyRef
-
case class
Exchange(name: String) extends Product with Serializable
Describes an exchange which should already exist, an error will be thrown on use if it does not
Describes an exchange which should already exist, an error will be thrown on use if it does not
It is recommended to use ChannelOwner.declareExchange or ChannelOwner.declareExchangePassive to create this as they ensures the exchange exists.
- abstract class ExchangeType extends AnyRef
-
trait
ManualAcker extends AnyRef
Call to send Acks to Messages
-
class
Message extends AnyRef
Message blob with content headers
- class MessageProperties extends AnyRef
- case class Publish(routingDescriptor: RoutingDescriptor, message: Message) extends Product with Serializable
-
sealed
trait
Queue extends AnyRef
Defines a queue to connect to or create
-
case class
QueueDeclare(name: Option[String], durable: Boolean = false, exclusive: Boolean = false, autoDelete: Boolean = true, args: Map[String, AnyRef] = Map.empty) extends Queue with Product with Serializable
Parameters to create a new queue
-
case class
QueuePassive(name: String) extends Queue with Product with Serializable
Describes an exchange which should already exist, an error is thrown if it does not
- trait RPCClient extends AnyRef
-
trait
RPCMethod extends (Message) ⇒ Future[Message]
RPC method over AMQP.
RPC method over AMQP.
The future either completes with the correlating response message, an RPCTimeout or UndeliveredException
- Annotations
- @throws( ... ) @throws( ... )
- sealed trait ReconnectionStrategy extends AnyRef
-
case class
RoutingDescriptor(exchange: Exchange, routingKey: String, deliveryMode: Option[DeliveryMode], mandatory: Boolean, immediate: Boolean) extends Product with Serializable
Specifies routing and send parameters
Specifies routing and send parameters
- exchange
for the message to be sent to
- routingKey
the exchange uses this to decide which queue(s) the message is to be added to
- deliveryMode
defines whether a message should be persisted if the queue it is on is persisted
- mandatory
a message with this flag will be returned by the exchange if it finds that no queues match the routingKey
- immediate
a message with this flag will only be delivered if a matching queue has a ready consumer, if not it is returned
- sealed trait RpcServerAutoAckMode extends AnyRef
- trait ToMessage[-T] extends AnyRef
-
case class
UndeliveredException() extends Exception with Product with Serializable
The queue server found nowhere to route the message
Value Members
-
object
ByteArray
Produces an immutable array of bytes
-
object
ConnectionHolder
Create a connection using the builder:
Create a connection using the builder:
val connection = ConnectionHolder.builder("amqps://guest:password@host:port") .eventHooks(EventHooks(eventListener)) .reconnectionStrategy(ReconnectionStrategy.JavaClientFixedReconnectDelay(1 second)) .build()
-
object
DeliveryMode
DeliveryMode is a header on AMQP messages.
DeliveryMode is a header on AMQP messages. It ONLY has an effect on messages which arrive at DURABLE queues.
DURABLE queues are ones which are persistent themselves - they are recreated after a broker restart.
Persistent messages on DURABLE queues are also written to disk and will be recovered after a broker restart.
- object Event
-
object
EventHooks
Some basic strategies for handling events
- object Exchange extends Serializable
- object ExchangeType
-
object
Message
Message blob with content headers, usually you would use the child objects to construct / extract different types of messages
- object MessageProperties
- object RPCClient
-
object
ReconnectionStrategy
Strategies for reconnection, currently only fixed delay or no reconnection are available
-
object
RpcServerAutoAckMode
Some options for when the server will Ack the request message, clients only have the AckOnReceive auto Ack strategy