amqp v1.2.1 AMQP.Basic View Source
Functions to publish, consume and acknowledge messages.
Link to this section Summary
Functions
Acknowledges one or more messages. If multiple
is set to true
, all messages up to the one
specified by delivery_tag
are considered acknowledged by the server.
End a queue consumer.
Removes the return handler, if it exists. Does nothing if there is no such handler.
Registers a queue consumer process. The pid
of the process can be set using
the consumer_pid
argument and defaults to the calling process.
Polls a queue for an existing message.
Negative acknowledge of one or more messages. If multiple
is set to true
, all messages up to the
one specified by delivery_tag
are considered as not acknowledged by the server. If requeue
is set
to true
, the message will be returned to the queue and redelivered to the next available consumer.
Publishes a message to an Exchange.
Sets the message prefetch count or prefetech size (in bytes). If global
is set to true
this
applies to the entire Connection, otherwise it applies only to the specified Channel.
Asks the server to redeliver all unacknowledged messages on a specified channel.
Rejects (and, optionally, requeues) a message.
Registers a handler to deal with returned messages. The registered
process will receive {:basic_return, payload, meta}
data structures.
Link to this section Types
consumer_tag()
View Source
consumer_tag() :: String.t()
consumer_tag() :: String.t()
delivery_tag()
View Source
delivery_tag() :: integer()
delivery_tag() :: integer()
error()
View Source
error() :: {:error, reason :: :blocked | :closing}
error() :: {:error, reason :: :blocked | :closing}
exchange()
View Source
exchange() :: String.t()
exchange() :: String.t()
payload()
View Source
payload() :: String.t()
payload() :: String.t()
queue()
View Source
queue() :: String.t()
queue() :: String.t()
routing_key()
View Source
routing_key() :: String.t()
routing_key() :: String.t()
Link to this section Functions
ack(channel, delivery_tag, options \\ [])
View Source
ack(AMQP.Channel.t(), delivery_tag(), keyword()) :: :ok | error()
ack(AMQP.Channel.t(), delivery_tag(), keyword()) :: :ok | error()
Acknowledges one or more messages. If multiple
is set to true
, all messages up to the one
specified by delivery_tag
are considered acknowledged by the server.
cancel(channel, consumer_tag, options \\ [])
View Source
cancel(AMQP.Channel.t(), String.t(), keyword()) :: {:ok, String.t()} | error()
cancel(AMQP.Channel.t(), String.t(), keyword()) :: {:ok, String.t()} | error()
End a queue consumer.
This method cancels a consumer. This does not affect already delivered messages, but it does mean the server will not send any more messages for that consumer. The client may receive an arbitrary number of messages in between sending the cancel method and receiving the reply.
cancel_return(channel)
View Source
cancel_return(AMQP.Channel.t()) :: :ok
cancel_return(AMQP.Channel.t()) :: :ok
Removes the return handler, if it exists. Does nothing if there is no such handler.
consume(chan, queue, consumer_pid \\ nil, options \\ []) View Source
Registers a queue consumer process. The pid
of the process can be set using
the consumer_pid
argument and defaults to the calling process.
The consumer process will receive the following data structures:
{:basic_deliver, payload, meta}
- This is sent for each message consumed, wherepayload
contains the message content andmeta
contains all the metadata set when sending with Basic.publish or additional info set by the broker;{:basic_consume_ok, %{consumer_tag: consumer_tag}}
- Sent when the consumer process is registered with Basic.consume. The caller receives the same information as the return of Basic.consume;{:basic_cancel, %{consumer_tag: consumer_tag, no_wait: no_wait}}
- Sent by the broker when the consumer is unexpectedly cancelled (such as after a queue deletion){:basic_cancel_ok, %{consumer_tag: consumer_tag}}
- Sent to the consumer process after a call to Basic.cancel
get(channel, queue, options \\ []) View Source
Polls a queue for an existing message.
Returns the tuple {:empty, meta}
if the queue is empty or the tuple {:ok, payload, meta} if at least
one message exists in the queue. The returned meta map includes the entry message_count
with the
current number of messages in the queue.
Receiving messages by polling a queue is not as as efficient as subscribing a consumer to a queue, so consideration should be taken when receiving large volumes of messages.
Setting the no_ack
option to true will tell the broker that the receiver will not send an acknowledgement of
the message. Once it believes it has delivered a message, then it is free to assume that the consuming application
has taken responsibility for it. In general, a lot of applications will not want these semantics, rather, they
will want to explicitly acknowledge the receipt of a message and have no_ack
with the default value of false.
nack(channel, delivery_tag, options \\ [])
View Source
nack(AMQP.Channel.t(), delivery_tag(), keyword()) :: :ok | error()
nack(AMQP.Channel.t(), delivery_tag(), keyword()) :: :ok | error()
Negative acknowledge of one or more messages. If multiple
is set to true
, all messages up to the
one specified by delivery_tag
are considered as not acknowledged by the server. If requeue
is set
to true
, the message will be returned to the queue and redelivered to the next available consumer.
This is a RabbitMQ specific extension to AMQP 0.9.1. It is equivalent to reject, but allows rejecting
multiple messages using the multiple
option.
publish(channel, exchange, routing_key, payload, options \\ [])
View Source
publish(AMQP.Channel.t(), exchange(), routing_key(), payload(), keyword()) ::
:ok | error()
publish(AMQP.Channel.t(), exchange(), routing_key(), payload(), keyword()) :: :ok | error()
Publishes a message to an Exchange.
This method publishes a message to a specific exchange. The message will be routed to queues as defined by the exchange configuration and distributed to any subscribers.
The parameter exchange
specifies the name of the exchange to publish to. If set to
empty string, it publishes to the default exchange.
The routing_key
parameter specifies the routing key for the message.
The payload
parameter specifies the message content as a binary.
In addition to the previous parameters, the following options can be used:
Options
:mandatory
- If set, returns an error if the broker can't route the message to a queue (defaultfalse
);:immediate
- If set, returns an error if the broker can't deliver te message to a consumer immediately (defaultfalse
);:content_type
- MIME Content type;:content_encoding
- MIME Content encoding;:headers
- Message headers. Can be used with headers Exchanges;:persistent
- If set, uses persistent delivery mode. Messages marked aspersistent
that are delivered todurable
queues will be logged to disk;:correlation_id
- application correlation identifier;:priority
- message priority, ranging from 0 to 9;:reply_to
- name of the reply queue;:expiration
- how long the message is valid (in milliseconds);:message_id
- message identifier;:timestamp
- timestamp associated with this message (epoch time);:type
- message type as a string;:user_id
- creating user ID. RabbitMQ will validate this against the active connection user;:app_id
- publishing application ID.
Examples
iex> AMQP.Basic.publish chan, "my_exchange", "my_routing_key", "Hello World!", persistent: true
:ok
qos(channel, options \\ [])
View Source
qos(AMQP.Channel.t(), keyword()) :: :ok | error()
qos(AMQP.Channel.t(), keyword()) :: :ok | error()
Sets the message prefetch count or prefetech size (in bytes). If global
is set to true
this
applies to the entire Connection, otherwise it applies only to the specified Channel.
recover(channel, options \\ [])
View Source
recover(AMQP.Channel.t(), keyword()) :: :ok | error()
recover(AMQP.Channel.t(), keyword()) :: :ok | error()
Asks the server to redeliver all unacknowledged messages on a specified channel.
If requeue
is set to true
the server will attempt to requeue the message,
potentially delivering it to another subscriber. Otherwise it will be redelivered
to the original recipient.
reject(channel, delivery_tag, options \\ [])
View Source
reject(AMQP.Channel.t(), delivery_tag(), keyword()) :: :ok | error()
reject(AMQP.Channel.t(), delivery_tag(), keyword()) :: :ok | error()
Rejects (and, optionally, requeues) a message.
return(channel, return_handler_pid)
View Source
return(AMQP.Channel.t(), pid()) :: :ok
return(AMQP.Channel.t(), pid()) :: :ok
Registers a handler to deal with returned messages. The registered
process will receive {:basic_return, payload, meta}
data structures.