This repository has been archived by the owner on May 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrabbitMQLib.php
162 lines (134 loc) · 4.32 KB
/
rabbitMQLib.php
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
require_once __DIR__ . '/vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;
class rabbitMQConsumer
{
public $host;
private $port;
private $username;
private $password;
private $vhost;
private $exchange;
private $queue;
private $routing_key = '*';
private $exchange_type = "direct";
//Initialize the consumer
public function __construct($exchange, $queue)
{
require_once __DIR__ . "/config.php"; //pull in our credentials
$this->host = $config['host'];
$this->port = $config['port'];
$this->username = $config['username'];
$this->password = $config['password'];
$this->vhost = $config['vhost'];
if (isset($config["exchange_type"])) {
$this->exchange_type = $config["exchange_type"];
}
$this->exchange = $exchange;
$this->queue = $queue;
}
public function consume_request($req)
{
$body = $req->body;
$payload = json_decode($body, true);
$response;
if (isset($this->callback)) {
$response = new AMQPMessage(
json_encode( call_user_func($this->callback, $payload) ), //Call the function callback, with parameter(s) in $payload
array('correlation_id' => $req->get('correlation_id')) //"return to sender using correlation_id"
);
}
$req->delivery_info['channel']->basic_publish(
$response,
'',
$req->get('reply_to')
);
$req->ack();
}
public function process_requests($callback)
{
$this->callback = $callback;
$this->connection = new AMQPStreamConnection($this->host, $this->port, $this->username, $this->password, $this->vhost);
$this->channel = $this->connection->channel();
$this->channel->queue_declare($this->queue, false, false, false, false);
$this->channel->basic_qos(null, 1, null);
$this->channel->basic_consume($this->queue, '', false, false, false, false, array($this, 'consume_request')); //This means that messages will be sent to function consume_request
while ($this->channel->is_open()) {
$this->channel->wait();
}
$this->channel->close();
$this->connection->close();
}
}
//test code:
//$testConsumer = new rabbitMQConsumer("amq.direct", "rpc_queue");
//$testConsumer->process_requests('fib');
class rabbitMQProducer
{
public $host;
private $port;
private $username;
private $password;
private $vhost;
private $exchange;
private $queue;
private $routing_key = '*';
private $exchange_type = "direct";
private $connection;
private $channel;
private $callback_queue;
private $response;
private $corr_id;
public function __construct($exchange, $queue)
{
require_once __DIR__ . "/config.php"; //pull in our credentials
$this->host = $config['host'];
$this->port = $config['port'];
$this->username = $config['username'];
$this->password = $config['password'];
$this->vhost = $config['vhost'];
if (isset($config["exchange_type"])) {
$this->exchange_type = $config["exchange_type"];
}
$this->exchange = $exchange;
$this->queue = $queue;
$this->connection = new AMQPStreamConnection($this->host, $this->port, $this->username, $this->password, $this->vhost);
$this->channel = $this->connection->channel();
list($this->callback_queue, ,) = $this->channel->queue_declare("",false,false,true,false);
$this->channel->basic_consume(
$this->callback_queue,
'',false,true,false,false,array(
$this,
'onResponse'
)
);
}
public function onResponse($rep)
{
if ($rep->get('correlation_id') == $this->corr_id) {
$this->response = $rep->body;
}
}
public function send_request($n)
{
$this->response = null;
$this->corr_id = uniqid();
$msg = new AMQPMessage(
(string) json_encode($n),
array(
'correlation_id' => $this->corr_id,
'reply_to' => $this->callback_queue
)
);
$this->channel->basic_publish($msg, '', $this->queue);
$timeout = 10;
while (!$this->response) {
$this->channel->wait(null, false, $timeout);
}
return json_decode($this->response, true);
}
}
//$client = new rabbitMQProducer('amq.direct', 'news');
//$response = $client->send_request('Aman');
?>