Question

I am new to ZEND and I am working on the version (ZEND 1.11.1) I am trying to implement the ZEND_QUEUE in my zend application and there is no proper tutorial for me. All through I am trying to implement the Queue.

I am developing a Queue for DB. The application works likes the following flow: 1. The user inputs a SQL query through a application and waits for the result. 2. The query will be moves to QUEUE and processed with the Database once the Query successfully done. The query should send the result sent to the user.

Inside my Controller:

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

The Problem which I am facing is I am not able to understand at which folder I want to add the code.

I am not using MODELs in my application as the requirement for the application is to use only CONTROLLER and VIEW.

Also when I try to extent the Zend_Queue_Adapter_Db I am getting the following error:

Fatal error: Class 'ZendJobQueue' not found

or

Fatal error: Uncaught exception 'Zend_Controller_Dispatcher_Exception' with message 'Invalid controller specified (error)' in F:\wamp\www\helloworld\library\Zend\Controller\Dispatcher\Standard.php:242 Stack trace: #0 F:\wamp\www\helloworld\library\Zend\Controller\Front.php(946):

Kindly advice me for the correct folder or any tutorial which help for a beginner to work with ZEND JOBQUEUE.

Was it helpful?

Solution

I got some code useful in the following link:

http://dennisgurnick.com/2011/09/29/zend-queue-with-mysql/

And I have done exactly the same as the website instructed:

; 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"

Added the following code in Boostrap.php file:

<?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;

  }

}

Also Added one more file inside Library folder but I am not clear why this file is added.

<?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;
  }

}

Added the below code in 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" );
}

To be open and frank this code seems to be running as I am receiving a "DONE" as O/P which was coded in die('Done');

If I run the code at first I received an output with the following:

Sent email to minnie.mouse@disney.com (time: current time)<br/>
Done

When I run the same file again it Outputs

Done

Alone, I am a but confused why It gives different out puts at different instance. After few hours if I run the code again it give me the first O/P.

DB Changes:

I have 2 Tables for this application they are:

  1. message
  2. queue

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 ;

The above two are the DB structure. I can see some datas inserted in the Queue table but I don't know which is getting affected?

Kindly help me to understand the structure of the Queue and teh Script.

Is it working well? If I understand the Above Logic and working hopefully I can create a DB Queue for Query Execution in Zend. I am trying hard to get some explanation.

For me Copy & Paste Code is not a good programming, Knowing what I am doing is good!

Thank you! Any help will be highly appreciated!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top