Zend_Cloud_QueueService.xml 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.cloud.queueservice">
  4. <title>Queue Service Introduction</title>
  5. <para>
  6. The QueueService implements access to message queues available as local or remote services.
  7. The simple queues that QueueService supports implement a messaging pattern that enables
  8. different processes to exchange messages in a reliable and scalable way. One common use case
  9. for such message queues is job dispatching, in which a frontend web server adds a complex
  10. job to a queue for a backend worker to do the expensive processing. The frontend web server
  11. can then return the page without waiting for the work to be completed.
  12. </para>
  13. <para>
  14. The interface <classname>Zend_Cloud_QueueService_Adapter</classname> defines the methods
  15. which concrete queue service adapters must implement. The following adapters are shipped
  16. with the Simple Cloud API:
  17. </para>
  18. <itemizedlist>
  19. <listitem>
  20. <para>
  21. <ulink
  22. url="http://aws.amazon.com/sqs/"><classname>Zend_Cloud_QueueService_Adapter_Sqs</classname></ulink>
  23. </para>
  24. </listitem>
  25. <listitem>
  26. <para>
  27. <ulink
  28. url="http://msdn.microsoft.com/en-us/library/dd179363.aspx"><classname>Zend_Cloud_QueueService_Adapter_WindowsAzure</classname></ulink>
  29. </para>
  30. </listitem>
  31. <listitem>
  32. <para>
  33. <ulink
  34. url="http://aws.amazon.com/sqs/"><classname>Zend_Cloud_QueueService_Adapter_ZendQueue</classname></ulink>
  35. </para>
  36. </listitem>
  37. </itemizedlist>
  38. <sect2 id="zend.cloud.queueservice.adapters">
  39. <title>Instantiating and Configuring QueueService Adapters</title>
  40. <para>
  41. To instantiate a QueueService adapter, use the static method
  42. <methodname>Zend_Cloud_QueueService_Factory::getAdapter()</methodname>, which accepts
  43. either an array or a <classname>Zend_Config</classname> object. Three parameters apply
  44. to all adapters, while the remaining
  45. parameters are adapter-specific properties; these adapter-specific properties often
  46. contain access details.
  47. </para>
  48. <para>
  49. The general parameters are as follows:
  50. </para>
  51. <itemizedlist>
  52. <listitem>
  53. <para><varname>queue_adapter</varname> specifies the concrete adapter class;</para>
  54. </listitem>
  55. <listitem>
  56. <para>
  57. <varname>message_class</varname> specifies the class to use to
  58. represent queue messages; defaults to
  59. <classname>Zend_Cloud_QueueService_Message</classname>; and
  60. </para>
  61. </listitem>
  62. <listitem>
  63. <para>
  64. <varname>messageset_class</varname> specifies the class to use
  65. to represent collections of queue messages; defaults to
  66. <classname>Zend_Cloud_QueueService_MesageSet</classname>.
  67. </para>
  68. </listitem>
  69. </itemizedlist>
  70. <example id="zend.cloud.queueservice.factory.example">
  71. <title>Instantiating an Amazon SQS adapter via the factory</title>
  72. <programlisting language="php"><![CDATA[
  73. $queues = Zend_Cloud_QueueService_Factory::getAdapter(array(
  74. Zend_Cloud_QueueService_Factory::QUEUE_ADAPTER_KEY => 'Zend_Cloud_QueueService_Adapter_Sqs',
  75. Zend_Cloud_QueueService_Adapter_Sqs::AWS_ACCESS_KEY => $amazonKey,
  76. Zend_Cloud_QueueService_Adapter_Sqs::AWS_SECRET_KEY => $amazonSecret,
  77. ));
  78. ]]></programlisting>
  79. </example>
  80. <sect3 id="zend.cloud.queueservice.adapters.options">
  81. <title>Service-Specific Options</title>
  82. <table frame='all' id="zend.cloud.queueservice.adapters.options.sqs">
  83. <title>Zend_Cloud_QueueService_Adapter_Sqs Options</title>
  84. <tgroup cols="5">
  85. <thead>
  86. <row>
  87. <entry>Option key</entry>
  88. <entry>Description</entry>
  89. <entry>Used in</entry>
  90. <entry>Required</entry>
  91. <entry>Default</entry>
  92. </row>
  93. </thead>
  94. <tbody>
  95. <row>
  96. <entry>aws_accesskey</entry>
  97. <entry>Your Amazon AWS access key</entry>
  98. <entry>Constructor</entry>
  99. <entry>Yes</entry>
  100. <entry>None</entry>
  101. </row>
  102. <row>
  103. <entry>aws_secretkey</entry>
  104. <entry>Your Amazon AWS secret key</entry>
  105. <entry>Constructor</entry>
  106. <entry>Yes</entry>
  107. <entry>None</entry>
  108. </row>
  109. <row>
  110. <entry>http_adapter</entry>
  111. <entry>HTTP adapter to use in all access operations</entry>
  112. <entry>Constructor</entry>
  113. <entry>No</entry>
  114. <entry><classname>Zend_Http_Client_Adapter_Socket</classname></entry>
  115. </row>
  116. <row>
  117. <entry>http_adapter</entry>
  118. <entry>HTTP adapter to use in all access operations</entry>
  119. <entry>Constructor</entry>
  120. <entry>No</entry>
  121. <entry><classname>Zend_Http_Client_Adapter_Socket</classname></entry>
  122. </row>
  123. <row>
  124. <entry>visibility_timeout</entry>
  125. <entry>Message visibility timeout</entry>
  126. <entry><methodname>receiveMessages()</methodname></entry>
  127. <entry>No</entry>
  128. <entry>60</entry>
  129. </row>
  130. </tbody>
  131. </tgroup>
  132. </table>
  133. <table frame='all' id="zend.cloud.queueservice.adapters.options.azure">
  134. <title>Zend_Cloud_QueueService_Adapter_WindowsAzure Options</title>
  135. <tgroup cols="5">
  136. <thead>
  137. <row>
  138. <entry>Option key</entry>
  139. <entry>Description</entry>
  140. <entry>Used in</entry>
  141. <entry>Required</entry>
  142. <entry>Default</entry>
  143. </row>
  144. </thead>
  145. <tbody>
  146. <row>
  147. <entry>storage_accountname</entry>
  148. <entry>Windows Azure account name</entry>
  149. <entry>Constructor</entry>
  150. <entry>Yes</entry>
  151. <entry>None</entry>
  152. </row>
  153. <row>
  154. <entry>storage_accountkey</entry>
  155. <entry>Windows Azure account key</entry>
  156. <entry>Constructor</entry>
  157. <entry>Yes</entry>
  158. <entry>None</entry>
  159. </row>
  160. <row>
  161. <entry>storage_host</entry>
  162. <entry>Windows Azure access host</entry>
  163. <entry>Constructor</entry>
  164. <entry>No</entry>
  165. <entry><varname>queue.core.windows.net</varname></entry>
  166. </row>
  167. <row>
  168. <entry>storage_proxy_host</entry>
  169. <entry>Proxy hostname</entry>
  170. <entry>Constructor</entry>
  171. <entry>No</entry>
  172. <entry>None</entry>
  173. </row>
  174. <row>
  175. <entry>storage_proxy_port</entry>
  176. <entry>Proxy port</entry>
  177. <entry>Constructor</entry>
  178. <entry>No</entry>
  179. <entry>8080</entry>
  180. </row>
  181. <row>
  182. <entry>storage_proxy_credentials</entry>
  183. <entry>Proxy credentials</entry>
  184. <entry>Constructor</entry>
  185. <entry>No</entry>
  186. <entry>None</entry>
  187. </row>
  188. <row>
  189. <entry>http_adapter</entry>
  190. <entry>HTTP adapter to use in all access operations</entry>
  191. <entry>Constructor</entry>
  192. <entry>No</entry>
  193. <entry><classname>Zend_Http_Client_Adapter_Socket</classname></entry>
  194. </row>
  195. <row>
  196. <entry>visibility_timeout</entry>
  197. <entry>Message visibility timeout</entry>
  198. <entry><methodname>receiveMessages()</methodname></entry>
  199. <entry>No</entry>
  200. <entry>60</entry>
  201. </row>
  202. <row>
  203. <entry>prefix</entry>
  204. <entry>
  205. Filter the results to only queue names beginning with given prefix
  206. </entry>
  207. <entry><methodname>listQueues()</methodname></entry>
  208. <entry>No</entry>
  209. <entry>None</entry>
  210. </row>
  211. <row>
  212. <entry>max_results</entry>
  213. <entry>Limit queue list to certain number of results</entry>
  214. <entry><methodname>listQueues()</methodname></entry>
  215. <entry>No</entry>
  216. <entry>5,000</entry>
  217. </row>
  218. <row>
  219. <entry>ttl</entry>
  220. <entry>Set time-to-live for message</entry>
  221. <entry><methodname>sendMessage()</methodname></entry>
  222. <entry>No</entry>
  223. <entry>7 days</entry>
  224. </row>
  225. </tbody>
  226. </tgroup>
  227. </table>
  228. <table frame='all' id="zend.cloud.queueservice.adapters.options.zend-queue">
  229. <title>Zend_Cloud_QueueService_Adapter_ZendQueue Options</title>
  230. <tgroup cols="5">
  231. <thead>
  232. <row>
  233. <entry>Option key</entry>
  234. <entry>Description</entry>
  235. <entry>Used in</entry>
  236. <entry>Required</entry>
  237. <entry>Default</entry>
  238. </row>
  239. </thead>
  240. <tbody>
  241. <row>
  242. <entry>adapter</entry>
  243. <entry>
  244. Concrete <classname>Zend_Queue</classname> adapter to use. See the
  245. <link linkend="zend.queue">Zend_Queue</link> documentation for supported
  246. adapters and their options.</entry>
  247. <entry>Constructor</entry>
  248. <entry>No</entry>
  249. <entry>Filesystem</entry>
  250. </row>
  251. <row>
  252. <entry>timeout</entry>
  253. <entry>Visibility timeout for messages</entry>
  254. <entry>
  255. <methodname>createQueue()</methodname>,
  256. <methodname>receiveMessages()</methodname>
  257. </entry>
  258. <entry>No</entry>
  259. <entry>30</entry>
  260. </row>
  261. </tbody>
  262. </tgroup>
  263. </table>
  264. </sect3>
  265. </sect2>
  266. <sect2 id="zend.cloud.queueservice.concepts">
  267. <title>Basic concepts</title>
  268. <para>
  269. Every queue service typically offers one or more <emphasis>queues</emphasis>. Each queue
  270. can store zero or more <emphasis>messages</emphasis>. A process can send a message to a
  271. queue, and another process can remove it. Usually processes remove the oldest message in
  272. the queue, observing a first in, first out (FIFO) queue-style interface.
  273. </para>
  274. </sect2>
  275. <sect2 id="zend.cloud.queueservice.exceptions">
  276. <title>Exceptions</title>
  277. <para>
  278. If some error occurs inside the storage service, a
  279. <classname>Zend_Cloud_QueueService_Exception</classname> is thrown. If the exception was
  280. caused by underlying service driver, you can use the
  281. <methodname>getClientException()</methodname> method to retrieve the original exception.
  282. </para>
  283. <para>
  284. Since different cloud providers implement different sets of services, some adapters do
  285. not implement certain features. In this case, the
  286. <classname>Zend_Cloud_OperationNotAvailableException</classname> exception is thrown.
  287. </para>
  288. </sect2>
  289. <sect2 id="zend.cloud.queueservice.create-queue">
  290. <title>Create a queue</title>
  291. <para>
  292. The <methodname>createQueue()</methodname> method creates a message queue with the given
  293. name. It returns a queue identifier, the format of which is service-dependent. Some
  294. services return a URL for the queue identifier, while others return a GUID to use in
  295. future operations.
  296. </para>
  297. <example id="zend.cloud.queueservice.create-queue.example">
  298. <title>Creating a queue</title>
  299. <programlisting language="php"><![CDATA[
  300. $queueId = $queues->createQueue('my-queue');
  301. ]]></programlisting>
  302. </example>
  303. </sect2>
  304. <sect2 id="zend.cloud.queueservice.delete-queue">
  305. <title>Delete a queue</title>
  306. <para>
  307. The <methodname>deleteQueue()</methodname> method removes the queue from the service.
  308. You must use the identifier received from <methodname>createQueue()</methodname> when
  309. calling <methodname>deleteQueue()</methodname>.
  310. </para>
  311. <example id="zend.cloud.queueservice.delete-queue.example">
  312. <title>Deleting a queue</title>
  313. <programlisting language="php"><![CDATA[
  314. $queueId = $queues->createQueue('my-queue');
  315. // ... do stuff ...
  316. $queues->deleteQueue($queueId);
  317. ]]></programlisting>
  318. </example>
  319. <note>
  320. <para>
  321. Deleting a queue can take significant time for some services. Typically, you cannot
  322. re-create a queue with the same name until the original queue is fully removed.
  323. </para>
  324. </note>
  325. </sect2>
  326. <sect2 id="zend.cloud.queueservice.list">
  327. <title>List queues</title>
  328. <para>
  329. To retrieve the list of all queues in the system, use the
  330. <methodname>listQueues()</methodname> method.
  331. </para>
  332. <example id="zend.cloud.queueservice.list.example">
  333. <title>Listing queues</title>
  334. <programlisting language="php"><![CDATA[
  335. $names = $queues->listQueues();
  336. foreach ($names as $name) {
  337. echo "Found queue $name\n";
  338. }
  339. ]]></programlisting>
  340. </example>
  341. </sect2>
  342. <sect2 id="zend.cloud.queueservice.store-metadata">
  343. <title>Set queue metadata</title>
  344. <para>
  345. In some services, you can associate a set of key-value pairs with the queue as queue
  346. metadata. To set queue metadata, use the <methodname>storeQueueMetadata()</methodname>
  347. method:
  348. </para>
  349. <example id="zend.cloud.queueservice.store-metadata.example">
  350. <title>Setting queue metadata</title>
  351. <programlisting language="php"><![CDATA[
  352. $queues->storeQueueMetadata($queueId, array(
  353. 'purpose' => 'Operations',
  354. 'administrator' => 'joe@example.com',
  355. ));
  356. ]]></programlisting>
  357. </example>
  358. </sect2>
  359. <sect2 id="zend.cloud.queueservice.fetch-metadata">
  360. <title>Fetch queue metadata</title>
  361. <para>
  362. To retrieve queue metadata, use the <methodname>fetchQueueMetadata()</methodname>
  363. method.
  364. </para>
  365. <example id="zend.cloud.queueservice.fetch-metadata.example">
  366. <title>Fetching queue metadata</title>
  367. <programlisting language="php"><![CDATA[
  368. $metadata = $queues->fetchQueueMetadata($queueId);
  369. foreac h($metadata as $key => $value) {
  370. echo "Metadata $key: $value\n";
  371. }
  372. ]]></programlisting>
  373. </example>
  374. </sect2>
  375. <sect2 id="zend.cloud.queueservice.send">
  376. <title>Send a message</title>
  377. <para>
  378. To add a message to a queue, use the <methodname>sendMessage()</methodname> method. The
  379. message is passed as an unstructured string.
  380. </para>
  381. <example id="zend.cloud.queueservice.send.example">
  382. <title>Sending a message</title>
  383. <programlisting language="php"><![CDATA[
  384. $queues->sendMessage($queueId, "Hello world!");
  385. ]]></programlisting>
  386. </example>
  387. </sect2>
  388. <sect2 id="zend.cloud.queueservice.receive">
  389. <title>Receive a message</title>
  390. <para>
  391. To receive one or more messages from the queue, use the
  392. <methodname>receiveMessages()</methodname> method. This method returns a
  393. <classname>Zend_Cloud_QueueService_MessageSet</classname> instance by default, unless
  394. configured otherwise. Each element of the MessageSet is an instance of
  395. <classname>Zend_Cloud_QueueService_Message</classname> by default, unless configuired
  396. otherwise.
  397. </para>
  398. <example id="zend.cloud.queueservice.receive.example">
  399. <title>Receiving a message</title>
  400. <programlisting language="php"><![CDATA[
  401. // Get the first message
  402. $messages = $queues->receiveMessages($queueId);
  403. if (count($messages)) {
  404. foreach ($messages as $message) {
  405. echo "Message: " . $message->getBody();
  406. break;
  407. }
  408. }
  409. // Get two messages
  410. $messages = $queues->receiveMessages($queueId, 2);
  411. ]]></programlisting>
  412. </example>
  413. <para>
  414. When a message is received, it is not visible to other clients. It is not deleted from
  415. the queue, however, until the client that has received the message calls the
  416. <methodname>deleteMessage()</methodname> method. If it is not deleted during the
  417. specfied visibility timeout, it will become visible to all other clients again. In other
  418. words, all clients will be able to retrieve the message with the
  419. <methodname>receiveMessages()</methodname> method if the visibility timeout is exceeded.
  420. </para>
  421. </sect2>
  422. <sect2 id="zend.cloud.queueservice.delete">
  423. <title>Delete a message</title>
  424. <para>
  425. In order to delete the message from the queue, use the
  426. <methodname>deleteMessage()</methodname> method. This method deletes the specified
  427. message.
  428. </para>
  429. <example id="zend.cloud.queueservice.delete.example">
  430. <title>Deleting a message</title>
  431. <programlisting language="php"><![CDATA[
  432. // process and delete $max messages
  433. $messages = $queues->receiveMessages($queueId, $max);
  434. if (count($messages)) {
  435. foreach ($messages as $message) {
  436. process($message);
  437. $queues->deleteMessage($queueId, $message);
  438. }
  439. }
  440. ]]></programlisting>
  441. </example>
  442. </sect2>
  443. <sect2 id="zend.cloud.queueservice.concreteadapters">
  444. <title>Accessing concrete adapters</title>
  445. <para>
  446. Sometimes it is necessary to retrieve the concrete adapter for the service that the
  447. Queue API is working with. This can be achieved by using the
  448. <methodname>getAdapter()</methodname> method.
  449. </para>
  450. <note>
  451. <para>
  452. Accessing the underlying adapter breaks portability among services, so it should be
  453. reserved for exceptional circumstances only.
  454. </para>
  455. </note>
  456. <example id="zend.cloud.queueservice.concreteadapters.example">
  457. <title>Using a concrete adapter</title>
  458. <programlisting language="php"><![CDATA[
  459. // send the message directly with the SQS client library
  460. $sqs = $queues->getAdapter();
  461. $sqs->sendMessage("myQueue", "hello!");
  462. ]]></programlisting>
  463. </example>
  464. </sect2>
  465. </sect1>