我得到了一些代碼在以下鏈接有用:
http://dennisgurnick.com/2011/09/29/zend-queue-with-mysql/
而且我已經做了完全一樣的網站批示:
; file: application/configs/application.ini
[production]
; ...
resources.frontController.params.displayExceptions = 0
; ...
queue.driverOptions.type = "pdo_mysql"
queue.driverOptions.host = "localhost"
queue.driverOptions.username = "howtoqueue"
queue.driverOptions.password = "howtoqueue"
queue.driverOptions.dbname = "howtoqueue"
添加以下代碼Boostrap.php文件:
<?php
// file: application/Bootstrap.php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initQueue()
{
$options = $this->getOptions();
$queueAdapter = new Zend_Queue_Adapter_Db($options[ 'queue' ]);
Zend_Registry::getInstance()->queueAdapter = $queueAdapter;
}
}
另外還增加了一個文件裏面庫文件夾,但我不清楚爲什麼添加這個文件。
<?php
// file: library/EmailPopo.php
class EmailPopo
{
private $_data = array();
public function __get($key) {
return $this->_data[$key];
}
public function __set($key, $value) {
$this->_data[$key] = $value;
}
}
添加以下代碼中的IndexController:
// file: application/controllers/IndexController.php
require_once "EmailPopo.php";
public function indexAction()
{
$queueAdapter = Zend_Registry::getInstance()->queueAdapter;
$options = array('name' => 'emailqueue');
$queue = new Zend_Queue($queueAdapter, $options);
$email = new EmailPopo();
$email->date = time();
$email->from = "[email protected]";
$email->to = "[email protected]";
$email->subject = "I want a divorce";
$email->body = "Letter's in the mail.";
$message = base64_encode(gzcompress(serialize($email)));
$queue->send($message);
}
public function queueAction() {
$queueAdapter = Zend_Registry::getInstance()->queueAdapter;
$options = array('name' => 'emailqueue');
$queue = new Zend_Queue($queueAdapter, $options);
$messages = $queue->receive(2);
foreach($messages as $message) {
try {
$email = unserialize(gzuncompress(base64_decode($message->body)));
$queue->deleteMessage($message);
echo sprintf(
"Sent email to %s (time: %s)<br/>",
$email->to,
new Zend_Date($email->date)
);
} catch(Exception $ex) {
echo "Kaboom!: " . $ex->getMessage() . "<br/>";
}
}
die("Done");
}
要公開和坦率此代碼似乎運行,因爲我接收到「DONE」作爲O/P這是在模頭譯碼( '完成');
如果我在第一次運行的代碼我收到與以下的輸出:
Sent email to [email protected] (time: current time)<br/>
Done
當我再次運行同一個文件時,它的輸出
Done
當家,我是一個,但是困惑,爲什麼它在不同的實例中給出了不同的輸出。幾個小時後,如果我再次運行代碼,它會給我第一個O/P。
DB的變化:
我有2個表對於該應用,他們是:
- 消息
- 隊列
1.Message:
CREATE TABLE IF NOT EXISTS `message` (
`message_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`queue_id` int(10) unsigned NOT NULL,
`handle` char(32) DEFAULT NULL,
`body` varchar(8192) NOT NULL,
`md5` char(32) NOT NULL,
`timeout` decimal(14,4) unsigned DEFAULT NULL,
`created` int(10) unsigned NOT NULL,
PRIMARY KEY (`message_id`),
UNIQUE KEY `message_handle` (`handle`),
KEY `message_queueid` (`queue_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=15 ;
ALTER TABLE `message`
ADD CONSTRAINT `message_ibfk_1` FOREIGN KEY (`queue_id`) REFERENCES `queue` (`queue_id`) ON DELETE CASCADE ON UPDATE CASCADE;
2 .Queue
CREATE TABLE IF NOT EXISTS `queue` (
`queue_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`queue_name` varchar(100) NOT NULL,
`timeout` smallint(5) unsigned NOT NULL DEFAULT '30',
PRIMARY KEY (`queue_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=9 ;
以上兩個是DB結構。 我可以看到插入隊列表中的一些數據,但我不知道哪些數據會受到影響?
請幫助我理解隊列和腳本的結構。
它工作正常嗎?如果我理解了Above邏輯並希望能夠在Zend中創建用於查詢執行的DB隊列。我正在努力解釋。
對我複製&粘貼代碼不是一個好的編程,知道我在做什麼是好的!
謝謝!任何幫助將不勝感激!
http://stackoverflow.com/questions/15971532/query-builder-using-zend-framework – TomPHP