我在以下链接中获得了一些有用的代码:
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;
}
}
还在 Library 文件夹中添加了一个文件,但我不清楚为什么要添加这个文件。
<?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 = "minnie.mouse@disney.com";
$email->to = "mickey.mouse@disney.com";
$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,它是在 die('Done'); 中编码的。
如果我首先运行代码,我会收到以下输出:
Sent email to minnie.mouse@disney.com (time: current time)<br/>
Done
当我再次运行同一个文件时,它输出
Done
独自一人,我很困惑为什么它会在不同的情况下给出不同的输出。几个小时后,如果我再次运行代码,它会给我第一个 O/P。
数据库更改:
我有 2 个用于此应用程序的表,它们是:
- 信息
- 队列
1.留言:
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.队列
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结构。我可以看到 Queue 表中插入了一些数据,但我不知道哪些数据受到影响?
请帮助我了解队列和脚本的结构。
它运作良好吗?如果我理解上述逻辑并希望我可以在 Zend 中创建一个用于查询执行的数据库队列。我正在努力得到一些解释。
对我来说复制和粘贴代码不是一个好的编程,知道我在做什么是好的!
谢谢!任何帮助将不胜感激!