| CARVIEW |
jwage / PHPMessageQueue
- Source
- Commits
- Network (1)
- Issues (0)
- Downloads (0)
- Wiki (1)
- Graphs
-
Branch:
master
-
Branches (1)
- master ✓
- Tags (0)
| name | age | message | |
|---|---|---|---|
| |
README.markdown | Tue Nov 17 19:00:49 -0800 2009 | - [Jonathan Wage] |
| |
example/ | Mon Nov 16 20:31:06 -0800 2009 | - [Jonathan Wage] |
| |
lib/ | Tue Nov 17 19:00:49 -0800 2009 | - [Jonathan Wage] |
| |
tests/ | Tue Nov 17 19:00:49 -0800 2009 | - [Jonathan Wage] |
PHP 5.3 Message Queue Library
Easily run daemons to watch a queue for new messages sent from your applications. This library requires Doctrine 2.0. We'll use the class loader from Doctrine. It is capable of loading the classes of both Doctrine and the MessageQueue libraries.
In a script named setup.php paste the following code:
<?php
$classLoader = new \Doctrine\Common\IsolatedClassLoader();
$classLoader->register();
In order to use the message queue library we first need to instantiate a new Queue instance.
Add the following code to setup.php and edit the $params for your database:
// ...
$params = array(
'driver' => 'pdo_mysql',
'user' => 'username',
'password' => 'password',
'dbname' => 'dbname',
);
$client = new \MessageQueue\Client\DoctrineDatabase($params);
$queue = new \MessageQueue\Queue('jwage', $client);
You can also use any of the Doctrine cache drivers as a means for managing the messages. For example you could use APC to store the messages
// ...
$cacheDriver = new \Doctrine\Common\Cache\ApcCache();
$cacheDriver->setManageCacheIds(true);
$client = new \MessageQueue\Client\DoctrineCacheDriver($cacheDriver);
$queue = new \MessageQueue\Queue('jwage', $client);
It is up to you what you use. Now, in another script named queue.php paste the following code:
<?php
require 'setup.php';
$queue->run();
The run method is the equivalent of doing this manually:
while (true) {
$queue->execute();
}
It constantly watches the queue for the new messages. You can alternatively setup the execute() method to run at a specified interval via a cron job instead of having a daemon running constantly.
Before we can start sending messages, we need to create a child class which represents a message. In another file named ExampleMessage.php paste the following code:
class ExampleMessage implements \MessageQueue\Message
{
public $someInfo;
public function execute()
{
print_r($this);
// do something
}
}
At the bottom of setup.php require the ExampleMessage.php class:
// ...
require 'ExampleMessage.php';
Now start the queue.php script from the command line:
$ php queue.php
Now in another script named message.php paste the following code which instantiates an instance of ExampleMessage and sends it to the queue:
<?php
require 'setup.php';
$message = ExampleMessage();
$message->someInfo = 'test';
$queue->send($message);
If you watch the output of the queue.php script we started earlier you should see something like the following:
$ php queue.php
ExampleMessage Object
(
[someInfo] => test
)
