amqp API reference
An AMQP 0-9-1 client over net for RabbitMQ and compatible brokers: the connection handshake (SASL PLAIN), classic and quorum queues, exchanges and bindings, publishing with properties, publisher confirms and mandatory publishing, pulling with get, and server-pushed deliveries with consume / receiveDelivery (prefetch with qos).
One channel (1), heartbeats disabled. Every read has a 30 s idle timeout except the wait for the next delivery. Do not issue other calls on a Conn while a consumer is active: a delivery arriving in place of a reply is a protocol error. A protocol error or a broker Close throws an amqp.AmqpError (kind "amqp"); a broker Close carries its replyCode (404 NOT_FOUND, 403 ACCESS_REFUSED, 406 PRECONDITION_FAILED, ...), replyText and whether it closed the channel or the connection; bad credentials are a connection close with 403 ACCESS_REFUSED. A socket failure (refused, reset) is a net.NetError (kind "net"), a stall kind "timeout", a connection the broker drops mid-frame kind "closed". Needs the default jennifer binary (net).
Import with import "amqp.j" as amqp;. See the amqp guide for prose and examples.
Contents
| Name | What it is |
|---|---|
ack | Acknowledge a delivered message by its tag. |
bindQueue | Bind a queue to an exchange with a routing key. |
cancelConsume | Cancel a subscription started with consume (Basic.Cancel). |
close | Close the connection cleanly (Connection.Close) and shut the socket. |
confirmSelect | Put the channel into publisher-confirm mode (Confirm.Select). |
connect | Connect to a broker and open a channel. |
consume | Start a server-pushed subscription with Basic.Consume and return the consumer tag (the broker generates one). |
declareExchange | Declare an exchange (idempotent, durable). |
declareQueue | Declare a queue (idempotent). |
declareQuorumQueue | Declare a quorum queue: a Raft-replicated queue type for high availability and data safety. |
get | Pull the next message from a queue with Basic.Get. |
nack | Negatively acknowledge a delivered message (Basic.Nack). |
options | Default options for a broker: port 5672, vhost "/". |
publish | Publish a message body to an exchange with a routing key. |
publishMandatory | Publish a message with the mandatory flag: a message no queue takes (no binding matches its routing key) co... |
publishText | Publish a text message (UTF-8). |
publishWith | Publish a message body carrying message properties (content-type, persistence, correlation-id, reply-to). |
qos | Limit how many unacknowledged deliveries the broker pushes to each consumer started on this channel afterwa... |
receiveDelivery | Block for the next server-pushed message (Basic.Deliver + content header + body frame(s)) and return it as ... |
waitConfirm | Block for the broker's confirmation of the oldest publish not yet reported (only valid after confirmSelect)... |
withPort | Copy options with a different port. |
withVhost | Copy options with a different virtual host. |
AmqpError | struct: A failed AMQP operation. |
Conn | struct: An open connection (single channel). |
Delivery | struct: A message pushed by the broker via Basic.Deliver (returned by receiveDelivery). |
Message | struct: A message pulled with get. |
Options | struct: Connection options. |
Properties | struct: Message properties carried on a publish (all optional; a "" string or a false flag omits that property from... |
QueueInfo | struct: Queue metadata returned by declareQueue. |
Functions
ack
amqp.ack(c as Conn, deliveryTag as int)Acknowledge a delivered message by its tag.
c- the connection
deliveryTag- the delivery tag from a got Message
bindQueue
amqp.bindQueue(
c as Conn,
queue as string,
exchange as string,
routingKey as string)Bind a queue to an exchange with a routing key. For a "fanout" exchange the routing key is ignored; for "direct" it is matched exactly; for "topic" it is a pattern (* / #).
c- the connection
queue- the queue name
exchange- the exchange name
routingKey- the binding routing key / pattern
- Throws
AmqpError - kind "amqp" on failure
cancelConsume
amqp.cancelConsume(c as Conn, consumerTag as string)Cancel a subscription started with consume (Basic.Cancel). After this the broker sends no further deliveries for that consumer tag.
c- the connection
consumerTag- the consumer tag returned by consume
- Throws
AmqpError - kind "amqp" on failure
close
amqp.close(c as Conn)Close the connection cleanly (Connection.Close) and shut the socket. Frames the broker sent before it saw the Close are read and discarded on the way to Close-Ok: publisher confirms nobody waited for, returned messages, and deliveries to a consumer still active. Any other method is a protocol error.
c- the connection
- Throws
AmqpError - kind "amqp" on an unexpected reply
confirmSelect
amqp.confirmSelect(c as Conn)Put the channel into publisher-confirm mode (Confirm.Select). After this, each publish is confirmed by the broker; call waitConfirm once per publish to block for that confirmation. A skipped waitConfirm leaves its confirm unread: the next waitConfirm returns that older confirm, and any other call that reads a reply (declare, get, consume, ...) fails on it; only close discards it. Publishing a batch first and then calling waitConfirm once per publish works too. Idempotent.
c- the connection
- Throws
AmqpError - kind "amqp" on failure
connect
amqp.connect(opts as Options)Connect to a broker and open a channel.
opts- the connection options
- Returns
Conn - the open connection
- Throws
AmqpError - kind "amqp" on a handshake failure
consume
amqp.consume(c as Conn, queue as string, autoAck as bool)Start a server-pushed subscription with Basic.Consume and return the consumer tag (the broker generates one). Follow with receiveDelivery in a loop - wrap it in a spawn so the pushed deliveries do not block the rest of the program. When autoAck is false, ack (or nack) each Delivery by its deliveryTag.
c- the connection
queue- the queue name
autoAck- whether the broker should auto-acknowledge each delivery
- Returns
string - the consumer tag (pass to cancelConsume)
- Throws
AmqpError - kind "amqp" on failure
declareExchange
amqp.declareExchange(c as Conn, name as string, exType as string)Declare an exchange (idempotent, durable). exType is "direct", "topic", or "fanout".
c- the connection
name- the exchange name
exType- the exchange type ("direct" / "topic" / "fanout")
- Throws
AmqpError - kind "amqp" on failure
declareQueue
amqp.declareQueue(c as Conn, name as string, durable as bool)Declare a queue (idempotent). durable survives a broker restart.
c- the connection
name- the queue name ("" for a server-generated name)
durable- whether the queue is durable
- Returns
QueueInfo - the queue name and message / consumer counts
- Throws
AmqpError - kind "amqp" on failure
declareQuorumQueue
amqp.declareQuorumQueue(c as Conn, name as string)Declare a quorum queue: a Raft-replicated queue type for high availability and data safety. Sets the x-queue-type=quorum argument. Quorum queues are always durable (there is no durable flag) and cannot be server-named, so name must be non-empty. Re-declaring must use the same type, so do not also declare the same name as a classic queue.
c- the connection
name- the queue name (non-empty)
- Returns
QueueInfo - the queue name and message / consumer counts
- Throws
AmqpError - kind "amqp" on an empty name or a declare failure
get
amqp.get(c as Conn, queue as string, autoAck as bool)Pull the next message from a queue with Basic.Get. When autoAck is false, ack the returned message with ack.
c- the connection
queue- the queue name
autoAck- whether the broker should auto-acknowledge
- Returns
Message - the message (its
emptyfield is true when none was ready) - Throws
AmqpError - kind "amqp" on failure
nack
amqp.nack(c as Conn, deliveryTag as int, requeue as bool)Negatively acknowledge a delivered message (Basic.Nack). With requeue true the broker re-queues the message for redelivery; with false it is dropped (or dead-lettered if configured). Nacks a single message (not "multiple").
c- the connection
deliveryTag- the delivery tag from a Message / Delivery
requeue- whether the broker should re-queue the message
options
amqp.options(host as string, user as string, password as string)Default options for a broker: port 5672, vhost "/".
host- the broker host
user- the username
password- the password
- Returns
Options - the options
publish
amqp.publish(
c as Conn,
exchange as string,
routingKey as string,
body as bytes)Publish a message body to an exchange with a routing key. Use exchange "" for the default exchange (routing key = queue name).
c- the connection
exchange- the exchange name ("" for the default)
routingKey- the routing key
body- the message body
- Throws
AmqpError - kind "amqp" on failure
publishMandatory
amqp.publishMandatory(
c as Conn,
exchange as string,
routingKey as string,
body as bytes,
props as Properties)Publish a message with the mandatory flag: a message no queue takes (no binding matches its routing key) comes back to the publisher as a Basic.Return instead of being dropped. Use it in confirm mode: the return arrives before the publish's confirm, and waitConfirm throws it as an AmqpError with scope "return" and replyCode 312 (NO_ROUTE). Outside confirm mode nothing waits for the return, and the next call that reads a reply fails on it.
c- the connection
exchange- the exchange name ("" for the default)
routingKey- the routing key
body- the message body
props- the message properties
- Throws
AmqpError - kind "amqp" on failure
publishText
amqp.publishText(
c as Conn,
exchange as string,
routingKey as string,
text as string)Publish a text message (UTF-8). Convenience over publish.
c- the connection
exchange- the exchange name ("" for the default)
routingKey- the routing key
text- the message text
- Throws
AmqpError - kind "amqp" on failure
publishWith
amqp.publishWith(
c as Conn,
exchange as string,
routingKey as string,
body as bytes,
props as Properties)Publish a message body carrying message properties (content-type, persistence, correlation-id, reply-to). Otherwise identical to publish. Build the property set with Properties{...}; an unset field ("" / false) is omitted from the wire.
c- the connection
exchange- the exchange name ("" for the default)
routingKey- the routing key
body- the message body
props- the message properties
- Throws
AmqpError - kind "amqp" on failure
qos
amqp.qos(c as Conn, prefetchCount as int)Limit how many unacknowledged deliveries the broker pushes to each consumer started on this channel afterwards (Basic.Qos prefetch-count). Call it before consume: without it the broker pushes the whole queue at once, and a consumer that crashes holds every message unacked until its connection closes. 0 removes the limit. Does not affect get.
c- the connection
prefetchCount- the most unacked deliveries per consumer, 0 to 65535 (0 = no limit)
- Throws
AmqpError - kind "amqp" on a count out of range or a refusal
receiveDelivery
amqp.receiveDelivery(c as Conn)Block for the next server-pushed message (Basic.Deliver + content header + body frame(s)) and return it as a Delivery. Only valid after consume. This is the cooperative receive loop: there is no callback - the app calls receiveDelivery repeatedly, typically from inside its own spawn, and acts on each Delivery.
c- the connection
- Returns
Delivery - the next delivered message
- Throws
AmqpError - kind "amqp" on a non-delivery frame or a dropped connection
waitConfirm
amqp.waitConfirm(c as Conn)Block for the broker's confirmation of the oldest publish not yet reported (only valid after confirmSelect) and report it. Returns true on Basic.Ack (the broker took responsibility for the message) and false on Basic.Nack (the broker could not). One Basic.Ack / Basic.Nack with multiple set confirms every publish up to its delivery tag; the later calls return those results without reading. Calling it with no publish outstanding is an error. A publishMandatory message the broker returned (no queue took it) throws once its confirm has been read, so the channel stays usable.
c- the connection
- Returns
bool - true when confirmed, false when nacked
- Throws
AmqpError - kind "amqp": scope "return" (312 NO_ROUTE) for a returned message
withPort
amqp.withPort(o as Options, port as int)Copy options with a different port.
o- the options
port- the port
- Returns
Options - a fresh options
withVhost
amqp.withVhost(o as Options, vhost as string)Copy options with a different virtual host.
o- the options
vhost- the virtual host
- Returns
Options - a fresh options
Structs
AmqpError
A failed AMQP operation. Every error of kind "amqp" is one, so its fields are safe to read once the kind matches.
def struct AmqpError {
kind as string,
message as string,
scope as string,
replyCode as int,
replyText as string,
classId as int,
methodId as int
};kind- always
"amqp" messageamqp:and what failed, with the broker's reply code and text when it closedscope"channel"or"connection"when the broker closed it,"return"when it returned a mandatory message; "" for any other failurereplyCode- the broker's reply code (404 NOT_FOUND, 403 ACCESS_REFUSED, 406 PRECONDITION_FAILED, 320 CONNECTION_FORCED, 312 NO_ROUTE for a return, ...); 0 when the broker did not close
replyText- the broker's reply text, "" when none
classId- the class of the method the broker refused; 0 when it named none
methodId- the method the broker refused; 0 when it named none
Conn
An open connection (single channel).
def struct Conn {
socket as net.Conn,
channel as int,
frameMax as int,
confirms as channel of ConfirmState
};socket- the underlying connection
channel- the channel number (always 1)
frameMax- the negotiated maximum frame size (bytes)
confirms- the publisher-confirm state, shared by every copy
Delivery
A message pushed by the broker via Basic.Deliver (returned by receiveDelivery).
def struct Delivery {
consumerTag as string,
deliveryTag as int,
redelivered as bool,
exchange as string,
routingKey as string,
body as bytes
};consumerTag- the consumer tag the delivery is for
deliveryTag- the delivery tag (pass to ack / nack)
redelivered- true if the broker has delivered this message before
exchange- the exchange the message came from
routingKey- the routing key
body- the message body
Message
A message pulled with get.
def struct Message {
empty as bool,
deliveryTag as int,
exchange as string,
routingKey as string,
body as bytes
};empty- true when the queue had no message (the other fields are zero)
deliveryTag- the delivery tag (pass to ack)
exchange- the exchange the message came from
routingKey- the routing key
body- the message body
Options
Connection options.
def struct Options {
host as string,
port as int,
user as string,
password as string,
vhost as string,
security as transport.Security
};host- the broker host
port- the broker port (5672 by default)
user- the username
password- the password
vhost- the virtual host ("/" by default)
securitytransport.Security.None(plaintext AMQP, the default) or.Tls(AMQPS - TLS on connect, verifying the broker certificate);.Starttlsis rejected (AMQP has no in-band upgrade)
Properties
Message properties carried on a publish (all optional; a "" string or a false flag omits that property from the wire, keeping the content header minimal).
def struct Properties {
contentType as string,
persistent as bool,
correlationId as string,
replyTo as string
};contentType- the MIME content-type (e.g. "application/json"); "" omits it
persistent- true sets delivery-mode 2 (persistent); false omits it (transient)
correlationId- the correlation id (request / reply matching); "" omits it
replyTo- the reply-to queue / routing key; "" omits it
QueueInfo
Queue metadata returned by declareQueue.
def struct QueueInfo {
name as string,
messageCount as int,
consumerCount as int
};name- the queue name (the server's, for a server-named queue)
messageCount- the number of ready messages
consumerCount- the number of consumers