/** * Get messages in the queue * * @param integer $maxMessages Maximum number of messages to return * @param integer $timeout Visibility timeout for these messages * @param Zend_Queue $queue * @return Zend_Queue_Message_Iterator * @throws Zend_Queue_Exception - database error */ public function receive($maxMessages = null, $timeout = null, Zend_Queue $queue = null) { if ($maxMessages === null) { $maxMessages = 1; } if ($timeout === null) { $timeout = self::RECEIVE_TIMEOUT_DEFAULT; } if ($queue === null) { $queue = $this->_queue; } $msgs = array(); $info = $this->_messageTable->info(); $microtime = microtime(true); // cache microtime $db = $this->_messageTable->getAdapter(); // start transaction handling try { if ($maxMessages > 0) { // ZF-7666 LIMIT 0 clause not included. $db->beginTransaction(); $query = $db->select(); if ($this->_options['options'][Zend_Db_Select::FOR_UPDATE]) { // turn on forUpdate $query->forUpdate(); } $query->from($info['name'], array('*'))->where('queue_id=?', $this->getQueueId($queue->getName()))->where('handle IS NULL OR timeout+' . (int) $timeout . ' < ' . (int) $microtime)->limit($maxMessages); foreach ($db->fetchAll($query) as $data) { // setup our changes to the message $data['handle'] = md5(uniqid(rand(), true)); $update = array('handle' => $data['handle'], 'timeout' => $microtime); // update the database $where = array(); $where[] = $db->quoteInto('message_id=?', $data['message_id']); $where[] = 'handle IS NULL OR timeout+' . (int) $timeout . ' < ' . (int) $microtime; $count = $db->update($info['name'], $update, $where); // we check count to make sure no other thread has gotten // the rows after our select, but before our update. if ($count > 0) { $msgs[] = $data; } } $db->commit(); } } catch (Exception $e) { $db->rollBack(); // require_once 'Zend/Queue/Exception.php'; throw new Zend_Queue_Exception($e->getMessage(), $e->getCode(), $e); } $options = array('queue' => $queue, 'data' => $msgs, 'messageClass' => $queue->getMessageClass()); $classname = $queue->getMessageSetClass(); if (!class_exists($classname)) { // require_once 'Zend/Loader.php'; Zend_Loader::loadClass($classname); } return new $classname($options); }
/** * Returns the length of the queue * * @return integer */ public function count() { $info = $this->_message->info(); $db = $this->_message->getAdapter(); $query = $db->select(); $query->from($info['name'], array(new Zend_Db_Expr('COUNT(*)'))); $query->where('queue_id=?', $this->getActiveQueue()); return $db->fetchOne($query); }