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.
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
24 * @see Zend_Queue_Adapter_AdapterAbstract
26 require_once 'Zend/Queue/Adapter/AdapterAbstract.php';
29 * Zend Platform JobQueue adapter
34 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
35 * @license http://framework.zend.com/license/new-bsd New BSD License
37 class Zend_Queue_Adapter_PlatformJobQueue
extends Zend_Queue_Adapter_AdapterAbstract
40 * @var ZendApi_JobQueue
42 protected $_zendQueue;
47 * @param array|Zend_Config $options
48 * @param Zend_Queue|null $queue
51 public function __construct($options, Zend_Queue
$queue = null)
53 parent
::__construct($options, $queue);
55 if (!extension_loaded("jobqueue_client")) {
56 require_once 'Zend/Queue/Exception.php';
57 throw new Zend_Queue_Exception('Platform Job Queue extension does not appear to be loaded');
60 if (! isset($this->_options
['daemonOptions'])) {
61 require_once 'Zend/Queue/Exception.php';
62 throw new Zend_Queue_Exception('Job Queue host and password should be provided');
65 $options = $this->_options
['daemonOptions'];
67 if (!array_key_exists('host', $options)) {
68 require_once 'Zend/Queue/Exception.php';
69 throw new Zend_Queue_Exception('Platform Job Queue host should be provided');
71 if (!array_key_exists('password', $options)) {
72 require_once 'Zend/Queue/Exception.php';
73 throw new Zend_Queue_Exception('Platform Job Queue password should be provided');
76 $this->_zendQueue
= new ZendApi_Queue($options['host']);
78 if (!$this->_zendQueue
) {
79 require_once 'Zend/Queue/Exception.php';
80 throw new Zend_Queue_Exception('Platform Job Queue connection failed');
82 if (!$this->_zendQueue
->login($options['password'])) {
83 require_once 'Zend/Queue/Exception.php';
84 throw new Zend_Queue_Exception('Job Queue login failed');
88 $this->_queue
->setMessageClass('Zend_Queue_Message_PlatformJob');
92 /********************************************************************
93 * Queue management functions
94 ********************************************************************/
97 * Does a queue already exist?
101 * @throws Zend_Queue_Exception (not supported)
103 public function isExists($name)
105 require_once 'Zend/Queue/Exception.php';
106 throw new Zend_Queue_Exception('isExists() is not supported in this adapter');
112 * @param string $name queue name
113 * @param integer $timeout default visibility timeout
115 * @throws Zend_Queue_Exception
117 public function create($name, $timeout=null)
119 require_once 'Zend/Queue/Exception.php';
120 throw new Zend_Queue_Exception('create() is not supported in ' . get_class($this));
124 * Delete a queue and all of its messages
126 * @param string $name queue name
128 * @throws Zend_Queue_Exception
130 public function delete($name)
132 require_once 'Zend/Queue/Exception.php';
133 throw new Zend_Queue_Exception('delete() is not supported in ' . get_class($this));
137 * Get an array of all available queues
140 * @throws Zend_Queue_Exception
142 public function getQueues()
144 require_once 'Zend/Queue/Exception.php';
145 throw new Zend_Queue_Exception('getQueues() is not supported in this adapter');
149 * Return the approximate number of messages in the queue
151 * @param Zend_Queue|null $queue
154 public function count(Zend_Queue
$queue = null)
156 if ($queue !== null) {
157 require_once 'Zend/Queue/Exception.php';
158 throw new Zend_Queue_Exception('Queue parameter is not supported');
161 return $this->_zendQueue
->getNumOfJobsInQueue();
164 /********************************************************************
165 * Messsage management functions
166 ********************************************************************/
169 * Send a message to the queue
171 * @param array | ZendAPI_job $message Message to send to the active queue
172 * @param Zend_Queue $queue Not supported
173 * @return Zend_Queue_Message
174 * @throws Zend_Queue_Exception
176 public function send($message, Zend_Queue
$queue = null)
178 if ($queue !== null) {
179 require_once 'Zend/Queue/Exception.php';
180 throw new Zend_Queue_Exception('Queue parameter is not supported');
183 // This adapter can work only for this message type
184 $classname = $this->_queue
->getMessageClass();
185 if (!class_exists($classname)) {
186 require_once 'Zend/Loader.php';
187 Zend_Loader
::loadClass($classname);
190 if ($message instanceof ZendAPI_Job
) {
191 $message = array('data' => $message);
194 $zendApiJob = new $classname($message);
196 // Unfortunately, the Platform JQ API is PHP4-style...
197 $platformJob = $zendApiJob->getJob();
199 $jobId = $this->_zendQueue
->addJob($platformJob);
202 require_once 'Zend/Queue/Exception.php';
203 throw new Zend_Queue_Exception('Failed to add a job to queue: '
204 . $this->_zendQueue
->getLastError());
207 $zendApiJob->setJobId($jobId);
212 * Get messages in the queue
214 * @param integer $maxMessages Maximum number of messages to return
215 * @param integer $timeout Ignored
216 * @param Zend_Queue $queue Not supported
217 * @throws Zend_Queue_Exception
218 * @return ArrayIterator
220 public function receive($maxMessages = null, $timeout = null, Zend_Queue
$queue = null)
222 if ($maxMessages === null) {
226 if ($queue !== null) {
227 require_once 'Zend/Queue/Exception.php';
228 throw new Zend_Queue_Exception('Queue shouldn\'t be set');
231 $jobs = $this->_zendQueue
->getJobsInQueue(null, $maxMessages, true);
233 $classname = $this->_queue
->getMessageClass();
234 if (!class_exists($classname)) {
235 require_once 'Zend/Loader.php';
236 Zend_Loader
::loadClass($classname);
240 'queue' => $this->_queue
,
242 'messageClass' => $this->_queue
->getMessageClass(),
245 $classname = $this->_queue
->getMessageSetClass();
247 if (!class_exists($classname)) {
248 require_once 'Zend/Loader.php';
249 Zend_Loader
::loadClass($classname);
251 return new $classname($options);
255 * Delete a message from the queue
257 * Returns true if the message is deleted, false if the deletion is
260 * @param Zend_Queue_Message $message
262 * @throws Zend_Queue_Exception
264 public function deleteMessage(Zend_Queue_Message
$message)
266 if (get_class($message) != $this->_queue
->getMessageClass()) {
267 require_once 'Zend/Queue/Exception.php';
268 throw new Zend_Queue_Exception(
269 'Failed to remove job from the queue; only messages of type '
270 . 'Zend_Queue_Message_PlatformJob may be used'
274 return $this->_zendQueue
->removeJob($message->getJobId());
277 public function isJobIdExist($id)
279 return (($this->_zendQueue
->getJob($id))?
true : false);
282 /********************************************************************
283 * Supporting functions
284 ********************************************************************/
287 * Return a list of queue capabilities functions
289 * $array['function name'] = true or false
290 * true is supported, false is not supported.
292 * @param string $name
295 public function getCapabilities()
300 'getQueues' => false,
305 'deleteMessage' => true,
309 /********************************************************************
310 * Functions that are not part of the Zend_Queue_Adapter_AdapterAbstract
311 ********************************************************************/
318 public function __sleep()
320 return array('_options');
328 public function __wakeup()
330 $options = $this->_options
['daemonOptions'];
332 $this->_zendQueue
= new ZendApi_Queue($options['host']);
334 if (!$this->_zendQueue
) {
335 require_once 'Zend/Queue/Exception.php';
336 throw new Zend_Queue_Exception('Platform Job Queue connection failed');
338 if (!$this->_zendQueue
->login($options['password'])) {
339 require_once 'Zend/Queue/Exception.php';
340 throw new Zend_Queue_Exception('Job Queue login failed');