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:
- message
- 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!