<?php
require_once __DIR__ . '/vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;
class OrderReserveRpcClient {
private $connection;
private $channel;
private $callback_queue;
private $response;
private $corr_id;
public function __construct() {
$this->connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
$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, false, false, false,
array($this, 'on_response'));
}
public function on_response($rep) {
if($rep->get('correlation_id') == $this->corr_id) {
$this->response = $rep->body;
}
}
public function call($Order, $GooId, $Quantity) {
$this->response = null;
$this->corr_id = uniqid();
$n = tojson($Order, $GooId, $Quantity)
$msg = new AMQPMessage(
(string) $n,
array('correlation_id' => $this->corr_id,
'reply_to' => $this->callback_queue)
);
$this->channel->basic_publish($msg, '', 'rpc.warehouse.try.reserve,goods.for.order');
while(!$this->response) {
$this->channel->wait();
}
return intval($this->response);
};
$orderreserve_rpc = new OrderReserveRpcClient();
$response = $orderreserve_rpc->call(Order, GooId, Quantity);
echo " [.] Got ", $response, "\n";
?>