Mojo::RabbitMQ::Client::Channel(3pm) | User Contributed Perl Documentation | Mojo::RabbitMQ::Client::Channel(3pm) |
Mojo::RabbitMQ::Client::Channel - handles all channel related methods
use Mojo::RabbitMQ::Client::Channel; my $channel = Mojo::RabbitMQ::Client::Channel->new(); $channel->catch(sub { warn "Some channel error occurred: " . $_[1] }); $channel->on( open => sub { my ($channel) = @_; ... } ); $channel->on(close => sub { warn "Channel closed" }); $client->open_channel($channel);
Mojo::RabbitMQ::Client::Channel allows one to call all channel related methods.
Mojo::RabbitMQ::Client::Channel inherits all events from Mojo::EventEmitter and can emit the following new ones.
$channel->on(open => sub { my ($channel) = @_; ... });
Emitted when channel receives Open-Ok.
$channel->on(close=> sub { my ($channel, $frame) = @_; ... });
Emitted when channel gets closed, "<$frame"> contains close reason.
Mojo::RabbitMQ::Client::Channel has following attributes.
my $id = $channel->id; $channel->id(20810);
If not set, Mojo::RabbitMQ::Client sets it to next free number when channel is opened.
$channel->is_open ? "Channel is open" : "Channel is closed";
$channel->is_active ? "Channel is active" : "Channel is not active";
This can be modified on reception of Channel-Flow.
my $client = $channel->client; $channel->client($client);
Mojo::RabbitMQ::Client::Channel inherits all methods from Mojo::EventEmitter and implements the following new ones.
$channel->close;
Cancels all consumers and closes channel afterwards.
my $exchange = $channel->declare_exchange( exchange => 'mojo', type => 'fanout', durable => 1, ... )->deliver;
Verify exchange exists, create if needed.
This method creates an exchange if it does not already exist, and if the exchange exists, verifies that it is of the correct and expected class.
Following arguments are accepted:
Same as declare_exchange but auto-delivers method and returns a Mojo::Promise object.
$channel->declare_exchange_p( exchange => 'mojo', type => 'fanout', durable => 1, ... )->then(sub { say "Exchange declared..."; })->catch(sub { my $err = shift; warn "Exchange declaration error: $err"; })->wait;
$channel->delete_exchange(exchange => 'mojo')->deliver;
Delete an exchange.
This method deletes an exchange. When an exchange is deleted all queue bindings on the exchange are cancelled.
Following arguments are accepted:
Same as delete_exchange but auto-delivers method and returns a Mojo::Promise object.
$channel->delete_exchange_p( exchange => 'mojo' )->then(sub { say "Exchange deleted..."; })->catch(sub { my $err = shift; warn "Exchange removal error: $err"; })->wait;
my $queue = $channel->declare_queue(queue => 'mq', durable => 1)->deliver
Declare queue, create if needed.
This method creates or checks a queue. When creating a new queue the client can specify various properties that control the durability of the queue and its contents, and the level of sharing for the queue.
Following arguments are accepted:
Same as declare_queue but auto-delivers method and returns a Mojo::Promise object.
$channel->declare_queue_p( queue => 'mq', durable => 1 )->then(sub { say "Queue declared..."; })->catch(sub { my $err = shift; warn "Queue declaration error: $err"; })->wait;
$channel->bind_queue( exchange => 'mojo', queue => 'mq', routing_key => '' )->deliver;
Bind queue to an exchange.
This method binds a queue to an exchange. Until a queue is bound it will not receive any messages. In a classic messaging model, store-and-forward queues are bound to a direct exchange and subscription queues are bound to a topic exchange.
Following arguments are accepted:
Same as bind_queue but auto-delivers method and returns a Mojo::Promise object.
$channel->bind_queue_p( exchange => 'mojo', queue => 'mq', routing_key => '' )->then(sub { say "Queue bound..."; })->catch(sub { my $err = shift; warn "Queue binding error: $err"; })->wait;
$channel->unbind_queue( exchange => 'mojo', queue => 'mq', routing_key => '' )->deliver;
Unbind a queue from an exchange.
This method unbinds a queue from an exchange.
Following arguments are accepted:
Same as unbind_queue but auto-delivers method and returns a Mojo::Promise object.
$channel->unbind_queue_p( exchange => 'mojo', queue => 'mq', routing_key => '' )->then(sub { say "Queue unbound..."; })->catch(sub { my $err = shift; warn "Queue unbinding error: $err"; })->wait;
$channel->purge_queue(queue => 'mq')->deliver;
Purge a queue.
This method removes all messages from a queue which are not awaiting acknowledgment.
Following arguments are accepted:
Same as purge_queue but auto-delivers method and returns a Mojo::Promise object.
$channel->purge_queue_p( queue => 'mq', )->then(sub { say "Queue purged..."; })->catch(sub { my $err = shift; warn "Queue purging error: $err"; })->wait;
$channel->delete_queue(queue => 'mq', if_empty => 1)->deliver;
Delete a queue.
This method deletes a queue. When a queue is deleted any pending messages are sent to a dead-letter queue if this is defined in the server configuration, and all consumers on the queue are cancelled.
Following arguments are accepted:
Same as delete_queue but auto-delivers method and returns a Mojo::Promise object.
$channel->delete_queue_p( queue => 'mq', if_empty => 1 )->then(sub { say "Queue removed..."; })->catch(sub { my $err = shift; warn "Queue removal error: $err"; })->wait;
my $message = $channel->publish( exchange => 'mojo', routing_key => 'mq', body => 'simple text body', ); $message->deliver();
Publish a message.
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 active consumers when the transaction, if any, is committed.
Following arguments are accepted:
All rejections are emitted as "reject" event.
$message->on(reject => sub { my $message = shift; my $frame = shift; my $method_frame = $frame->method_frame; my $reply_code = $method_frame->reply_code; my $reply_text = $method_frame->reply_text; });
As said above, all rejections are emitted as "reject" event.
$message->on(reject => sub { ... });
my $consumer = $channel->consume(queue => 'mq'); $consumer->on(message => sub { ... }); $consumer->deliver;
This method asks the server to start a "consumer", which is a transient request for messages from a specific queue. Consumers last as long as the channel they were declared on, or until the client cancels them.
Following arguments are accepted:
$consumer->on(success => sub { my $consumer = shift; my $frame = shift; my $consumer_tag = $frame->method_frame->consumer_tag; });
See RabbitMQ Compatibility and Conformance <https://www.rabbitmq.com/specification.html>
$channel->cancel(consumer_tag => 'amq.ctag....')->deliver;
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 cancel-ok reply.
Following arguments are accepted:
my $get = $channel->get(queue => 'mq') $get->deliver;
Direct access to a queue.
This method provides a direct access to the messages in a queue using a synchronous dialogue that is designed for specific types of application where synchronous functionality is more important than performance.
This is simple event emitter to which you have to subscribe. It can emit:
This method delivers a message to the client following a get method. A message delivered by 'get-ok' must be acknowledged unless the no-ack option was set in the get method.
You can access all get-ok reply parameters as below:
$get->on(message => sub { my $get = shift; my $get_ok = shift; my $message = shift; say "Still got: " . $get_ok->method_frame->message_count; });
This method tells the client that the queue has no messages available for the client.
Following arguments are accepted:
$channel->ack(delivery_tag => 1);
Acknowledge one or more messages.
When sent by the client, this method acknowledges one or more messages delivered via the Deliver or Get-Ok methods. When sent by server, this method acknowledges one or more messages published with the Publish method on a channel in confirm mode. The acknowledgement can be for a single message or a set of messages up to and including a specific message.
Following arguments are accepted:
$channel->qos(prefetch_count => 1)->deliver;
Sets specified Quality of Service to channel, or entire connection. Accepts following arguments:
$channel->recover(requeue => 0)->deliver;
Redeliver unacknowledged messages.
This method asks the server to redeliver all unacknowledged messages on a specified channel. Zero or more messages may be redelivered.
$channel->reject(delivery_tag => 1, requeue => 0)->deliver;
Reject an incoming message.
This method allows a client to reject a message. It can be used to interrupt and cancel large incoming messages, or return untreatable messages to their original queue.
Following arguments are accepted:
Work with transactions.
The Tx class allows publish and ack operations to be batched into atomic units of work. The intention is that all publish and ack requests issued within a transaction will complete successfully or none of them will. Servers SHOULD implement atomic transactions at least where all publish or ack requests affect a single queue. Transactions that cover multiple queues may be non-atomic, given that queues can be created and destroyed asynchronously, and such events do not form part of any transaction. Further, the behaviour of transactions with respect to the immediate and mandatory flags on Basic.Publish methods is not defined.
$channel->select_tx()->deliver;
Select standard transaction mode.
This method sets the channel to use standard transactions. The client must use this method at least once on a channel before using the Commit or Rollback methods.
$channel->commit_tx()->deliver;
Commit the current transaction.
This method commits all message publications and acknowledgments performed in the current transaction. A new transaction starts immediately after a commit.
$channel->rollback_tx()->deliver;
Abandon the current transaction.
This method abandons all message publications and acknowledgments performed in the current transaction. A new transaction starts immediately after a rollback. Note that unacked messages will not be automatically redelivered by rollback; if that is required an explicit recover call should be issued.
Mojo::RabbitMQ::Client, Mojo::RabbitMQ::Client::Method, Net::AMQP::Protocol::v0_8
Copyright (C) 2015-2017, Sebastian Podjasek and others
Based on AnyEvent::RabbitMQ - Copyright (C) 2010 Masahito Ikuta, maintained by "bobtfish@bobtfish.net"
This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0.
2018-07-29 | perl v5.26.2 |