Thứ Ba, 2 tháng 7, 2013

Zend_Queue simple example for offline processing


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

Không có nhận xét nào:

Đăng nhận xét

Học lập trình web căn bản với PHP

Bài 1: Các kiến thức căn bản Part 1:  https://jimmyvan88.blogspot.com/2012/05/can-ban-lap-trinh-web-voi-php-bai-1-cac.html Part 2:  https://...