Index: queue_example/queue_example.info
===================================================================
RCS file: queue_example/queue_example.info
diff -N queue_example/queue_example.info
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ queue_example/queue_example.info	8 Apr 2010 05:58:51 -0000
@@ -0,0 +1,7 @@
+; $Id$
+name = Queue example
+description = Examples of using the Drupal Queue API.
+package = Example modules
+core = 7.x
+files[] = queue_example.module
+files[] = queue_example.test
Index: queue_example.module
===================================================================
RCS file: queue_example/queue_example.module
diff -N queue_example/queue_example.module
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ queue_example/queue_example.module	8 Apr 2010 05:58:51 -0000
@@ -0,0 +1,171 @@
+<?php
+// $Id$
+
+/**
+ * @file
+ * Examples demonstrating the Drupal Queue API.
+ */
+
+/**
+ * Implements hook_menu() to set up the URLs (menu entries) for the
+ * queue examples.
+ */
+function queue_example_menu() {
+  $items = array();
+  $items['queue_example/systemqueue'] = array(
+    'title' => 'Queue item example',
+    'description' => t("Example of the Drupal Queue API"),
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('queue_store_form'),
+    'access callback' => TRUE,
+    
+  );
+  $items['queue_example/systemqueue/store'] = array(
+    'title' => 'Store',
+    'description' => t("Example of the Drupal Queue API"),
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('queue_store_form'),
+    'access callback' => TRUE,
+    'type' => MENU_DEFAULT_LOCAL_TASK,
+  );
+  $items['queue_example/systemqueue/items'] = array(
+    'title' => 'Items',
+    'description' => t("Example of the Drupal Queue API"),
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('queue_example_items_form', 'queue_example_queue'),
+    'access callback' => TRUE,
+    'type' => MENU_LOCAL_TASK,
+    'weight' => 2,
+  );
+
+  return $items;
+}
+
+/**
+ * Form for Queue storage
+ */
+function queue_store_form($form, &$form_state) {
+  $form['item_data'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Data to queue'),
+    '#description' => t('Enter a string to queue. For example, "jbjlbhqrpbqrqguvf"'),
+    '#required' => TRUE,
+  );
+  
+  $form['submit'] = array(
+    '#type' => 'submit',
+    '#value' => t('Submit'),
+  );
+  
+  return $form;
+}
+
+/**
+ * Submit handler for queueing
+ */
+function queue_store_form_submit($form, &$form_state) {
+  // Create a default queue called 'queue_example_queue'.
+  // If the default queue class is SystemQueue this creates a queue that stores
+  // in the database.
+  $queue = DrupalQueue::get('queue_example_queue');
+  $queue->createQueue();
+  // Queue the data by invoking the method createItem().
+  $queue->createItem($form_state['values']['item_data']);
+  $message = t('Queued your submission. <a href="!url">Claim and view</a> the queued items.', array('!url' => url('queue_example/systemqueue/items')));
+  drupal_set_message($message);
+}
+
+/**
+ * Page for viewing and deleting queued items.
+ */
+function queue_example_items_form($form, &$form_state, $queue_name) {
+  // Get the queue.
+  $queue = DrupalQueue::get($queue_name);
+
+  $count = $queue->numberOfItems();
+  $form['count_output'] = array(
+    '#markup' => t('<p>There !count in the queue.</p>', array('!count' => format_plural($count, 'is 1 item', 'are @count items')))
+  );
+  
+  $data = array();
+  $claimed = 0;
+  // Claim an item, will return the item as object with properties 'name' and
+  // 'data'. 'name' is the queue name and 'data' is the item's data, as entered.
+  // We claim it with a lease of 3600 seconds. Cron would have to run to release
+  // the claim lease unless the user runs our 'release items' operation.
+  $item = $queue->claimItem(3600);
+  while ($item !== FALSE) {
+    // If item was claimed hold onto it.
+    $claimed++;
+    $item_data[] = $item->data;
+    $queue_items[] = $item;
+    // Try and claim another item.
+    $item = $queue->claimItem(3600);
+  }
+
+  if (!empty($queue_items)) {
+    // List the items.
+    $output = t('<p>Claimed items (!claimed): @data</p>', array('!claimed' => $claimed, '@data' => implode(', ', $item_data)));
+    $form['output'] = array(
+      '#markup' => $output,
+    );
+    // Hold onto items so we can delete them without having to reclaim.
+      $form['queue_items'] = array(
+        '#type' => 'value',
+        '#value' => $queue_items,
+      );
+    // Delete button.
+    $form['delete'] = array(
+      '#type' => 'submit',
+      '#value' => t('Delete items'),
+    );
+  }
+  elseif ($count) {
+    $output = t('<p>No items can be claimed because they are leased.</p>');
+    $form['output'] = array(
+      '#markup' => $output,
+    );
+  }
+  
+  $form['queue'] = array(
+    '#type' => 'value',
+    '#value' => $queue,
+  );
+    
+  $form['release'] = array(
+    '#type' => 'submit',
+    '#value' => t('Release items'),
+  );
+  return $form;
+}
+
+/**
+ * Submit handler for items form.
+ */
+function queue_example_items_form_submit($form, &$form_state) {
+  // Retrieve the queue.
+  $queue = $form_state['values']['queue'];
+
+  if ($form_state['values']['op'] == $form_state['values']['release']) {
+    // Release the items.
+    // In interest of demonstration we expire items directly.
+    // Drupal core runs this on cron so it is not recommended you reuse.
+    db_update('queue')
+      ->fields(array(
+        'expire' => 0,
+      ))
+      ->condition('expire', REQUEST_TIME, '<')
+      ->execute();
+    // Rebuild the form to allow the delete button to appear.
+    $form_state['rebuild'] = TRUE;
+  }
+  else {
+    // Delete the items.
+    $count = 0;
+    foreach ($form_state['values']['queue_items'] as $item) {
+      $queue->deleteItem($item);
+      $count++;
+    }
+    drupal_set_message(t('Deleted !count items', array('!count' => $count)));
+  }
+}
\ No newline at end of file
