PageRenderTime 75ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

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

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