Using Azure queues from C++

Azure queues are used to store a large number of items (referred to as messages). Each message can go up to 64 Kb. A typical use for queues is intra-app communication, for example your website might add messages to the queue for a background service to process later on. The C++ Azure storage SDK maintains its consistent API when it comes to queues as well (so if you know how to use blobs and tables, you are good). Here’s a code snippet that shows how to insert messages into a queue.

void InsertQueueMessages()
{
  auto storage_account = cloud_storage_account::parse(
      U("UseDevelopmentStorage=true"));
  auto queue_client = storage_account
      .create_cloud_queue_client();
  auto queue = queue_client.get_queue_reference(
      U("sample"));
  bool created = queue.create_if_not_exists();

  queue.add_message(cloud_queue_message(
      U("queue message 01")));
  queue.add_message(cloud_queue_message(
      U("queue message 02")));
  queue.add_message(cloud_queue_message(
      U("queue message 03")));
}

Like any decent queue, you can either peek or dequeue a message. Here’s an example snippet that shows how to peek into a queue.

void PeekQueueMessage()
{
  auto storage_account = cloud_storage_account::parse(
      U("UseDevelopmentStorage=true"));
  auto queue_client = storage_account
      .create_cloud_queue_client();
  auto queue = queue_client.get_queue_reference(
      U("sample"));
  bool created = queue.create_if_not_exists();

  queue.download_attributes();
  int count = queue.approximate_message_count();
  ucout << U("Approx count = ") << count << endl;

  // Peek the next 2 messages (assumes at least 2 present)
  auto messages = queue.peek_messages(2);
  for (auto message : messages)
  {
    ucout << message.content_as_string() << endl;
  }
}

The count method has ‘approximate‘ in the name because the count returned is not refreshed until the next call to download_attributes.

Here’s a code snippet that shows how to dequeue items from a queue.

void DeQueueMessages()
{
  auto storage_account = cloud_storage_account::parse(
      U("UseDevelopmentStorage=true"));
  auto queue_client = storage_account
      .create_cloud_queue_client();
  auto queue = queue_client.get_queue_reference(
      U("sample"));
  bool created = queue.create_if_not_exists();

  queue.download_attributes();
  int count = queue.approximate_message_count();
  ucout << U("Approx count = ") << count << endl;

  for (size_t i = 0; i < count; i++)
  {
    auto message = queue.get_message();
    queue.delete_message(message);
    ucout << message.content_as_string() << endl;
  }

  queue.download_attributes();
  count = queue.approximate_message_count();
  ucout << U("Approx count = ") << count << endl;
}

Notice how I call delete_message after calling get_message. This is because get_message only hides the item from other callers for a default time out (30 seconds) after which the message is back. This allows you to rollback changes in case something goes wrong. So you’d get a message, process it, and when you are done processing it, you can call delete_message. This assumes you do your processing in under 30 seconds. If you want to increase (or decrease) that time out, you can do that too. I’ll cover that in the next blog entry along with showing how to update a message in the queue.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s