Zend Framework Source Code Snippets

Zend_Queue simple example for offline processing

Bookmark and Share
1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
19
2021
22
23
24
2526
27
28
29
3031
32
33
34
3536
37
38
39
4041
42
43
44
4546
47
48
49
5051
52
53
54
5556
57
58
59
6061
62
63
64
6566
67
68
69
7071
72
73
74
7576
77
78
79
8081
82
83
84
8586
87
88
89
9091
92
93
94
9596
97
98
99
100101
102
103
104
105106
107
108
109
110111
112
113
114
115116
117
118
119
120121
122
123
124
125126
127
128
129
130131
132
133
134
135136
137
138
139
140141
142
143
144
145146
147
148
## Adapted class
<?php
/**
 * Zend_Queue offline processing hack example
 * * @author      Dave Marshall 
 * @version     $Rev: $
 * @since       $Date: $
 * @link        $URL: $
 */class EmailSender {
 
    private static $queue = null;
 
    /**     * Set Queue
     *
     * @param Zend_Queue $queue
     */
    public static function setQueue($queue)    {
        self::$queue = $queue;
    }
 
    /**     * Takes an array of addresses and sends an email to each one.
     *
     * @see reallySendEmail
     * @see sendQueuedEmails
     * @param array $address     */
    public static function sendEmail($address)
    {
        if (self::$queue === null) {
            // just send them now if we don't have a queue instance            return self::reallySendEmail($address);
        }
 
        self::$queue->send(serialize(func_get_args()));
    } 
    /**
     * Takes an array of addresses and sends an email to each one.
     *
     * @see sendEmail     * @param array $address
     */
    private static function reallySendEmail($address)
    {
        /**         * Code in here
         */
        echo 'Sending email to ' . implode(', ', $address) . PHP_EOL;
    }
     /**
     * Reads emails from the queue and sends them
     *
     * @param int $count - The number of queued items to process
     */    public static function sendQueuedEmails($count)
    {
        /**
         * Should really check the queue is good here
         */ 
        $messages = self::$queue->receive(intval($count));
        foreach($messages as $msg) {
            $args = unserialize($msg->body);
            call_user_func_array(array(__CLASS__, 'reallySendEmail'), $args);            self::$queue->deleteMessage($msg);
        }
    }
}
 ## Usage in application code
<?php
 
set_include_path(
    'path/to/zend/framework' . PATH_SEPARATOR    . 'path/to/zend/framework/incubator' . PATH_SEPARATOR
    . get_include_path()
);
 
require_once "Zend/Loader.php";Zend_Loader::registerAutoload();
 
define('DB_SERVER', 'localhost');
define('DB_PORT', 3306);
define('DB_USER', 'root');define('DB_PASS', 'password');
define('DB_NAME', 'emailsender');
 
$config = array(
    'name' => 'transmittal',    'driverOptions' => array(
        'host'     => DB_SERVER,
        'port'     => DB_PORT,
        'username' => DB_USER,
        'password' => DB_PASS,        'dbname'   => DB_NAME,
        'type'     => 'pdo_mysql'
    )
);
 // Create a database queue
$queue = new Zend_Queue('Db', $config);
$queue->createQueue('myqueue'); // called for good measure
 
EmailSender::setQueue($queue);EmailSender::sendEmail(array('yourname@gmail.com'));
 
## Usage for crontab script
<?php
 set_include_path(
    'path/to/zend/framework' . PATH_SEPARATOR
    . 'path/to/zend/framework/incubator' . PATH_SEPARATOR
    . get_include_path()
); 
require_once "Zend/Loader.php";
Zend_Loader::registerAutoload();
 
define('DB_SERVER', 'localhost');define('DB_PORT', 3306);
define('DB_USER', 'root');
define('DB_PASS', 'password');
define('DB_NAME', 'emailsender');
 $config = array(
    'name' => 'transmittal',
    'driverOptions' => array(
        'host'     => DB_SERVER,
        'port'     => DB_PORT,        'username' => DB_USER,
        'password' => DB_PASS,
        'dbname'   => DB_NAME,
        'type'     => 'pdo_mysql'
    ));
 
// Create a database queue
$queue = new Zend_Queue('Db', $config);
$queue->createQueue('myqueue'); // called for good measure 
EmailSender::setQueue($queue);
EmailSender::sendQueuedEmails(5);

Comments

davedevelopment davedevelopment
1 year ago

Got quite a lot of views on this, but didn't get any feedback, would be appreciated.

You must login before commenting on a snippet. If you do not have an account, please register.

Snippet description

This is a little hacky, but a nice way to demonstrate Zend_Queue.

An application we are developing sometimes had to send a lot of emails on request from the user, sending them was taking too long 'online', so we refactored the class to add the calls to a queue, allowing a scheduled task to send the emails 'offline'.

This was tested against the latest trunk of Zend Framework and the incubator.

Snippet details

Created:
davedevelopment davedevelopment
1 year ago
Edited:
davedevelopment davedevelopment
1 year ago
Revision Id:
113
Edit Message:
Added Zend Framework Version the code was tested with
ZF Version
1.7.8
Tags:
Zend_Queue queue user experience
Comments:
1
Views:
812
Points:
0 (0 votes)

History

r113

Added Zend Framework Version the code was tested with

davedevelopment davedevelopment
1 year ago
diff
r108

Clarified fall back to just sending

davedevelopment davedevelopment
1 year ago
diff
r107

Initial Release

davedevelopment davedevelopment
1 year ago