11 * to license@zend.com so we can send you a copy immediately. |
11 * to license@zend.com so we can send you a copy immediately. |
12 * |
12 * |
13 * @category Zend |
13 * @category Zend |
14 * @package Zend_Cloud |
14 * @package Zend_Cloud |
15 * @subpackage QueueService |
15 * @subpackage QueueService |
16 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
16 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) |
17 * @license http://framework.zend.com/license/new-bsd New BSD License |
17 * @license http://framework.zend.com/license/new-bsd New BSD License |
18 */ |
18 */ |
19 |
19 |
20 require_once 'Zend/Cloud/QueueService/Adapter/AbstractAdapter.php'; |
20 require_once 'Zend/Cloud/QueueService/Adapter/AbstractAdapter.php'; |
21 require_once 'Zend/Cloud/QueueService/Exception.php'; |
21 require_once 'Zend/Cloud/QueueService/Exception.php'; |
25 * WindowsAzure adapter for simple queue service. |
25 * WindowsAzure adapter for simple queue service. |
26 * |
26 * |
27 * @category Zend |
27 * @category Zend |
28 * @package Zend_Cloud |
28 * @package Zend_Cloud |
29 * @subpackage QueueService |
29 * @subpackage QueueService |
30 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
30 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) |
31 * @license http://framework.zend.com/license/new-bsd New BSD License |
31 * @license http://framework.zend.com/license/new-bsd New BSD License |
32 */ |
32 */ |
33 class Zend_Cloud_QueueService_Adapter_ZendQueue |
33 class Zend_Cloud_QueueService_Adapter_ZendQueue |
34 extends Zend_Cloud_QueueService_Adapter_AbstractAdapter |
34 extends Zend_Cloud_QueueService_Adapter_AbstractAdapter |
35 { |
35 { |
36 /** |
36 /** |
37 * Options array keys for the Zend_Queue adapter. |
37 * Options array keys for the Zend_Queue adapter. |
38 */ |
38 */ |
39 const ADAPTER = 'adapter'; |
39 const ADAPTER = 'adapter'; |
40 |
40 |
41 /** |
41 /** |
42 * Storage client |
42 * Storage client |
43 * |
43 * |
44 * @var Zend_Queue |
44 * @var Zend_Queue |
45 */ |
45 */ |
46 protected $_queue = null; |
46 protected $_queue = null; |
47 |
47 |
48 /** |
48 /** |
49 * @var array All queues |
49 * @var array All queues |
50 */ |
50 */ |
51 protected $_queues = array(); |
51 protected $_queues = array(); |
52 |
52 |
53 /** |
53 /** |
54 * Constructor |
54 * Constructor |
55 * |
55 * |
56 * @param array|Zend_Config $options |
56 * @param array|Zend_Config $options |
57 * @return void |
57 * @return void |
58 */ |
58 */ |
59 public function __construct ($options = array()) |
59 public function __construct ($options = array()) |
60 { |
60 { |
61 if ($options instanceof Zend_Config) { |
61 if ($options instanceof Zend_Config) { |
85 $this->_queue = new Zend_Queue($adapter, $options); |
85 $this->_queue = new Zend_Queue($adapter, $options); |
86 } catch (Zend_Queue_Exception $e) { |
86 } catch (Zend_Queue_Exception $e) { |
87 throw new Zend_Cloud_QueueService_Exception('Error on create: '.$e->getMessage(), $e->getCode(), $e); |
87 throw new Zend_Cloud_QueueService_Exception('Error on create: '.$e->getMessage(), $e->getCode(), $e); |
88 } |
88 } |
89 } |
89 } |
90 |
90 |
91 /** |
91 /** |
92 * Create a queue. Returns the ID of the created queue (typically the URL). |
92 * Create a queue. Returns the ID of the created queue (typically the URL). |
93 * It may take some time to create the queue. Check your vendor's |
93 * It may take some time to create the queue. Check your vendor's |
94 * documentation for details. |
94 * documentation for details. |
95 * |
95 * |
104 return $name; |
104 return $name; |
105 } catch (Zend_Queue_Exception $e) { |
105 } catch (Zend_Queue_Exception $e) { |
106 throw new Zend_Cloud_QueueService_Exception('Error on queue creation: '.$e->getMessage(), $e->getCode(), $e); |
106 throw new Zend_Cloud_QueueService_Exception('Error on queue creation: '.$e->getMessage(), $e->getCode(), $e); |
107 } |
107 } |
108 } |
108 } |
109 |
109 |
110 /** |
110 /** |
111 * Delete a queue. All messages in the queue will also be deleted. |
111 * Delete a queue. All messages in the queue will also be deleted. |
112 * |
112 * |
113 * @param string $queueId |
113 * @param string $queueId |
114 * @param array $options |
114 * @param array $options |
126 } |
126 } |
127 } catch (Zend_Queue_Exception $e) { |
127 } catch (Zend_Queue_Exception $e) { |
128 throw new Zend_Cloud_QueueService_Exception('Error on queue deletion: '.$e->getMessage(), $e->getCode(), $e); |
128 throw new Zend_Cloud_QueueService_Exception('Error on queue deletion: '.$e->getMessage(), $e->getCode(), $e); |
129 } |
129 } |
130 } |
130 } |
131 |
131 |
132 /** |
132 /** |
133 * List all queues. |
133 * List all queues. |
134 * |
134 * |
135 * @param array $options |
135 * @param array $options |
136 * @return array Queue IDs |
136 * @return array Queue IDs |
141 return $this->_queue->getQueues(); |
141 return $this->_queue->getQueues(); |
142 } catch (Zend_Queue_Exception $e) { |
142 } catch (Zend_Queue_Exception $e) { |
143 throw new Zend_Cloud_QueueService_Exception('Error on listing queues: '.$e->getMessage(), $e->getCode(), $e); |
143 throw new Zend_Cloud_QueueService_Exception('Error on listing queues: '.$e->getMessage(), $e->getCode(), $e); |
144 } |
144 } |
145 } |
145 } |
146 |
146 |
147 /** |
147 /** |
148 * Get a key/value array of metadata for the given queue. |
148 * Get a key/value array of metadata for the given queue. |
149 * |
149 * |
150 * @param string $queueId |
150 * @param string $queueId |
151 * @param array $options |
151 * @param array $options |
160 return $this->_queues[$queueId]->getOptions(); |
160 return $this->_queues[$queueId]->getOptions(); |
161 } catch (Zend_Queue_Exception $e) { |
161 } catch (Zend_Queue_Exception $e) { |
162 throw new Zend_Cloud_QueueService_Exception('Error on fetching queue metadata: '.$e->getMessage(), $e->getCode(), $e); |
162 throw new Zend_Cloud_QueueService_Exception('Error on fetching queue metadata: '.$e->getMessage(), $e->getCode(), $e); |
163 } |
163 } |
164 } |
164 } |
165 |
165 |
166 /** |
166 /** |
167 * Store a key/value array of metadata for the specified queue. |
167 * Store a key/value array of metadata for the specified queue. |
168 * WARNING: This operation overwrites any metadata that is located at |
168 * WARNING: This operation overwrites any metadata that is located at |
169 * $destinationPath. Some adapters may not support this method. |
169 * $destinationPath. Some adapters may not support this method. |
170 * |
170 * |
171 * @param string $queueId |
171 * @param string $queueId |
172 * @param array $metadata |
172 * @param array $metadata |
173 * @param array $options |
173 * @param array $options |
174 * @return void |
174 * @return void |
175 */ |
175 */ |
182 return $this->_queues[$queueId]->setOptions($metadata); |
182 return $this->_queues[$queueId]->setOptions($metadata); |
183 } catch (Zend_Queue_Exception $e) { |
183 } catch (Zend_Queue_Exception $e) { |
184 throw new Zend_Cloud_QueueService_Exception('Error on setting queue metadata: '.$e->getMessage(), $e->getCode(), $e); |
184 throw new Zend_Cloud_QueueService_Exception('Error on setting queue metadata: '.$e->getMessage(), $e->getCode(), $e); |
185 } |
185 } |
186 } |
186 } |
187 |
187 |
188 /** |
188 /** |
189 * Send a message to the specified queue. |
189 * Send a message to the specified queue. |
190 * |
190 * |
191 * @param string $queueId |
191 * @param string $queueId |
192 * @param string $message |
192 * @param string $message |
193 * @param array $options |
193 * @param array $options |
194 * @return string Message ID |
194 * @return string Message ID |
195 */ |
195 */ |
202 return $this->_queues[$queueId]->send($message); |
202 return $this->_queues[$queueId]->send($message); |
203 } catch (Zend_Queue_Exception $e) { |
203 } catch (Zend_Queue_Exception $e) { |
204 throw new Zend_Cloud_QueueService_Exception('Error on sending message: '.$e->getMessage(), $e->getCode(), $e); |
204 throw new Zend_Cloud_QueueService_Exception('Error on sending message: '.$e->getMessage(), $e->getCode(), $e); |
205 } |
205 } |
206 } |
206 } |
207 |
207 |
208 /** |
208 /** |
209 * Recieve at most $max messages from the specified queue and return the |
209 * Recieve at most $max messages from the specified queue and return the |
210 * message IDs for messages recieved. |
210 * message IDs for messages recieved. |
211 * |
211 * |
212 * @param string $queueId |
212 * @param string $queueId |
213 * @param int $max |
213 * @param int $max |
214 * @param array $options |
214 * @param array $options |
215 * @return array |
215 * @return array |
216 */ |
216 */ |
228 } |
228 } |
229 } catch (Zend_Queue_Exception $e) { |
229 } catch (Zend_Queue_Exception $e) { |
230 throw new Zend_Cloud_QueueService_Exception('Error on recieving messages: '.$e->getMessage(), $e->getCode(), $e); |
230 throw new Zend_Cloud_QueueService_Exception('Error on recieving messages: '.$e->getMessage(), $e->getCode(), $e); |
231 } |
231 } |
232 } |
232 } |
233 |
233 |
234 /** |
234 /** |
235 * Create Zend_Cloud_QueueService_Message array for |
235 * Create Zend_Cloud_QueueService_Message array for |
236 * Azure messages. |
236 * Azure messages. |
237 * |
237 * |
238 * @param array $messages |
238 * @param array $messages |
239 * @return Zend_Cloud_QueueService_Message[] |
239 * @return Zend_Cloud_QueueService_Message[] |
240 */ |
240 */ |
241 protected function _makeMessages($messages) |
241 protected function _makeMessages($messages) |
242 { |
242 { |
246 foreach ($messages as $message) { |
246 foreach ($messages as $message) { |
247 $result[] = new $messageClass($message->body, $message); |
247 $result[] = new $messageClass($message->body, $message); |
248 } |
248 } |
249 return new $setClass($result); |
249 return new $setClass($result); |
250 } |
250 } |
251 |
251 |
252 /** |
252 /** |
253 * Delete the specified message from the specified queue. |
253 * Delete the specified message from the specified queue. |
254 * |
254 * |
255 * @param string $queueId |
255 * @param string $queueId |
256 * @param Zend_Cloud_QueueService_Message $message Message ID or message |
256 * @param Zend_Cloud_QueueService_Message $message Message ID or message |
257 * @param array $options |
257 * @param array $options |
258 * @return void |
258 * @return void |
259 */ |
259 */ |
260 public function deleteMessage($queueId, $message, $options = null) |
260 public function deleteMessage($queueId, $message, $options = null) |
261 { |
261 { |
267 $message = $message->getMessage(); |
267 $message = $message->getMessage(); |
268 } |
268 } |
269 if (!($message instanceof Zend_Queue_Message)) { |
269 if (!($message instanceof Zend_Queue_Message)) { |
270 throw new Zend_Cloud_QueueService_Exception('Cannot delete the message: Zend_Queue_Message object required'); |
270 throw new Zend_Cloud_QueueService_Exception('Cannot delete the message: Zend_Queue_Message object required'); |
271 } |
271 } |
272 |
272 |
273 return $this->_queues[$queueId]->deleteMessage($message); |
273 return $this->_queues[$queueId]->deleteMessage($message); |
274 } catch (Zend_Queue_Exception $e) { |
274 } catch (Zend_Queue_Exception $e) { |
275 throw new Zend_Cloud_QueueService_Exception('Error on deleting a message: '.$e->getMessage(), $e->getCode(), $e); |
275 throw new Zend_Cloud_QueueService_Exception('Error on deleting a message: '.$e->getMessage(), $e->getCode(), $e); |
276 } |
276 } |
277 } |
277 } |
278 |
278 |
279 /** |
279 /** |
280 * Peek at the messages from the specified queue without removing them. |
280 * Peek at the messages from the specified queue without removing them. |
281 * |
281 * |
282 * @param string $queueId |
282 * @param string $queueId |
283 * @param int $num How many messages |
283 * @param int $num How many messages |
287 public function peekMessages($queueId, $num = 1, $options = null) |
287 public function peekMessages($queueId, $num = 1, $options = null) |
288 { |
288 { |
289 require_once 'Zend/Cloud/OperationNotAvailableException.php'; |
289 require_once 'Zend/Cloud/OperationNotAvailableException.php'; |
290 throw new Zend_Cloud_OperationNotAvailableException('ZendQueue doesn\'t currently support message peeking'); |
290 throw new Zend_Cloud_OperationNotAvailableException('ZendQueue doesn\'t currently support message peeking'); |
291 } |
291 } |
292 |
292 |
293 /** |
293 /** |
294 * Get Azure implementation |
294 * Get Azure implementation |
295 * @return Zend_Queue |
295 * @return Zend_Queue |
296 */ |
296 */ |
297 public function getClient() |
297 public function getClient() |
298 { |
298 { |
299 return $this->_queue; |
299 return $this->_queue; |
300 } |
300 } |