-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrabbitmq.php
More file actions
54 lines (41 loc) · 1.28 KB
/
rabbitmq.php
File metadata and controls
54 lines (41 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
require_once "vendor/autoload.php";
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;
echo " >>> RUNNING ... \n";
$targetQueue = $argv[1] ?? "";
$type = $argv[2] ?? "";
$message = $argv[3] ?? "";
if (empty($targetQueue)) {
echo " [x] Incorrect arg1: Define a target queue.\n";
exit;
}
$connection = new AMQPStreamConnection('127.0.0.1', 5672, 'guest', 'guest');
$channel = $connection->channel();
$channel->queue_declare($targetQueue, false, true, false, false);
switch ($type) {
default:
echo " [x] Incorrect arg2: Use 'producer' or 'consumer'. \n";
break;
case "producer":
if (empty($message)) {
echo " [x] Incorrect arg3: Define a message. \n";
exit;
}
$message = new AMQPMessage($message);
$channel->basic_publish($message, "", $targetQueue);
echo " [v] Sent message! \n";
break;
case "consumer":
$callback = function ($msg) {
echo ' --- MSG: ', $msg->body, "\n";
};
$channel->basic_consume($targetQueue, '', false, true, false, false, $callback);
while ($channel->is_open()) {
$channel->wait();
}
break;
}
$channel->close();
$connection->close();
echo "... END <<< \n";