DbForUpdate.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  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.
  14. *
  15. * @category Zend
  16. * @package Zend_Queue
  17. * @subpackage Custom
  18. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Db.php 14846 2009-04-11 15:56:47Z danlo $
  21. */
  22. /**
  23. * @see Zend_Queue_Adapter_Db
  24. */
  25. require_once 'Zend/Queue/Adapter/Db.php';
  26. /**
  27. * Class for using connecting to a Zend_Db-based queuing system
  28. *
  29. * @category Zend
  30. * @package Zend_Queue
  31. * @subpackage Custom
  32. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. /*
  36. $config['options'][Zend_Db_Select::FOR_UPDATE] is a new feature that was
  37. written after this code was written. However, this will still serve as a
  38. good example adapter
  39. */
  40. class Custom_DbForUpdate extends Zend_Queue_Adapter_Db
  41. {
  42. /**
  43. * Return the first element in the queue
  44. *
  45. * @param integer $maxMessages
  46. * @param integer $timeout
  47. * @param Zend_Queue $queue
  48. * @return Zend_Queue_Message_Iterator
  49. */
  50. public function receive($maxMessages=null, $timeout=null, Zend_Queue $queue=null)
  51. {
  52. if ($maxMessages === null) {
  53. $maxMessages = 1;
  54. }
  55. if ($timeout === null) {
  56. $timeout = self::RECEIVE_TIMEOUT_DEFAULT;
  57. }
  58. if ($queue === null) {
  59. $queue = $this->_queue;
  60. }
  61. $msgs = array();
  62. $info = $this->_msg_table->info();
  63. $microtime = microtime(true); // cache microtime
  64. $db = $this->_msg_table->getAdapter();
  65. try {
  66. // transaction must start before the select query.
  67. $db->beginTransaction();
  68. // changes: added forUpdate
  69. $query = $db->select()->forUpdate();
  70. $query->from($info['name'], array('*'));
  71. $query->where('queue_id=?', $this->getQueueId($queue->getName()));
  72. $query->where('handle IS NULL OR timeout+' . (int)$timeout . ' < ' . (int)$microtime);
  73. $query->limit($maxMessages);
  74. foreach ($db->fetchAll($query) as $data) {
  75. // setup our changes to the message
  76. $data['handle'] = md5(uniqid(rand(), true));
  77. $update = array(
  78. 'handle' => $data['handle'],
  79. 'timeout' => $microtime
  80. );
  81. // update the database
  82. $where = array();
  83. $where[] = $db->quoteInto('message_id=?', $data['message_id']);
  84. $count = $db->update($info['name'], $update, $where);
  85. // we check count to make sure no other thread has gotten
  86. // the rows after our select, but before our update.
  87. if ($count > 0) {
  88. $msgs[] = $data;
  89. $this->getLogger()->debug('Received message:' . $data['message_id'] . ' byte size=' . strlen($data['body']));
  90. }
  91. }
  92. $db->commit();
  93. } catch (Exception $e) {
  94. $db->rollBack();
  95. $this->getLogger()->err($e->getMessage() . ' code ' . $e->getCode());
  96. /**
  97. * @see Zend_Queue_Exception
  98. */
  99. require_once 'Zend/Queue/Exception.php';
  100. throw new Zend_Queue_Exception($e->getMessage(), $e->getCode());
  101. }
  102. $config = array(
  103. 'queue' => $queue,
  104. 'data' => $msgs,
  105. 'messageClass' => $queue->getMessageClass()
  106. );
  107. $classname = $queue->getMessageSetClass();
  108. Zend_Loader::loadClass($classname);
  109. return new $classname($config);
  110. }
  111. }