PrePAN

Sign in to PrePAN

Profile

mikkoi@github

Software Engineer and Architect

GitHub: mikkoi PAUSE ID: MIKKOI

User's Modules

Net::IronMQ::Client IronMQ (Message Queue) Client

Net::IronMQ is a client for the IronMQ message queue at http://www.iron.io/. IronMQ is a cloud based message queue with a REST API.

Is the API laid out below (documented as "instructions") consistent and clear? Easy to use?

Net::IronMQ is a client for the IronMQ message queue. IronMQ is a cloud based message queue with a REST API. Net::IronMQ creates a Perl object for interacting with IronMQ. All IronMQ functions are available. (Push queue functions are not yet implemented.)

The class Net::IronMQ::Client instantiates the 'project', IronMQ access configuration.

IronMQ Message Queue

IronMQ is a message queue as a service available to Internet connecting applications via its REST interface. Built with distributed cloud applications in mind. Provides on-demand message queuing with HTTPS transport, one-time FIFO delivery, message persistence, and cloud-optimized performance.

[see http://www.iron.io/

Using the IronMQ Client Library

Net::IronMQ::Client is a normal Perl package meant to be used as an object.

   require Net::IronMQ::Client;
   my $iron_mq_client = Net::IronMQ::Client->new( { } );

The following parameters can be given to new() as items in the first parameter which is a hash.

  • project_id, The ID of the project to use for requests.
  • token, The OAuth token that is used to authenticate requests.
  • host, The domain name the API can be located at. E.g. 'mq-aws-us-east-1.iron.io/1'.
  • protocol, The protocol that will be used to communicate with the API. Defaults to "https".
  • port, The port to connect to the API through. Defaults to 443.
  • api_version, The version of the API to connect through. Defaults to the version supported by the client.
  • host_path_prefix, Path prefix to the RESTful url. Defaults to '/1'. Used with non-standard clouds/emergency service back up addresses.
  • timeout, REST client timeout (for REST calls accessing IronMQ.)
  • config, Config filename with path if required.

You can also give the parameters in the config file '.iron.json' or 'iron.json' (in local dir) or as environmental variables. Please read http://dev.iron.io/mq/reference/configuration/ for further details.

After creating the client, the client can create a new message queue, get, modify or delete an old one or get all the existing message queues within the same project.

The client has all the methods which interact with queues; the queue (object of Net::IronMQ::Queue) has methods which involve messages.

When failed, the methods return undef. After each REST API call to IronMQ the HTTP return code and result can be retrieved with methods return_code and return_msg.

  # Create a new queue (Parameter queue name; return a Net::IronMQ::Queue object)
   my $iron_mq_queue = $iron_mq_client->create_queue('My_Message_Queue');

   # Get an existing queue. (Parameter queue name; return a Net::IronMQ::Queue object)
   my $iron_mq_queue = $iron_mq_client->get_queue('My_Message_Queue');

   # Delete an existing queue. (Parameter queue name; return 1)
   my $success = $iron_mq_client->delete_queue('My_Message_Queue');

   # Get all the queues. Return a list of Net::IronMQ::Queue objects.
   my @iron_mq_queues = $iron_mq_client->get_queues();

   # Update a queue. Return a Net::IronMQ::Queue object.
   # Queue properties: set queue type (pull, multicast, unicast), set subscribers.
   # Push queue feature; not yet implemented.
   my $iron_mq_queue = $iron_mq_client->update_queue('My_Message_Queue', {} ); # Not yet implemented.

   # Get info about the queue Return a hash containing items name, id, size, project, etc.).
   my $queue_info = $iron_mq_client->info_about_queue('My_Message_Queue');
   print $iron_mq_client->return_msg if !defined $queue_info;
   my $size = $queue_info->{'size'};

   # Get the last message from IronMQ.
   my $last_msg = $iron_mq_client->return_msg;

   # Get the last return code from IronMQ.
   my $last_return_code = $iron_mq_client->return_code;

A Net::IronMQ::Queue object gives access to a single message queue. With it you can do all the normal things one would with a message queue.

Messages are objects of the class Net::IronMQ::Message. It contains the following attributes:

  • body, Free text. Will be JSONized. If you need an object serialized, don't use JSON. Use e.g. YAML. Then give the resulting string here.
  • timeout, When reading from queue, after timeout (in seconds), item will be placed back onto queue.
  • delay, The item will not be available on the queue until this many seconds have passed.
  • expires_in, How long in seconds to keep the item on the queue before it is deleted.
  • id, Message id from IronMQ (available after message has been pulled/peeked).
  • reserved_count, Not yet implemented. (available after message has been pulled/peeked).
  • push_status, Not yet implemented. (available after message has been pulled/peeked).
   my $iron_mq_msg_send_01 = Net::IronMQ::Message->new( {
           'body' => "My message",
           } );

       # Or
   my $iron_mq_msg_send_02 = Net::IronMQ::Message->new( {
           'body' => $msg_body,
           'timeout' => $msg_timeout, # When reading from queue, after timeout (in seconds), item will be placed back onto queue.
           'delay' => $msg_delay,     # The item will not be available on the queue until this many seconds have passed.
           'expires_in' => $msg_expires_in, # How long in seconds to keep the item on the queue before it is deleted.
           } );

Net::IronMQ::Queue objects are created by the client Net::IronMQ::Client. With a Net::IronMQ::Queue object you can push messages to the queue, or pull messages from it. The names push and pull are used because the queue is likened to a pipe. The queue is like a FIFO pipe (first in, first out).

Get queue id.

   my $queue_id = $iron_mq_queue->id();

Get queue name.

   my $queue_name = $iron_mq_queue->name();

Add one or more messages to the queue. Returns the ids of the messages sent or the number of sent messages.

   my $msg_send_id = $iron_mq_queue->push($iron_mq_msg_send_01);
   my @msg_send_ids = $iron_mq_queue->push($iron_mq_msg_send_01, $iron_mq_msg_send_02, [...]);
   my $number_of_msgs_sent = $iron_mq_queue->push($iron_mq_msg_send_01, $iron_mq_msg_send_02, [...]);

Read one or more messages from the queue and reserve them so another process cannot access them. Parameters: n (number of messages you want, default 1, maximum 100; if there is less, all available messages will be returned), timeout (After timeout (in seconds), item will be placed back onto queue, default is 60 seconds, minimum is 30 seconds, and maximum is 86,400 seconds (24 hours)).

   my $iron_mq_msg_pull = $iron_mq_queue->pull();
   my @iron_mq_msg_pulls = $iron_mq_queue->pull( { 'n' => 10, timeout => 120 } );

Read one or more messages from the queue but don't reserve them. Parameters: n (number of messages you want, default 1, maximum 100; if there is less, all available messages will be returned).

   my $iron_mq_msg_peek = $iron_mq_queue->peek();
   my @iron_mq_msg_peeks = $iron_mq_queue->peek( { 'n' => 10 } );

Delete one or more messages from the queue. Call this when you have processed the messages. Returns the ids of the messages deleted or the number of deleted messages.

   my $deleted_msg_id = $iron_mq_queue->delete($msg_id_01);
   my @deleted_msg_ids = $iron_mq_queue->delete($msg_id_01, $msg_id_02, [...]);
   my $number_of_msgs_deleted = $iron_mq_queue->delete($msg_id_01, $msg_id_02, [...]);

Release one or more messages back to the queue. (Release for several messages at the same time is not supported directly by IronMQ but the client implements this feature.) Returns the ids of the messages released or the number of released messages.

   my $released_msg_id = $iron_mq_queue->release($msg_id_01);
   my @released_msg_ids = $iron_mq_queue->release($msg_id_01, $msg_id_02, [...]);
   my $number_of_msgs_released = $iron_mq_queue->release($msg_id_01, $msg_id_02, [...]);

Touch one or more messages in the queue. Touching a reserved message extends its timeout to the duration specified when the message was created. Default is 60 seconds. (Touch for several messages at the same time is not supported directly by IronMQ but the client implements this feature.) Returns the ids of the messages touched or the number of touched messages.

   my $touched_msg_id = $iron_mq_queue->touch($msg_id_01);
   my @touched_msg_ids = $iron_mq_queue->touch($msg_id_01, $msg_id_02, [...]);
   my $number_of_msgs_touched = $iron_mq_queue->touch($msg_id_01, $msg_id_02, [...]);

Clear all messages from a queue. Delete all messages on a queue, whether they are reserved or not.

   my $cleared = $iron_mq_queue->clear;

Get queue size.

   my $size = $iron_mq_queue->size;

Get push status for a message. Push queue feature; not yet implemented.

Add Subscribers to a Queue. Push queue feature; not yet implemented.

Remove Subscribers from a Queue Push queue feature; not yet implemented.

Exceptions

A REST call to IronMQ server may fail for several reason. All failures generate an exception using the Exception::Class package. Class IronMQHTTPCallException contains the field status_code, response_message and error. Error is formatted as such: IronMQHTTPCallException: status_code=HTTP status code response_message=response_message.

   use Try::Tiny;
   use Scalar::Util qw{blessed};
   try {
       my $queried_iron_mq_queue_01 = $iron_mq_client->get_queue($unique_queue_name_01);
   }
   catch {
       die $_ unless blessed $_ && $_->can('rethrow');
       if ( $_->isa('IronMQHTTPCallException') ) {
           # Do something.
       }
       else {
           $_->rethrow; # Push the error upwards.
       }
   };

mikkoi@github 3 comments