我是ZEND的新手,我正在开发Zend1.11.1版本,我试图在我的zend应用程序中实现ZEND_QUEUE,但是没有适合我的教程。从头到尾,我都在尝试实现队列。
我正在为DB开发一个队列。该应用程序的工作方式类似于以下流程: 1.用户通过应用程序输入SQL查询并等待结果。2.一旦查询成功完成,查询将被移动到队列并使用数据库进行处理。查询应将结果发送给用户。
在我的控制器内部:
class IndexController extends Zend_Controller_Action
{
public function indexAction()
{
$options = array(
'name' => 'queue1',
'driverOptions' => array(
'host' => '127.0.0.1',
'port' => '3306',
'username' => 'queue',
'password' => 'queue',
'dbname' => 'queue',
'type' => 'pdo_mysql'
)
);
// Create a database queue.
// Zend_Queue will prepend Zend_Queue_Adapter_ to 'Db' for the class name.
$queue = new Zend_Queue('Db', $options);
foreach ($queue->getQueues() as $name) {
echo $name, "\n";
}
$queue->send('My Test Message');
}
}我面临的问题是我不能理解我想要在哪个文件夹中添加代码。
我没有在我的应用程序中使用模型,因为应用程序的要求是只使用控制器和视图。
另外,当我尝试扩展Zend_Queue_Adapter_Db时,我得到了以下错误:
致命错误:找不到类'ZendJobQueue‘
或
致命错误:在F:\wamp\www\helloworld\library\Zend\Controller\Dispatcher\Standard.php:242堆栈跟踪:#0 F:\wamp\www\helloworld\library\Zend\Controller\Front.php(946):中未捕获异常'Zend_Controller_Dispatcher_Exception‘,消息为'Invalid controller specified (error)’
请告诉我正确的文件夹或任何帮助初学者使用ZEND JOBQUEUE的教程。
发布于 2013-04-16 18:01:06
我在下面的链接中得到了一些有用的代码:
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 = "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“as O/P,它是在die(”Done“)中编码的;
如果我一开始运行代码,我会收到如下输出:
Sent email to minnie.mouse@disney.com (time: current time)<br/>
Done当我再次运行相同的文件时,它输出
Done独自一人,我很困惑为什么它会在不同的情况下给出不同的输出。几个小时后,如果我再次运行代码,它会给我第一个O/P。
数据库更改:
对于这个应用程序,我有两个表,它们是:
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.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结构。我可以看到一些数据插入到队列表中,但我不知道哪个数据受到了影响?
请帮助我了解队列的结构和脚本。
它工作得很好吗?如果我理解上面的逻辑,并希望我能在Zend中创建一个用于查询执行的DB队列。我很努力地想得到一些解释。
对我来说,复制和粘贴代码不是一个好的编程,知道我在做什么是好的!
谢谢!任何帮助我们都将不胜感激!
https://stackoverflow.com/questions/15964182
复制相似问题