10000 Merge remote branch 'alvaro/master' into php · bsumgit/rabbitmq-tutorials@4e0736d · GitHub
[go: up one dir, main page]

Skip to content

Commit 4e0736d

Browse files
committed
Merge remote branch 'alvaro/master' into php
2 parents 5fb68cf + 4af188c commit 4e0736d

File tree

9 files changed

+184
-0
lines changed

9 files changed

+184
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
php/lib/php-amqplib

php/README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# RabbitMQ - PHP Examples #
2+
3+
## Requirements ##
4+
5+
To run the examples you need:
6+
7+
- A running RabbitMQ server
8+
- PHP 5.3
9+
- php-amqplib
10+
11+
## Setup ##
12+
13+
To get the php-amqplib library execute the following command inside the present folder:
14+
15+
$ git clone http://github.com/tnc/php-amqplib.git lib/php-amqplib
16+
17+
## Running the Examples ##
18+
19+
To run the examples do in one Terminal:
20+
21+
$ php send.php some message to send
22+
23+
Then in another Terminal try:
24+
25+
$ php receive.php

php/config/config.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
// define('AMQP_DEBUG', true); //enable if you want to see logs.
3+
4+
define('HOST', 'localhost');
5+
define('PORT', 5672);
6+
define('USER', 'guest');
7+
define('PASS', 'guest');
8+
define('VHOST', '/');

php/emit_log.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
require_once(__DIR__ . '/lib/php-amqplib/amqp.inc');
4+
include_once(__DIR__ . '/config/config.php');
5+
6+
$connection = new AMQPConnection(HOST, PORT, USER, PASS, VHOST);
7+
$channel = $connection->channel();
8+
$channel->exchange_declare('logs', 'fanout');
9+
10+
$data = implode(' ', array_slice($argv, 1));
11+
if(empty($data)) $data = "info: Hello World!";
12+
$msg = new AMQPMessage($data);
13+
14+
$channel->basic_publish($msg, 'logs');
15+
16+
echo " [x] Sent ", $data, "\n";
17+
18+
$channel->close();
19+
$connection->close();
20+
21+
?>

php/new_task.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
require_once(__DIR__ . '/lib/php-amqplib/amqp.inc');
4+
include_once(__DIR__ . '/config/config.php');
5+
6+
$connection = new AMQPConnection(HOST, PORT, USER, PASS, VHOST);
7+
$channel = $connection->channel();
8+
9+
$channel->queue_declare('task_queue', false, true);
10+
11+
$data = implode(' ', array_slice($argv, 1));
12+
if(empty($data)) $data = "Hello World!";
13+
$msg = new AMQPMessage($data,
14+
array('delivery_mode' => 2) # make message persistent
15+
);
16+
17+
$channel->basic_publish($msg, '', 'task_queue');
18+
19+
echo " [x] Sent ", $data, "\n";
20+
21+
$channel->close();
22+
$connection->close();
23+
24+
?>

php/receive.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
require_once(__DIR__ . '/lib/php-amqplib/amqp.inc');
4+
include_once(__DIR__ . '/config/config.php');
5+
6+
$connection = new AMQPConnection(HOST, PORT, USER, PASS, VHOST);
7+
$channel = $connection->channel();
8+
9+
$channel->queue_declare('test');
10+
11+
echo ' [*] Waiting for messages. To exit press CTRL+C', "\n";
12+
13+
$callback = function($msg){
14+
echo " [x] Received ", $msg->body, "\n";
15+
};
16+
17+
$channel->basic_consume('test', 'consumer_tag', false, true, false, false, $callback);
18+
19+
while(count($channel->callbacks)) {
20+
$channel->wait();
21+
}
22+
23+
$channel->close();
24+
$connection->close();
25+
26+
?>

php/receive_logs.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
require_once(__DIR__ . '/lib/php-amqplib/amqp.inc');
4+
include_once(__DIR__ . '/config/config.php');
5+
6+
$connection = new AMQPConnection(HOST, PORT, USER, PASS, VHOST);
7+
$channel = $connection->channel();
8+
$channel->exchange_declare('logs', 'fanout');
9+
10+
list($queue_name, ,) = $channel->queue_declare();
11+
12+
$channel->queue_bind($queue_name, 'logs');
13+
14+
echo ' [*] Waiting for logs. To exit press CTRL+C', "\n";
15+
16+
$callback = function($msg){
17+
echo $msg->body, "\n";
18+
};
19+
20+
$channel->basic_consume($queue_name, 'consumer_tag', false, true, false, false, $callback);
21+
22+
while(count($channel->callbacks)) {
23+
$channel->wait();
24+
}
25+
26+
$channel->close();
27+
$connection->close();
28+
29+
?>

php/send.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
require_once(__DIR__ . '/lib/php-amqplib/amqp.inc');
4+
include_once(__DIR__ . '/config/config.php');
5+
6+
$connection = new AMQPConnection(HOST, PORT, USER, PASS, VHOST);
7+
$channel = $connection->channel();
8+
9+
$channel->queue_declare('test');
10+
11+
$msg = new AMQPMessage('Hello World!');
12+
13+
$channel->basic_publish($msg, '', 'test');
14+
15+
echo " [x] Sent 'Hello World!'\n";
16+
17+
$channel->close();
18+
$connection->close();
19+
20+
?>

php/worker.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
require_once(__DIR__ . '/lib/php-amqplib/amqp.inc');
4+
include_once(__DIR__ . '/config/config.php');
5+
6+
$connection = new AMQPConnection(HOST, PORT, USER, PASS, VHOST);
7+
$channel = $connection->channel();
8+
9+
$channel->queue_declare('task_queue', false, true);
10+
11+
echo ' [*] Waiting for messages. To exit press CTRL+C', "\n";
12+
13+
$callback = function($msg){
14+
echo " [x] Received ", $msg->body, "\n";
15+
sleep(substr_count($msg->body, '.'));
16+
echo " [x] Done", "\n";
17+
$msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
18+
};
19+
20+
$channel->basic_qos(null, 1, null);
21+
$channel->basic_consume('task_queue', 'consumer_tag', false, false, false, false, $callback);
22+
23+
while(count($channel->callbacks)) {
24+
$channel->wait();
25+
}
26+
< 49F9 /div>
27+
$channel->close();
28+
$connection->close();
29+
30+
?>

0 commit comments

Comments
 (0)
0