---
name: "AnyEvent::RabbitMQ::Channel"
section: "3pm"
platform: "linux"
package: "libanyevent-rabbitmq-perl"
version: "1.22~dfsg-1"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/AnyEvent::RabbitMQ::Channel"
---

# AnyEvent::RabbitMQ::Channel(3pm)

Abstraction of an AMQP channel.

## NAME

AnyEvent::RabbitMQ::Channel - Abstraction of an AMQP channel.

## SYNOPSIS

```
    my $ch = $rf->open_channel();
    $ch->declare_exchange(exchange => 'test_exchange');
```

## DESCRIPTION

A RabbitMQ channel.

A channel is a light-weight virtual connection within a TCP connection to a RabbitMQ broker.

## ARGUMENTS FOR "open\_channel"

- on\_close

  Callback invoked when the channel closes.  Callback will be passed the incoming message that caused the close, if any.

- on\_return

  Callback invoked when a mandatory or immediate message publish fails. Callback will be passed the incoming message, with accessors `"method_frame"`, `"header_frame"`, and `"body_frame"`.

## METHODS

### declare\_exchange (%args)

Declare an exchange (to publish messages to) on the server.

Arguments:

- on\_success

  - on\_failure

  - type

  Default 'direct'

- passive

  Default 0

- durable

  Default 0

- auto\_delete

  Default 0

- internal

  Default 0

- exchange

  The name of the exchange

### bind\_exchange

Binds an exchange to another exchange, with a routing key.

Arguments:

- source

  The name of the source exchange to bind

- destination

  The name of the destination exchange to bind

- routing\_key

  The routing key to bind with

### unbind\_exchange

### delete\_exchange

### declare\_queue

Declare a queue (create it if it doesn't exist yet) for publishing messages to on the server.

```
  my $done    = AnyEvent->condvar;
  $channel->declare_queue(
     exchange    => $queue_exchange,
     queue       => $queueName,
     durable     => 0,
     auto_delete => 1,
     passive     => 0,
     arguments   => { 'x-expires' => 0, },
     on_success  => sub { $done->send; },
     on_failure  => sub {
         say "Unable to create queue $queueName";
         $done->send;
     },
  );
  $done->recv;
```

Arguments:

- queue

  Name of the queue to be declared. If the queue name is the empty string, RabbitMQ will create a unique name for the queue. This is useful for temporary/private reply queues.

- on\_success

  Callback that is called when the queue was declared successfully. The argument to the callback is of type Net::AMQP::Frame::Method. To get the name of the Queue (if you declared it with an empty name), you can say

  ```
      on_success => sub {
          my $method = shift;
          my $name   = $method->method_frame->queue;
      };
  ```

- on\_failure

  Callback that is called when the declaration of the queue has failed.

- auto\_delete

  0 or 1, default 0

- passive

  0 or 1, default 0

- durable

  0 or 1, default 0

- exclusive

  0 or 1, default 0

- no\_ack

  0 or 1, default 1

- ticket

  default 0

- arguments

  `"arguments"` is a hashref of additional parameters which RabbitMQ extensions may use. This list is not complete and your RabbitMQ server configuration will determine which arguments are valid and how they act.

  - x-expires

    The queue will automatically be removed after being idle for this many milliseconds.

    Default of 0 disables automatic queue removal.

### bind\_queue

Binds a queue to an exchange, with a routing key.

Arguments:

- queue

  The name of the queue to bind

- exchange

  The name of the exchange to bind

- routing\_key

  The routing key to bind with

### unbind\_queue

### purge\_queue

Flushes the contents of a queue.

### delete\_queue

Deletes a queue. The queue may not have any active consumers.

### consume

Subscribe to consume messages from a queue.

Arguments:

- queue

  The name of the queue to be consumed from.

- on\_consume

  Callback called with an argument of the message which has been consumed.

  The message is a hash reference, where the value to key `"header"` is an object of type Net::AMQP::Protocol::Basic::ContentHeader, body is a Net::AMQP::Frame::Body, and `"deliver"` a Net::AMQP::Frame::Method.

- on\_cancel

  Callback called if consumption is cancelled.  This may be at client request or as a side effect of queue deletion.  (Notification of queue deletion is a RabbitMQ extension.)

- consumer\_tag

  Identifies this consumer, will be auto-generated if you do not provide it, but you must supply a value if you want to be able to later cancel the subscription.

- on\_success

  Callback called if the subscription was successful (before the first message is consumed).

- on\_failure

  Callback called if the subscription fails for any reason.

- no\_ack

  Pass through the `"no_ack"` flag. Defaults to `1`. If set to `1`, the server will not expect messages to be acknowledged.

### publish

Publish a message to an exchange.

Arguments:

- exchange

  The name of the exchange to send the message to.

- routing\_key

  The routing key with which to publish the message.

- header

  Hash of AMQP message header info, including the confusingly similar element "headers", which may contain arbitrary string key/value pairs.

- body

  The text body of the message to send.

- mandatory

  Boolean; if true, then if the message doesn't land in a queue (e.g. the exchange has no bindings), it will be "returned."  (see "on\_return")

- immediate

  Boolean; if true, then if the message cannot be delivered directly to a consumer, it will be "returned."  (see "on\_return")

- on\_ack

  Callback called with the frame that acknowledges receipt (if channel is in confirm mode), typically Net::AMQP::Protocol::Basic::Ack.

- on\_nack

  Callback called with the frame that declines receipt (if the channel is in confirm mode), typically Net::AMQP::Protocol::Basic::Nack or Net::AMQP::Protocol::Channel::Close.

- on\_return

  In AMQP, a "returned" message is one that cannot be delivered in compliance with the `"immediate"` or `"mandatory"` flags.

  If in confirm mode, this callback will be called with the frame that reports message return, typically Net::AMQP::Protocol::Basic::Return.  If confirm mode is off or this callback is not provided, then the channel or connection objects' on\_return callbacks (if any), will be called instead.

  NOTE: If confirm mode is on, the on\_ack or on\_nack callback will be called whether or not on\_return is called first.

### cancel

Cancel a queue subscription.

Note that the cancellation **will not** take place at once, and further messages may be consumed before the subscription is cancelled. No further messages will be consumed after the on\_success callback has been called.

Arguments:

- consumer\_tag

  Identifies this consumer, needs to be the value supplied when the queue is initially consumed from.

- on\_success

  Callback called if the subscription was successfully cancelled.

- on\_failure

  Callback called if the subscription could not be cancelled for any reason.

### get

Try to get a single message from a queue.

Arguments:

- queue

  Mandatory. Name of the queue to try to receive a message from.

- on\_success

  Will be called either with either a message, or, if the queue is empty, a notification that there was nothing to collect from the queue.

- on\_failure

  This callback will be called if an error is signalled on this channel.

- no\_ack

  0 or 1, default 1

### ack

### qos

### confirm

Put channel into confirm mode.  In confirm mode, publishes are confirmed by the server, so the on\_ack callback of publish works.

### recover

### select\_tx

### commit\_tx

### rollback\_tx

## AUTHOR, COPYRIGHT AND LICENSE

See AnyEvent::RabbitMQ for author(s), copyright and license.
