本文整理汇总了PHP中AMQPQueue::consume方法的典型用法代码示例。如果您正苦于以下问题:PHP AMQPQueue::consume方法的具体用法?PHP AMQPQueue::consume怎么用?PHP AMQPQueue::consume使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类AMQPQueue
的用法示例。
在下文中一共展示了AMQPQueue::consume方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: Run
public function Run()
{
// Declare a new exchange
$ex = new AMQPExchange($this->cnn);
$ex->declare('game', AMQP_EX_TYPE_TOPIC);
// Create a new queue
$q1 = new AMQPQueue($this->cnn);
$q1->declare('queue1');
$q1->purge('queue1');
$q2 = new AMQPQueue($this->cnn);
$q2->declare('queue2');
$q1->purge('queue2');
$q3 = new AMQPQueue($this->cnn);
$q3->declare('queue3');
$q3->purge('queue3');
$options = array('min' => 0, 'max' => 10, 'ack' => true);
// Bind it on the exchange to routing.key
$ex->bind('queue1', 'game1.#');
$ex->bind('queue2', 'game1.#');
$ex->bind('queue3', 'game1.#');
$ex->bind('queue3', 'queue3.#');
$msgbody1 = 'hello';
// Publish a message to the exchange with a routing key
$result = $ex->publish($msgbody1, 'game1.msg');
$this->AssertEquals($result, TRUE, 'publish message failed');
$msgbody2 = 'world';
// Publish a message to the exchange with a routing key
$result = $ex->publish($msgbody2, 'game1.msg');
$this->AssertEquals($result, TRUE, 'publish message failed');
$msgbody3 = 'hello player3';
// Publish a message to the exchange with a routing key
$result = $ex->publish($msgbody3, 'queue3.command');
$this->AssertEquals($result, TRUE, 'publish message failed');
// Read from the queue
$msg = $q1->consume($options);
$this->AddMessage(var_export($msg, true));
$this->AssertEquals(count($msg), 2);
$this->AssertEquals($msg[0]['message_body'], $msgbody1, 'message not equal');
$this->AssertEquals($msg[1]['message_body'], $msgbody2, 'message not equal');
// Read from the queue
$msg = $q2->consume($options);
$this->AssertEquals(count($msg), 2);
$this->AssertEquals($msg[0]['message_body'], $msgbody1, 'message not equal');
$this->AssertEquals($msg[1]['message_body'], $msgbody2, 'message not equal');
// Read from the queue
$msg = $q3->consume($options);
$this->AddMessage(var_export($msg, true));
$this->AssertEquals(count($msg), 3);
$this->AssertEquals($msg[0]['message_body'], $msgbody1, 'message not equal');
$this->AssertEquals($msg[1]['message_body'], $msgbody2, 'message not equal');
$this->AssertEquals($msg[2]['message_body'], $msgbody3, 'message not equal');
$msg = $q3->consume($options);
$this->AssertEquals(count($msg), 0);
}
示例2: Run
public function Run()
{
// Declare a new exchange
$ex = new AMQPExchange($this->cnn);
$ex->declare('game', AMQP_EX_TYPE_FANOUT);
// Create a new queue
$q1 = new AMQPQueue($this->cnn);
$q1->declare('queue1');
$q2 = new AMQPQueue($this->cnn);
$q2->declare('queue2');
// Bind it on the exchange to routing.key
//$ex->bind('queue1', 'broadcast=true,target=queue1,x-match=any');
$ex->bind('queue1', '');
$ex->bind('queue2', '');
$msgBody = 'hello';
// Publish a message to the exchange with a routing key
$ex->publish($msgBody, 'foo');
// Read from the queue
$msg = $q1->consume();
$this->AssertEquals(count($msg), 1);
$this->AssertEquals($msg[0]['message_body'], $msgBody, 'message not equal');
// Read from the queue
$msg = $q2->consume();
$this->AssertEquals(count($msg), 1);
$this->AssertEquals($msg[0]['message_body'], $msgBody, 'message not equal');
$this->AddMessage(var_export($msg[0], true));
}
示例3: consume
/**
* @inheritdoc
*/
public function consume(callable $callback, $flags = null, $consumerTag = null)
{
$wrapper = function (\AMQPEnvelope $envelope) use($callback) {
return $callback(new Envelope($envelope));
};
$this->delegate->consume($wrapper, self::convertToDelegateFlags($flags), $consumerTag);
}
示例4: receive
/**
* {@inheritDoc}
*/
public function receive(array $eventNames, \Closure $callback)
{
$exchangeName = $this->exchange->getName();
foreach ($eventNames as $eventName) {
$this->queue->bind($exchangeName, $eventName);
}
$this->queue->consume(function (\AMQPEnvelope $envelope, \AMQPQueue $queue) use($callback) {
$message = $envelope->getBody();
$messageTag = $envelope->getDeliveryTag();
$status = call_user_func($callback, $message);
if ($status) {
$queue->ack($messageTag);
} else {
$queue->nack($messageTag);
}
});
}
示例5: get
public function get($queueName, $timeout = null)
{
if (null === $this->queue) {
$queue = new \AMQPQueue($this->channel);
$queue->setName($queueName);
$queue->declare();
}
return unserialize($queue->consume());
}
示例6: consume
/**
* @param callable|null $callback
* @param int $flags
* @param string $consumerTag
*/
public function consume(callable $callback = null, $flags = Client::NOPARAM, $consumerTag = null)
{
try {
$this->rawQueue->consume(function (\AMQPEnvelope $envelope) use($callback) {
return $callback(Envelope::createFromRaw($envelope, $this->decodeStrategy), $this);
}, $flags, $consumerTag);
} catch (\Exception $e) {
ClientHelper::throwRightException($e);
}
}
示例7: subscribe
public function subscribe($topic, $queueName, $process_callback)
{
$channel = new AMQPChannel($this->amqpConnection);
$channel->setPrefetchCount(0);
$queue = new AMQPQueue($channel);
$queue->setName($queueName);
$queue->setFlags(AMQP_DURABLE);
$queue->declareQueue();
$queue->bind('topic', $topic);
$queue->consume($process_callback);
if (!$this->amqpConnection->disconnect()) {
throw new Exception("Could not disconnect !");
}
}
示例8: Run
public function Run()
{
// Declare a new exchange
$ex = new AMQPExchange($this->cnn);
$ex->declare('game', AMQP_EX_TYPE_HEADER);
// Create a new queue
$q1 = new AMQPQueue($this->cnn);
$q1->declare('queue1');
$q1->purge('queue1');
$q2 = new AMQPQueue($this->cnn);
$q2->declare('queue2');
$q1->purge('queue2');
$q3 = new AMQPQueue($this->cnn);
$q3->declare('queue3');
$q3->purge('queue3');
$options = array('min' => 0, 'max' => 10, 'ack' => true);
// Bind it on the exchange to routing.key
$ex->bind('queue1', 'broadcast=1,target=1,x-match=any');
$ex->bind('queue2', 'broadcast=1,target=2,x-match=any');
$ex->bind('queue3', 'broadcast=1,target=3,x-match=any');
$msgbody1 = 'hello';
// Publish a message to the exchange with a routing key
$result = $ex->publish($msgbody1, NULL, AMQP_IMMEDIATE, array('headers' => array('broadcast' => 1)));
$this->AssertEquals($result, TRUE, 'publish message failed');
$msgbody2 = 'world';
// Publish a message to the exchange with a routing key
$result = $ex->publish($msgbody2, NULL, AMQP_IMMEDIATE, array('headers' => array('broadcast' => 1)));
$this->AssertEquals($result, TRUE, 'publish message failed');
$msgbody3 = 'queue3';
// Publish a message to the exchange with a routing key
$result = $ex->publish($msgbody1, NULL, AMQP_IMMEDIATE, array('headers' => array('target' => 3)));
$this->AssertEquals($result, TRUE, 'publish message failed');
// Read from the queue
$msg = $q1->consume($options);
$this->AddMessage(var_export($msg, true));
$this->AssertEquals(count($msg), 2);
//$this->AssertEquals($msg[0]['message_body'], $msgbody1, 'message not equal');
//$this->AssertEquals($msg[1]['message_body'], $msgbody2, 'message not equal');
// Read from the queue
$msg = $q2->consume($options);
$this->AssertEquals(count($msg), 2);
//$this->AssertEquals($msg[0]['message_body'], $msgbody1, 'message not equal');
//$this->AssertEquals($msg[1]['message_body'], $msgbody2, 'message not equal');
// Read from the queue
$msg = $q3->consume($options);
$this->AssertEquals(count($msg), 3);
//$this->AssertEquals($msg[0]['message_body'], $msgbody1, 'message not equal');
//$this->AssertEquals($msg[1]['message_body'], $msgbody2, 'message not equal');
//$this->AssertEquals($msg[2]['message_body'], $msgbody3, 'message not equal');
}
示例9: getMessage
/**
* Get message from queue
*
* @return \Apple\ApnPush\Notification\MessageInterface|null
*
* @throws \RuntimeException
*/
public function getMessage()
{
if (null === $this->queue) {
throw new \RuntimeException('Can\'n get message. Not found queue.');
}
$message = null;
$this->queue->consume(function (\AMQPEnvelope $amqpMessage, \AMQPQueue $queue) use(&$message) {
$message = unserialize($amqpMessage->getBody());
$queue->ack($amqpMessage->getDeliveryTag());
// End consume process
return false;
}, AMQP_NOPARAM);
return $message;
}
示例10: consume
public function consume($queueName, $callBackFunction)
{
try {
$queue = new AMQPQueue($this->channel);
$queue->setName($queueName);
$queue->setFlags(AMQP_DURABLE);
$queue->declareQueue();
echo ' [*] Waiting for messages on ' . $queueName . '. To exit press CTRL+C ', PHP_EOL;
$queue->consume($callBackFunction);
} catch (AMQPQueueException $ex) {
print_r($ex);
} catch (Exception $ex) {
print_r($ex);
}
}
示例11: basic_consume
/**
* @param string $queue
* @param string $consumer_tag
* @param bool $no_local
* @param bool $no_ack
* @param bool $exclusive
* @param bool $nowait
* @param callable $callback
* @param string $ticket
* @param array $arguments
* @return mixed
*/
public function basic_consume($queue = '', $consumer_tag = '', $no_local = false, $no_ack = false, $exclusive = false, $nowait = false, callable $callback = null, $ticket = null, $arguments = array())
{
$q = new \AMQPQueue($this->channel);
$q->setName($queue);
$q->declareQueue();
$flags = AMQP_NOPARAM;
$flags += $no_local ? AMQP_NOLOCAL : 0;
$flags += $no_ack ? AMQP_AUTOACK : 0;
$flags += $exclusive ? AMQP_EXCLUSIVE : 0;
$flags += $nowait ? AMQP_NOWAIT : 0;
$q->consume(function ($message, \AMQPQueue $q) use($callback, $flags, $consumer_tag) {
$deliveryInfo = array('channel' => $q, 'delivery_tag' => '');
return $callback(new AMQPMessage($message, array(), $deliveryInfo));
}, $flags, $consumer_tag);
}
示例12: Run
public function Run()
{
// Create a new queue
$q1 = new AMQPQueue($this->cnn);
$q1->declare('queue1');
$q1->purge('queue1');
$options = array('min' => 0, 'max' => 10, 'ack' => true);
// Bind it on the exchange to routing.key
$q1->bind(\Constant\AMQPChannelConstant::AMF, \Constant\AMQPCommand::BingoBroadcast);
// Read from the queue
while (TRUE) {
$msg = $q1->consume($options);
var_dump($msg);
}
}
示例13: syncCall
/**
* Синхронный вызов
* @param $service
* @param $message
* @return \AMQPEnvelope
* @throws \Exception
*/
public function syncCall($service, $message)
{
if (is_array($message) || is_object($message)) {
$message = Json::encode($message);
}
// id этого запроса
$correlationId = self::$syncCallCorrelationId++;
$chanel = $this->amqp->newChannel();
// Принимать неограниченное количество сообщений
$chanel->qos(0, 0);
//<editor-fold desc="Создаем временную очередь ответов">
$queue = new \AMQPQueue($chanel);
$queue->setName($this->amqp->getSyncCallTransactionId());
// Очередь удалиться когда опустеет
$queue->setFlags(AMQP_AUTODELETE | AMQP_EXCLUSIVE);
$queue->declareQueue();
//</editor-fold>
//<editor-fold desc="Отправляем запрос на обработку">
// Точка доступа
$exchange = new \AMQPExchange($chanel);
$attributes = array('reply_to' => $this->amqp->getSyncCallTransactionId(), 'correlation_id' => $correlationId);
if (!$exchange->publish($message, $service, AMQP_NOPARAM, $attributes)) {
throw new \Exception('Exchange publish error');
}
//</editor-fold>
//<editor-fold desc="Ожидаем ответ">
/**
* @param \AMQPEnvelope $envelope
* @param \AMQPQueue $queue
* @return bool
*/
$processMessage = function ($envelope, $queue) use($correlationId, &$r) {
if ($envelope->getCorrelationId() == $correlationId) {
$r = $envelope;
return false;
}
return true;
};
$queue->consume($processMessage);
//</editor-fold>
return $r;
}
示例14: main
public static function main()
{
self::$pool = new Pool(MAX_CONCURRENCY_JOB, \CALLWorker::class, []);
$conn_args = (require 'mq.config.php');
$e_name = 'e_linvo';
//交换机名
$q_name = 'q_linvo';
//队列名
$k_route = 'key_1';
//路由key
//创建连接和channel
$conn = new AMQPConnection($conn_args);
if (!$conn->connect()) {
write_log('mq.hx9999.com 无法连接上');
return;
}
$channel = new AMQPChannel($conn);
//创建交换机
$ex = new AMQPExchange($channel);
$ex->setName($e_name);
$ex->setType(AMQP_EX_TYPE_DIRECT);
//direct类型
$ex->setFlags(AMQP_DURABLE);
//持久化
//创建队列
$q = new AMQPQueue($channel);
$q->setName($q_name);
$q->setFlags(AMQP_DURABLE);
//持久化
while (True) {
$q->consume(function ($envelope, $queue) {
$msg = $envelope->getBody();
$queue->ack($envelope->getDeliveryTag());
//手动发送ACK应答
self::$pool->submit(new Sendsms($msg));
});
}
self::$pool->shutdown();
$conn->disconnect();
//睡眠
sleep(5);
}
示例15: consume
/**
* @inheritdoc
*/
public function consume(callable $callback = null, int $flags = Constants::AMQP_NOPARAM, string $consumerTag = '')
{
if (null !== $callback) {
$innerCallback = function (\AMQPEnvelope $envelope, \AMQPQueue $queue) use($callback) {
$envelope = new Envelope($envelope);
return $callback($envelope, $this);
};
} else {
$innerCallback = null;
}
try {
$this->queue->consume($innerCallback, $flags, $consumerTag);
} catch (\AMQPConnectionException $e) {
throw QueueException::fromAmqpExtension($e);
} catch (\AMQPChannelException $e) {
throw QueueException::fromAmqpExtension($e);
} catch (\AMQPQueueException $e) {
throw QueueException::fromAmqpExtension($e);
}
}