added example 2 worker/new_task
This commit is contained in:
parent
d11638a2da
commit
46d66dec01
45
php-amqp/new_task.php
Normal file
45
php-amqp/new_task.php
Normal file
@ -0,0 +1,45 @@
|
||||
#!/usr/bin/env php
|
||||
<?php
|
||||
/**
|
||||
@author Chimdi Azubuike <me@chimdi.com>
|
||||
*/
|
||||
|
||||
//Establish connection to AMQP
|
||||
$connection = new AMQPConnection();
|
||||
$connection->setHost('127.0.0.1');
|
||||
$connection->setLogin('guest');
|
||||
$connection->setPassword('guest');
|
||||
$connection->connect();
|
||||
|
||||
|
||||
|
||||
//Create and declare channel
|
||||
$channel = new AMQPChannel($connection);
|
||||
$channel->setPrefetchCount(1);
|
||||
|
||||
|
||||
$routing_key = 'task_queue';
|
||||
|
||||
try{
|
||||
$queue = new AMQPQueue($channel);
|
||||
$queue->setName($routing_key);
|
||||
$queue->setFlags(AMQP_DURABLE);
|
||||
$queue->declareQueue();
|
||||
|
||||
}catch(Exception $ex){
|
||||
print_r($ex);
|
||||
}
|
||||
|
||||
|
||||
//Read from stdin
|
||||
$data = implode(' ', array_slice($argv,1));
|
||||
if(empty($data))
|
||||
$data = "Hello World!";
|
||||
|
||||
$exchange = new AMQPExchange($channel);
|
||||
$exchange->publish($data, $routing_key);
|
||||
|
||||
echo " [x] Sent {$data}", PHP_EOL;
|
||||
|
||||
$connection->disconnect();
|
||||
|
41
php-amqp/worker.php
Normal file
41
php-amqp/worker.php
Normal file
@ -0,0 +1,41 @@
|
||||
#!/usr/bin/env php
|
||||
<?php
|
||||
/**
|
||||
@author Chimdi Azubuike <me@chimdi.com>
|
||||
*/
|
||||
|
||||
//Establish connection AMQP
|
||||
$connection = new AMQPConnection();
|
||||
$connection->setHost('127.0.0.1');
|
||||
$connection->setLogin('guest');
|
||||
$connection->setPassword('guest');
|
||||
$connection->connect();
|
||||
|
||||
//Create and declare channel
|
||||
$channel = new AMQPChannel($connection);
|
||||
|
||||
$routing_key = 'task_queue';
|
||||
|
||||
$callback_func = function(AMQPEnvelope $message, AMQPQueue $q) use (&$max_jobs) {
|
||||
echo " [x] Received: ", $message->getBody(), PHP_EOL;
|
||||
sleep(1);
|
||||
echo " [X] Done", PHP_EOL;
|
||||
$q->ack($message->getDeliveryTag());
|
||||
};
|
||||
|
||||
try{
|
||||
$queue = new AMQPQueue($channel);
|
||||
$queue->setName($routing_key);
|
||||
$queue->setFlags(AMQP_DURABLE);
|
||||
$queue->declareQueue();
|
||||
|
||||
|
||||
echo ' [*] Waiting for logs. To exit press CTRL+C', PHP_EOL;
|
||||
$queue->consume($callback_func);
|
||||
}catch(AMQPQueueException $ex){
|
||||
print_r($ex);
|
||||
}catch(Exception $ex){
|
||||
print_r($ex);
|
||||
}
|
||||
|
||||
$connection->disconnect();
|
Loading…
Reference in New Issue
Block a user