PageRenderTime 56ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/efdac/e-forest_platform
PHP | 336 lines | 175 code | 35 blank | 126 comment | 19 complexity | 837b92f4011061f402243f9337289f60 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. 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-2010 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. * Constructor
  54. *
  55. * @param array|Zend_Config $config An array having configuration data
  56. * @param Zend_Queue The Zend_Queue object that created this class
  57. * @return void
  58. */
  59. public function __construct($options, Zend_Queue $queue = null)
  60. {
  61. parent::__construct($options);
  62. $options = &$this->_options['driverOptions'];
  63. if (!array_key_exists('scheme', $options)) {
  64. $options['scheme'] = self::DEFAULT_SCHEME;
  65. }
  66. if (!array_key_exists('host', $options)) {
  67. $options['host'] = self::DEFAULT_HOST;
  68. }
  69. if (!array_key_exists('port', $options)) {
  70. $options['port'] = self::DEFAULT_PORT;
  71. }
  72. if (array_key_exists('stompClient', $options)) {
  73. $this->_client = $options['stompClient'];
  74. } else {
  75. $this->_client = new Zend_Queue_Stomp_Client($options['scheme'], $options['host'], $options['port']);
  76. }
  77. $connect = $this->_client->createFrame();
  78. // Username and password are optional on some messaging servers
  79. // such as Apache's ActiveMQ
  80. $connect->setCommand('CONNECT');
  81. if (isset($options['username'])) {
  82. $connect->setHeader('login', $options['username']);
  83. $connect->setHeader('passcode', $options['password']);
  84. }
  85. $response = $this->_client->send($connect)->receive();
  86. if ((false !== $response)
  87. && ($response->getCommand() != 'CONNECTED')
  88. ) {
  89. require_once 'Zend/Queue/Exception.php';
  90. throw new Zend_Queue_Exception("Unable to authenticate to '".$options['scheme'].'://'.$options['host'].':'.$options['port']."'");
  91. }
  92. }
  93. /**
  94. * Close the socket explicitly when destructed
  95. *
  96. * @return void
  97. */
  98. public function __destruct()
  99. {
  100. // Gracefully disconnect
  101. $frame = $this->_client->createFrame();
  102. $frame->setCommand('DISCONNECT');
  103. $this->_client->send($frame);
  104. unset($this->_client);
  105. }
  106. /**
  107. * Create a new queue
  108. *
  109. * @param string $name queue name
  110. * @param integer $timeout default visibility timeout
  111. * @return void
  112. * @throws Zend_Queue_Exception
  113. */
  114. public function create($name, $timeout=null)
  115. {
  116. require_once 'Zend/Queue/Exception.php';
  117. throw new Zend_Queue_Exception('create() is not supported in ' . get_class($this));
  118. }
  119. /**
  120. * Delete a queue and all of its messages
  121. *
  122. * @param string $name queue name
  123. * @return void
  124. * @throws Zend_Queue_Exception
  125. */
  126. public function delete($name)
  127. {
  128. require_once 'Zend/Queue/Exception.php';
  129. throw new Zend_Queue_Exception('delete() is not supported in ' . get_class($this));
  130. }
  131. /**
  132. * Delete a message from the queue
  133. *
  134. * Returns true if the message is deleted, false if the deletion is
  135. * unsuccessful.
  136. *
  137. * @param Zend_Queue_Message $message
  138. * @return boolean
  139. */
  140. public function deleteMessage(Zend_Queue_Message $message)
  141. {
  142. $frame = $this->_client->createFrame();
  143. $frame->setCommand('ACK');
  144. $frame->setHeader('message-id', $message->handle);
  145. $this->_client->send($frame);
  146. return true;
  147. }
  148. /**
  149. * Get an array of all available queues
  150. *
  151. * @return void
  152. * @throws Zend_Queue_Exception
  153. */
  154. public function getQueues()
  155. {
  156. require_once 'Zend/Queue/Exception.php';
  157. throw new Zend_Queue_Exception('getQueues() is not supported in this adapter');
  158. }
  159. /**
  160. * Return the first element in the queue
  161. *
  162. * @param integer $maxMessages
  163. * @param integer $timeout
  164. * @param Zend_Queue $queue
  165. * @return Zend_Queue_Message_Iterator
  166. */
  167. public function receive($maxMessages=null, $timeout=null, Zend_Queue $queue=null)
  168. {
  169. if ($maxMessages === null) {
  170. $maxMessages = 1;
  171. }
  172. if ($timeout === null) {
  173. $timeout = self::RECEIVE_TIMEOUT_DEFAULT;
  174. }
  175. if ($queue === null) {
  176. $queue = $this->_queue;
  177. }
  178. // read
  179. $data = array();
  180. // signal that we are reading
  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. if ($maxMessages > 0) {
  187. if ($this->_client->canRead()) {
  188. for ($i = 0; $i < $maxMessages; $i++) {
  189. $response = $this->_client->receive();
  190. switch ($response->getCommand()) {
  191. case 'MESSAGE':
  192. $datum = array(
  193. 'message_id' => $response->getHeader('message-id'),
  194. 'handle' => $response->getHeader('message-id'),
  195. 'body' => $response->getBody(),
  196. 'md5' => md5($response->getBody())
  197. );
  198. $data[] = $datum;
  199. break;
  200. default:
  201. $block = print_r($response, true);
  202. require_once 'Zend/Queue/Exception.php';
  203. throw new Zend_Queue_Exception('Invalid response received: ' . $block);
  204. }
  205. }
  206. }
  207. }
  208. $options = array(
  209. 'queue' => $queue,
  210. 'data' => $data,
  211. 'messageClass' => $queue->getMessageClass()
  212. );
  213. $classname = $queue->getMessageSetClass();
  214. if (!class_exists($classname)) {
  215. require_once 'Zend/Loader.php';
  216. Zend_Loader::loadClass($classname);
  217. }
  218. return new $classname($options);
  219. }
  220. /**
  221. * Push an element onto the end of the queue
  222. *
  223. * @param string $message message to send to the queue
  224. * @param Zend_Queue $queue
  225. * @return Zend_Queue_Message
  226. */
  227. public function send($message, Zend_Queue $queue=null)
  228. {
  229. if ($queue === null) {
  230. $queue = $this->_queue;
  231. }
  232. $frame = $this->_client->createFrame();
  233. $frame->setCommand('SEND');
  234. $frame->setHeader('destination', $queue->getName());
  235. $frame->setHeader('content-length', strlen($message));
  236. $frame->setBody((string) $message);
  237. $this->_client->send($frame);
  238. $data = array(
  239. 'message_id' => null,
  240. 'body' => $message,
  241. 'md5' => md5($message),
  242. 'handle' => null
  243. );
  244. $options = array(
  245. 'queue' => $queue,
  246. 'data' => $data
  247. );
  248. $classname = $queue->getMessageClass();
  249. if (!class_exists($classname)) {
  250. require_once 'Zend/Loader.php';
  251. Zend_Loader::loadClass($classname);
  252. }
  253. return new $classname($options);
  254. }
  255. /**
  256. * Returns the length of the queue
  257. *
  258. * @param Zend_Queue $queue
  259. * @return integer
  260. * @throws Zend_Queue_Exception (not supported)
  261. */
  262. public function count(Zend_Queue $queue=null)
  263. {
  264. require_once 'Zend/Queue/Exception.php';
  265. throw new Zend_Queue_Exception('count() is not supported in this adapter');
  266. }
  267. /**
  268. * Does a queue already exist?
  269. *
  270. * @param string $name
  271. * @return boolean
  272. * @throws Zend_Queue_Exception (not supported)
  273. */
  274. public function isExists($name)
  275. {
  276. require_once 'Zend/Queue/Exception.php';
  277. throw new Zend_Queue_Exception('isExists() is not supported in this adapter');
  278. }
  279. /**
  280. * Return a list of queue capabilities functions
  281. *
  282. * $array['function name'] = true or false
  283. * true is supported, false is not supported.
  284. *
  285. * @param string $name
  286. * @return array
  287. */
  288. public function getCapabilities()
  289. {
  290. return array(
  291. 'create' => false,
  292. 'delete' => false,
  293. 'send' => true,
  294. 'receive' => true,
  295. 'deleteMessage' => true,
  296. 'getQueues' => false,
  297. 'count' => false,
  298. 'isExists' => false,
  299. );
  300. }
  301. }