Documentation

Example usage - Zend_Queue

Example usage

The below example of Zend_Queue shows a variety of features, including queue creation, queue retrieval, message retrieval, message deletion, and sending messages.

  1. // For configuration options
  2. // @see Zend_Queue_Adapater::__construct()
  3. $options = array(
  4.     'name' => 'queue1',
  5. );
  6.  
  7. // Create an array queue
  8. $queue = new Zend_Queue('Array', $options);
  9.  
  10. // Get list of queues
  11. foreach ($queue->getQueues() as $name) {
  12.     echo $name, "\n";
  13. }
  14.  
  15. // Create a new queue
  16. $queue2 = $queue->createQueue('queue2');
  17.  
  18. // Get number of messages in a queue (supports Countable interface from SPL)
  19. echo count($queue);
  20.  
  21. // Get up to 5 messages from a queue
  22. $messages = $queue->receive(5);
  23.  
  24. foreach ($messages as $i => $message) {
  25.     echo $message->body, "\n";
  26.  
  27.     // We have processed the message; now we remove it from the queue.
  28.     $queue->deleteMessage($message);
  29. }
  30.  
  31. // Send a message to the currently active queue
  32. $queue->send('My Test Message');
  33.  
  34. // Delete a queue we created and all of it's messages
  35. $queue->deleteQueue('queue2');

Previous topic

Introduction

Next topic

Framework

Copyright

© 2006-2021 by Zend by Perforce. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.

Contacts