2013-04-12 61 views
1

我是ZEND新手,我正在使用该版本(ZEND 1.11.1)我正在尝试在我的zend应用程序中实现ZEND_QUEUE,并且没有适合我的教程。通过我试图实现队列。Zend Framework 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'); 
    } 
} 

,我现在面临是我不能够以我想添加的代码文件夹来理解这个问题。

我没有在我的应用程序中使用MODEL,因为应用程序的要求是仅使用CONTROLLER和VIEW。

此外,当我试图程度Zend_Queue_Adapter_Db我收到以下错误:

致命错误:类“ZendJobQueue”未找到

致命错误:未捕获的异常“Zend_Controller_Dispatcher_Exception”与在F:\ wamp \ www \ helloworld \ library \ Zend \ Controller \ Dispatcher \ Standard.php:242堆栈跟踪:#0 F:\ wamp \ www \ helloworld \ library \ Zend \ Controller \ Front.php(946):

请向我建议正确的文件夹或帮助初学者使用ZEND JOBQUEUE的任何教程。

回答

1

我得到了一些代码在以下链接有用:

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. 消息
  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队列。我正在努力解释。

对我复制&粘贴代码不是一个好的编程,知道我在做什么是好的!

谢谢!任何帮助将不胜感激!

+0

http://stackoverflow.com/questions/15971532/query-builder-using-zend-framework – TomPHP