PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/www/system/library/Zend/Queue/Adapter/Activemq.php

https://bitbucket.org/vmihailenco/zf-blog
PHP | 336 lines | 163 code | 47 blank | 126 comment | 19 complexity | 399154c7a4ef714c0f7736bf6b4eae89 MD5 | raw file
  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 Adapter
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Activemq.php 20096 2010-01-06 02:05:09Z bkarwin $
  21. */
  22. /**
  23. * @see Zend_Queue_Adapter_AdapterAbstract
  24. */
  25. /**
  26. * @see Zend_Queue_Adapter_Stomp_Client
  27. */
  28. /**
  29. * @see Zend_Queue_Adapter_Stomp_Frame
  30. */
  31. /**
  32. * Class for using Stomp to talk to an Stomp compliant server
  33. *
  34. * @category Zend
  35. * @package Zend_Queue
  36. * @subpackage Adapter
  37. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  38. * @license http://framework.zend.com/license/new-bsd New BSD License
  39. */
  40. class Zend_Queue_Adapter_Activemq extends Zend_Queue_Adapter_AdapterAbstract
  41. {
  42. const DEFAULT_SCHEME = 'tcp';
  43. const DEFAULT_HOST = '127.0.0.1';
  44. const DEFAULT_PORT = 61613;
  45. /**
  46. * @var Zend_Queue_Adapter_Stomp_client
  47. */
  48. private $_client = null;
  49. /**
  50. * Constructor
  51. *
  52. * @param array|Zend_Config $config An array having configuration data
  53. * @param Zend_Queue The Zend_Queue object that created this class
  54. * @return void
  55. */
  56. public function __construct($options, Zend_Queue $queue = null)
  57. {
  58. parent::__construct($options);
  59. $options = &$this->_options['driverOptions'];
  60. if (!array_key_exists('scheme', $options)) {
  61. $options['scheme'] = self::DEFAULT_SCHEME;
  62. }
  63. if (!array_key_exists('host', $options)) {
  64. $options['host'] = self::DEFAULT_HOST;
  65. }
  66. if (!array_key_exists('port', $options)) {
  67. $options['port'] = self::DEFAULT_PORT;
  68. }
  69. if (array_key_exists('stompClient', $options)) {
  70. $this->_client = $options['stompClient'];
  71. } else {
  72. $this->_client = new Zend_Queue_Stomp_Client($options['scheme'], $options['host'], $options['port']);
  73. }
  74. $connect = $this->_client->createFrame();
  75. // Username and password are optional on some messaging servers
  76. // such as Apache's ActiveMQ
  77. $connect->setCommand('CONNECT');
  78. if (isset($options['username'])) {
  79. $connect->setHeader('login', $options['username']);
  80. $connect->setHeader('passcode', $options['password']);
  81. }
  82. $response = $this->_client->send($connect)->receive();
  83. if ((false !== $response)
  84. && ($response->getCommand() != 'CONNECTED')
  85. ) {
  86. throw new Zend_Queue_Exception("Unable to authenticate to '".$options['scheme'].'://'.$options['host'].':'.$options['port']."'");
  87. }
  88. }
  89. /**
  90. * Close the socket explicitly when destructed
  91. *
  92. * @return void
  93. */
  94. public function __destruct()
  95. {
  96. // Gracefully disconnect
  97. $frame = $this->_client->createFrame();
  98. $frame->setCommand('DISCONNECT');
  99. $this->_client->send($frame);
  100. unset($this->_client);
  101. }
  102. /**
  103. * Create a new queue
  104. *
  105. * @param string $name queue name
  106. * @param integer $timeout default visibility timeout
  107. * @return void
  108. * @throws Zend_Queue_Exception
  109. */
  110. public function create($name, $timeout=null)
  111. {
  112. throw new Zend_Queue_Exception('create() is not supported in ' . get_class($this));
  113. }
  114. /**
  115. * Delete a queue and all of its messages
  116. *
  117. * @param string $name queue name
  118. * @return void
  119. * @throws Zend_Queue_Exception
  120. */
  121. public function delete($name)
  122. {
  123. throw new Zend_Queue_Exception('delete() is not supported in ' . get_class($this));
  124. }
  125. /**
  126. * Delete a message from the queue
  127. *
  128. * Returns true if the message is deleted, false if the deletion is
  129. * unsuccessful.
  130. *
  131. * @param Zend_Queue_Message $message
  132. * @return boolean
  133. */
  134. public function deleteMessage(Zend_Queue_Message $message)
  135. {
  136. $frame = $this->_client->createFrame();
  137. $frame->setCommand('ACK');
  138. $frame->setHeader('message-id', $message->handle);
  139. $this->_client->send($frame);
  140. return true;
  141. }
  142. /**
  143. * Get an array of all available queues
  144. *
  145. * @return void
  146. * @throws Zend_Queue_Exception
  147. */
  148. public function getQueues()
  149. {
  150. throw new Zend_Queue_Exception('getQueues() is not supported in this adapter');
  151. }
  152. /**
  153. * Return the first element in the queue
  154. *
  155. * @param integer $maxMessages
  156. * @param integer $timeout
  157. * @param Zend_Queue $queue
  158. * @return Zend_Queue_Message_Iterator
  159. */
  160. public function receive($maxMessages=null, $timeout=null, Zend_Queue $queue=null)
  161. {
  162. if ($maxMessages === null) {
  163. $maxMessages = 1;
  164. }
  165. if ($timeout === null) {
  166. $timeout = self::RECEIVE_TIMEOUT_DEFAULT;
  167. }
  168. if ($queue === null) {
  169. $queue = $this->_queue;
  170. }
  171. // read
  172. $data = array();
  173. // signal that we are reading
  174. $frame = $this->_client->createFrame();
  175. $frame->setCommand('SUBSCRIBE');
  176. $frame->setHeader('destination', $queue->getName());
  177. $frame->setHeader('ack','client');
  178. $this->_client->send($frame);
  179. if ($maxMessages > 0) {
  180. if ($this->_client->canRead()) {
  181. for ($i = 0; $i < $maxMessages; $i++) {
  182. $response = $this->_client->receive();
  183. switch ($response->getCommand()) {
  184. case 'MESSAGE':
  185. $datum = array(
  186. 'message_id' => $response->getHeader('message-id'),
  187. 'handle' => $response->getHeader('message-id'),
  188. 'body' => $response->getBody(),
  189. 'md5' => md5($response->getBody())
  190. );
  191. $data[] = $datum;
  192. break;
  193. default:
  194. $block = print_r($response, true);
  195. throw new Zend_Queue_Exception('Invalid response received: ' . $block);
  196. }
  197. }
  198. }
  199. }
  200. $options = array(
  201. 'queue' => $queue,
  202. 'data' => $data,
  203. 'messageClass' => $queue->getMessageClass()
  204. );
  205. $classname = $queue->getMessageSetClass();
  206. if (!class_exists($classname)) {
  207. Zend_Loader::loadClass($classname);
  208. }
  209. return new $classname($options);
  210. }
  211. /**
  212. * Push an element onto the end of the queue
  213. *
  214. * @param string $message message to send to the queue
  215. * @param Zend_Queue $queue
  216. * @return Zend_Queue_Message
  217. */
  218. public function send($message, Zend_Queue $queue=null)
  219. {
  220. if ($queue === null) {
  221. $queue = $this->_queue;
  222. }
  223. $frame = $this->_client->createFrame();
  224. $frame->setCommand('SEND');
  225. $frame->setHeader('destination', $queue->getName());
  226. $frame->setHeader('content-length', strlen($message));
  227. $frame->setBody((string) $message);
  228. $this->_client->send($frame);
  229. $data = array(
  230. 'message_id' => null,
  231. 'body' => $message,
  232. 'md5' => md5($message),
  233. 'handle' => null
  234. );
  235. $options = array(
  236. 'queue' => $queue,
  237. 'data' => $data
  238. );
  239. $classname = $queue->getMessageClass();
  240. if (!class_exists($classname)) {
  241. Zend_Loader::loadClass($classname);
  242. }
  243. return new $classname($options);
  244. }
  245. /**
  246. * Returns the length of the queue
  247. *
  248. * @param Zend_Queue $queue
  249. * @return integer
  250. * @throws Zend_Queue_Exception (not supported)
  251. */
  252. public function count(Zend_Queue $queue=null)
  253. {
  254. throw new Zend_Queue_Exception('count() is not supported in this adapter');
  255. }
  256. /**
  257. * Does a queue already exist?
  258. *
  259. * @param string $name
  260. * @return boolean
  261. * @throws Zend_Queue_Exception (not supported)
  262. */
  263. public function isExists($name)
  264. {
  265. throw new Zend_Queue_Exception('isExists() is not supported in this adapter');
  266. }
  267. /**
  268. * Return a list of queue capabilities functions
  269. *
  270. * $array['function name'] = true or false
  271. * true is supported, false is not supported.
  272. *
  273. * @param string $name
  274. * @return array
  275. */
  276. public function getCapabilities()
  277. {
  278. return array(
  279. 'create' => false,
  280. 'delete' => false,
  281. 'send' => true,
  282. 'receive' => true,
  283. 'deleteMessage' => true,
  284. 'getQueues' => false,
  285. 'count' => false,
  286. 'isExists' => false,
  287. );
  288. }
  289. }