Пример #1
0
            $this->queue = new AMQPQueue($this->channel);
            $this->queue->setFlags(AMQP_EXCLUSIVE);
            $this->queue->declareQueue();
            $this->callbackQueueName = $this->queue->getName();
            //Set Publish Attributes
            $attributes = array('correlation_id' => $this->corrId, 'reply_to' => $this->callbackQueueName);
            $this->exchange->publish($value, $this->rpcQueue, AMQP_NOPARAM, $attributes);
            $callback = function (AMQPEnvelope $message, AMQPQueue $q) {
                if ($message->getCorrelationId() == $this->corrId) {
                    //echo sprintf("CorrelationID: %s",$message->getCorrelationId()), PHP_EOL;
                    //echo sprintf("Response: %s",$message->getBody()), PHP_EOL;
                    $this->response = $message->getBody();
                    $q->nack($message->getDeliveryTag());
                    return false;
                }
            };
            $this->queue->consume($callback);
            //Return RPC Results
            return $this->response;
        } catch (AMQPQueueException $ex) {
            print_r($ex);
        } catch (Exception $ex) {
            print_r($ex);
        }
    }
}
$value = isset($argv[1]) ? $argv[1] : 5;
$fibonacciRpc = new FibonacciRpcClient();
echo sprintf(" [x] Requesting fib(%s)", $value), PHP_EOL;
$response = $fibonacciRpc->call($value);
echo sprintf(" [.] Received: %s", $response), PHP_EOL;
Пример #2
0
    private $callback_queue;
    private $response;
    private $corr_id;
    public function __construct()
    {
        $this->connection = new AMQPConnection('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->properties['correlation_id'] == $this->corr_id) {
            $this->response = $rep->body;
        }
    }
    public function call($n)
    {
        $this->response = null;
        $this->corr_id = uniqid();
        $msg = new AMQPMessage((string) $n, array('correlation_id' => $this->corr_id, 'reply_to' => $this->callback_queue));
        $this->channel->basic_publish($msg, '', 'rpc_queue');
        while (!$this->response) {
            $this->channel->wait();
        }
        return intval($this->response);
    }
}
$fibonacci_rpc = new FibonacciRpcClient();
$response = $fibonacci_rpc->call(30);
echo " [.] Got ", $response, "\n";
Пример #3
0
        $this->channel->basic_consume($this->callback_queue, '', false, false, false, false, [$this, 'on_response']);
    }
    /**
     * @param AMQPMessage $rep
     */
    public function on_response(AMQPMessage $rep)
    {
        if ($rep->get('correlation_id') == $this->corr_id) {
            $this->response = $rep->body;
        }
    }
    /**
     * @param $n
     * @return int
     */
    public function call($n)
    {
        $this->response = null;
        $this->corr_id = uniqid();
        $msg = new AMQPMessage((string) $n, ['correlation_id' => $this->corr_id, 'reply_to' => $this->callback_queue]);
        $this->channel->basic_publish($msg, '', 'rpc_queue');
        while (!$this->response) {
            $this->channel->wait();
        }
        return intval($this->response);
    }
}
$fibonacci_rpc = new FibonacciRpcClient();
$number = isset($argv[1]) ? $argv[1] : 30;
$response = $fibonacci_rpc->call($number);
echo " [.] Got ", $response, "\n";