Apachemq.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Stomp.php 15165 2009-04-26 14:44:50Z danlo $
  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-2009 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_Apachemq 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. // signal that we are reading
  179. $frame = $this->_client->createFrame();
  180. $frame->setCommand('SUBSCRIBE');
  181. $frame->setHeader('destination', $queue->getName());
  182. $frame->setHeader('ack','client');
  183. $this->_client->send($frame);
  184. // read
  185. $data = array();
  186. if ($this->_client->canRead()) {
  187. for ($i = 0; $i < $maxMessages; $i++) {
  188. $response = $this->_client->receive();
  189. switch ($response->getCommand()) {
  190. case 'MESSAGE':
  191. $datum = array(
  192. 'message_id' => $response->getHeader('message-id'),
  193. 'handle' => $response->getHeader('message-id'),
  194. 'body' => $response->getBody(),
  195. 'md5' => md5($response->getBody())
  196. );
  197. $data[] = $datum;
  198. break;
  199. default:
  200. $block = print_r($response, true);
  201. require_once 'Zend/Queue/Exception.php';
  202. throw new Zend_Queue_Exception('Invalid response received: ' . $block);
  203. }
  204. }
  205. }
  206. $options = array(
  207. 'queue' => $queue,
  208. 'data' => $data,
  209. 'messageClass' => $queue->getMessageClass()
  210. );
  211. $classname = $queue->getMessageSetClass();
  212. if (!class_exists($classname)) {
  213. require_once 'Zend/Loader.php';
  214. Zend_Loader::loadClass($classname);
  215. }
  216. return new $classname($options);
  217. }
  218. /**
  219. * Push an element onto the end of the queue
  220. *
  221. * @param string $message message to send to the queue
  222. * @param Zend_Queue $queue
  223. * @return Zend_Queue_Message
  224. */
  225. public function send($message, Zend_Queue $queue=null)
  226. {
  227. if ($queue === null) {
  228. $queue = $this->_queue;
  229. }
  230. $frame = $this->_client->createFrame();
  231. $frame->setCommand('SEND');
  232. $frame->setHeader('destination', $queue->getName());
  233. $frame->setHeader('content-length', strlen($message));
  234. $frame->setBody((string) $message);
  235. $this->_client->send($frame);
  236. $data = array(
  237. 'message_id' => null,
  238. 'body' => $message,
  239. 'md5' => md5($message),
  240. 'handle' => null
  241. );
  242. $options = array(
  243. 'queue' => $queue,
  244. 'data' => $data
  245. );
  246. $classname = $queue->getMessageClass();
  247. if (!class_exists($classname)) {
  248. require_once 'Zend/Loader.php';
  249. Zend_Loader::loadClass($classname);
  250. }
  251. return new $classname($options);
  252. }
  253. /**
  254. * Returns the length of the queue
  255. *
  256. * @param Zend_Queue $queue
  257. * @return integer
  258. * @throws Zend_Queue_Exception (not supported)
  259. */
  260. public function count(Zend_Queue $queue=null)
  261. {
  262. require_once 'Zend/Queue/Exception.php';
  263. throw new Zend_Queue_Exception('count() is not supported in this adapter');
  264. }
  265. /**
  266. * Does a queue already exist?
  267. *
  268. * @param string $name
  269. * @return boolean
  270. * @throws Zend_Queue_Exception (not supported)
  271. */
  272. public function isExists($name)
  273. {
  274. require_once 'Zend/Queue/Exception.php';
  275. throw new Zend_Queue_Exception('isExists() is not supported in this adapter');
  276. }
  277. /**
  278. * Return a list of queue capabilities functions
  279. *
  280. * $array['function name'] = true or false
  281. * true is supported, false is not supported.
  282. *
  283. * @param string $name
  284. * @return array
  285. */
  286. public function getCapabilities()
  287. {
  288. return array(
  289. 'create' => false,
  290. 'delete' => false,
  291. 'send' => true,
  292. 'receive' => true,
  293. 'deleteMessage' => true,
  294. 'getQueues' => false,
  295. 'count' => false,
  296. 'isExists' => false,
  297. );
  298. }
  299. }