7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@zend.com so we can send you a copy immediately.
17 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
18 * @license http://framework.zend.com/license/new-bsd New BSD License
23 * Class for connecting to queues performing common operations.
27 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
28 * @license http://framework.zend.com/license/new-bsd New BSD License
30 class Zend_Queue
implements Countable
33 * Use the TIMEOUT constant in the config of a Zend_Queue
35 const TIMEOUT
= 'timeout';
38 * Default visibility passed to count
40 const VISIBILITY_TIMEOUT
= 30;
43 * Use the NAME constant in the config of Zend_Queue
48 * @var Zend_Queue_Adapter_AdapterInterface
50 protected $_adapter = null;
53 * User-provided configuration
57 protected $_options = array();
60 * Zend_Queue_Message class
64 protected $_messageClass = 'Zend_Queue_Message';
67 * Zend_Queue_Message_Iterator class
71 protected $_messageSetClass = 'Zend_Queue_Message_Iterator';
76 protected $_logger = null;
82 * $queue = new Zend_Queue($config);
84 * $queue = new Zend_Queue('array', $config);
86 * $queue = new Zend_Queue(null, $config); // Zend_Queue->createQueue();
88 * @param string|Zend_Queue_Adapter|array|Zend_Config|null String or adapter instance, or options array or Zend_Config instance
89 * @param Zend_Config|array $options Zend_Config or a configuration array
92 public function __construct($spec, $options = array())
95 if ($spec instanceof Zend_Queue_Adapter_AdapterInterface
) {
97 } elseif (is_string($spec)) {
99 } elseif ($spec instanceof Zend_Config
) {
100 $options = $spec->toArray();
101 } elseif (is_array($spec)) {
105 // last minute error checking
106 if ((null === $adapter)
107 && (!is_array($options) && (!$options instanceof Zend_Config
))
109 require_once 'Zend/Queue/Exception.php';
110 throw new Zend_Queue_Exception('No valid params passed to constructor');
113 // Now continue as we would if we were a normal constructor
114 if ($options instanceof Zend_Config
) {
115 $options = $options->toArray();
116 } elseif (!is_array($options)) {
120 // Make sure we have some defaults to work with
121 if (!isset($options[self
::TIMEOUT
])) {
122 $options[self
::TIMEOUT
] = self
::VISIBILITY_TIMEOUT
;
125 // Make sure all defaults are appropriately set.
126 if (!array_key_exists('timeout', $options)) {
127 $options[self
::TIMEOUT
] = self
::VISIBILITY_TIMEOUT
;
129 if (array_key_exists('messageClass', $options)) {
130 $this->setMessageClass($options['messageClass']);
132 if (array_key_exists('messageSetClass', $options)) {
133 $this->setMessageSetClass($options['messageSetClass']);
136 $this->setOptions($options);
138 // if we were passed an adapter we either build the $adapter or use it
139 if (null !== $adapter) {
140 $this->setAdapter($adapter);
147 * @param array $options
150 public function setOptions(array $options)
152 $this->_options
= array_merge($this->_options
, $options);
157 * Set an individual configuration option
159 * @param string $name
160 * @param mixed $value
163 public function setOption($name, $value)
165 $this->_options
[(string) $name] = $value;
170 * Returns the configuration options for the queue
174 public function getOptions()
176 return $this->_options
;
180 * Determine if a requested option has been defined
182 * @param string $name
185 public function hasOption($name)
187 return array_key_exists($name, $this->_options
);
191 * Retrieve a single option
193 * @param string $name
194 * @return null|mixed Returns null if option does not exist; option value otherwise
196 public function getOption($name)
198 if ($this->hasOption($name)) {
199 return $this->_options
[$name];
205 * Set the adapter for this queue
207 * @param string|Zend_Queue_Adapter_AdapterInterface $adapter
208 * @return Zend_Queue Provides a fluent interface
210 public function setAdapter($adapter)
212 if (is_string($adapter)) {
213 if (null === ($adapterNamespace = $this->getOption('adapterNamespace'))) {
214 $adapterNamespace = 'Zend_Queue_Adapter';
217 $adapterName = str_replace(
224 strtolower($adapterNamespace . '_' . $adapter)
229 if (!class_exists($adapterName)) {
230 require_once 'Zend/Loader.php';
231 Zend_Loader
::loadClass($adapterName);
235 * Create an instance of the adapter class.
236 * Pass the configuration to the adapter class constructor.
238 $adapter = new $adapterName($this->getOptions(), $this);
241 if (!$adapter instanceof Zend_Queue_Adapter_AdapterInterface
) {
242 require_once 'Zend/Queue/Exception.php';
243 throw new Zend_Queue_Exception("Adapter class '" . get_class($adapterName) . "' does not implement Zend_Queue_Adapter_AdapterInterface");
246 $this->_adapter
= $adapter;
248 $this->_adapter
->setQueue($this);
250 if (null !== ($name = $this->getOption(self
::NAME
))) {
251 $this->_setName($name);
258 * Get the adapter for this queue
260 * @return Zend_Queue_Adapter_AdapterInterface
262 public function getAdapter()
264 return $this->_adapter
;
268 * @param string $className
269 * @return Zend_Queue Provides a fluent interface
271 public function setMessageClass($className)
273 $this->_messageClass
= (string) $className;
280 public function getMessageClass()
282 return $this->_messageClass
;
286 * @param string $className
287 * @return Zend_Queue Provides a fluent interface
289 public function setMessageSetClass($className)
291 $this->_messageSetClass
= (string) $className;
298 public function getMessageSetClass()
300 return $this->_messageSetClass
;
304 * Get the name of the queue
306 * Note: _setName() used to exist, but it caused confusion with createQueue
307 * Will evaluate later to see if we should add it back in.
311 public function getName()
313 return $this->getOption(self
::NAME
);
319 * @param string $name queue name
320 * @param integer $timeout default visibility timeout
321 * @return Zend_Queue|false
322 * @throws Zend_Queue_Exception
324 public function createQueue($name, $timeout = null)
326 if (!is_string($name)) {
327 require_once 'Zend/Queue/Exception.php';
328 throw new Zend_Queue_Exception('$name is not a string');
331 if ((null !== $timeout) && !is_integer($timeout)) {
332 require_once 'Zend/Queue/Exception.php';
333 throw new Zend_Queue_Exception('$timeout must be an integer');
336 // Default to standard timeout
337 if (null === $timeout) {
338 $timeout = $this->getOption(self
::TIMEOUT
);
341 // Some queues allow you to create on the fly, but cannot return
342 // a list of queues. Stomp protocol for example.
343 if ($this->isSupported('create')) {
344 if ($this->getAdapter()->isExists($name)) {
348 if (!$this->getAdapter()->create($name, $timeout)) {
355 'timeout' => $timeout
358 return new self($this->getAdapter(), $options);
362 * Delete the queue this object is working on.
364 * This queue is disabled, regardless of the outcome of the deletion
365 * of the queue, because the programmers intent is to disable this queue.
369 public function deleteQueue()
371 if ($this->isSupported('delete')) {
372 $deleted = $this->getAdapter()->delete($this->getName());
379 * @see Zend_Queue_Adapter_Null
381 require_once('Zend/Queue/Adapter/Null.php');
382 $this->setAdapter(new Zend_Queue_Adapter_Null($this->getOptions()));
388 * Delete a message from the queue
390 * Returns true if the message is deleted, false if the deletion is
393 * Returns true if the adapter doesn't support message deletion.
395 * @param Zend_Queue_Message $message
397 * @throws Zend_Queue_Exception
399 public function deleteMessage(Zend_Queue_Message
$message)
401 if ($this->getAdapter()->isSupported('deleteMessage')) {
402 return $this->getAdapter()->deleteMessage($message);
408 * Send a message to the queue
410 * @param mixed $message message
411 * @return Zend_Queue_Message
412 * @throws Zend_Queue_Exception
414 public function send($message)
416 return $this->getAdapter()->send($message);
420 * Returns the approximate number of messages in the queue
424 public function count()
426 if ($this->getAdapter()->isSupported('count')) {
427 return $this->getAdapter()->count();
433 * Return the first element in the queue
435 * @param integer $maxMessages
436 * @param integer $timeout
437 * @return Zend_Queue_Message_Iterator
439 public function receive($maxMessages=null, $timeout=null)
441 if (($maxMessages !== null) && !is_integer($maxMessages)) {
442 require_once 'Zend/Queue/Exception.php';
443 throw new Zend_Queue_Exception('$maxMessages must be an integer or null');
446 if (($timeout !== null) && !is_integer($timeout)) {
447 require_once 'Zend/Queue/Exception.php';
448 throw new Zend_Queue_Exception('$timeout must be an integer or null');
451 // Default to returning only one message
452 if ($maxMessages === null) {
456 // Default to standard timeout
457 if ($timeout === null) {
458 $timeout = $this->getOption(self
::TIMEOUT
);
461 return $this->getAdapter()->receive($maxMessages, $timeout);
465 * Return a list of queue capabilities functions
467 * $array['function name'] = true or false
468 * true is supported, false is not supported.
470 * @param string $name
473 public function getCapabilities()
475 return $this->getAdapter()->getCapabilities();
479 * Indicates if a function is supported or not.
481 * @param string $name
484 public function isSupported($name)
486 $translation = array(
487 'deleteQueue' => 'delete',
488 'createQueue' => 'create'
491 if (isset($translation[$name])) {
492 $name = $translation[$name];
495 return $this->getAdapter()->isSupported($name);
499 * Get an array of all available queues
502 * @throws Zend_Queue_Exception
504 public function getQueues()
506 if (!$this->isSupported('getQueues')) {
507 throw new Zend_Queue_Exception( __FUNCTION__
. '() is not supported by ' . get_class($this->getAdapter()));
510 return $this->getAdapter()->getQueues();
514 * Set the name of the queue
516 * This is AN UNSUPPORTED FUNCTION
518 * @param string $name
519 * @return Zend_Queue|false Provides a fluent interface
521 protected function _setName($name)
523 if (!is_string($name)) {
525 * @see Zend_Queue_Exception
527 require_once 'Zend/Queue/Exception.php';
528 throw new Zend_Queue_Exception("$name is not a string");
531 if ($this->getAdapter()->isSupported('create')) {
532 if (!$this->getAdapter()->isExists($name)) {
533 $timeout = $this->getOption(self
::TIMEOUT
);
535 if (!$this->getAdapter()->create($name, $timeout)) {
536 // Unable to create the new queue
542 $this->setOption(self
::NAME
, $name);
548 * returns a listing of Zend_Queue details.
549 * useful for debugging
553 public function debugInfo()
556 $info['self'] = get_class($this);
557 $info['adapter'] = get_class($this->getAdapter());
558 foreach ($this->getAdapter()->getCapabilities() as $feature => $supported) {
559 $info['adapter-' . $feature] = ($supported) ?
'yes' : 'no';
561 $info['options'] = $this->getOptions();
562 $info['options']['driverOptions'] = '[hidden]';
563 $info['currentQueue'] = $this->getName();
564 $info['messageClass'] = $this->getMessageClass();
565 $info['messageSetClass'] = $this->getMessageSetClass();