【问题标题】:Zend Framework ZEND_QUEUE implementationZend 框架 ZEND_QUEUE 实现
【发布时间】:2013-04-12 06:03:06
【问题描述】:

我是 ZEND 的新手,我正在开发版本 (ZEND 1.11.1) 我正在尝试在我的 zend 应用程序中实现 ZEND_QUEUE,但没有适合我的教程。我一直在尝试实现队列。

我正在为数据库开发一个队列。该应用程序的工作流程如下: 1. 用户通过应用程序输入 SQL 查询并等待结果。 2. 查询成功完成后,查询将移至 QUEUE 并由数据库处理。查询应该将结果发送给用户。

在我的控制器内部:

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');
     }
}

我面临的问题是我无法理解要在哪个文件夹中添加代码。

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

此外,当我尝试扩展 Zend_Queue_Adapter_Db 时,我收到以下错误:

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

致命错误:在 F:\wamp\www\helloworld\library\Zend\Controller\Dispatcher\Standard.php:242 堆栈跟踪:#0 F 中未捕获异常“Zend_Controller_Dispatcher_Exception”和消息“指定的控制器无效(错误)” :\wamp\www\helloworld\library\Zend\Controller\Front.php(946):

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

【问题讨论】:

    标签: zend-framework zend-db zend-db-table job-queue


    【解决方案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;
    
      }
    
    }
    

    还在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. 留言
    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 中创建一个用于查询执行的数据库队列。我正在努力得到一些解释。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多