コード例 #1
0
ファイル: Channel.php プロジェクト: znk3r/mqlib
 public function consume(Queue $queue, $consumerOptions, $action)
 {
     $this->channel->basic_consume($queue->getName(), $consumerOptions['name'], $consumerOptions['noLocal'], $consumerOptions['noAck'], $consumerOptions['exclusive'], $consumerOptions['noWait'], function ($queueMessage) use($action) {
         $action(new Incoming($queueMessage));
     });
     // Listen to the socket/stream
     while (count($this->channel->callbacks)) {
         $this->channel->wait();
     }
 }
コード例 #2
0
ファイル: consumer.php プロジェクト: znk3r/mqlib
<?php

require_once __DIR__ . '/../../vendor/autoload.php';
use znk3r\MQlib\Consumer;
use znk3r\MQlib\Queue\Queue;
use znk3r\MQlib\Exchange\Topic;
$exchangeName = 'mqlib_topic_exchange';
$queueName = 'mqlib_topic_queue-' . rand();
$consumerName = 'consumer-' . getmypid();
// Declare topic exchange
// Must be the same exchange as the one used by the producer
$exchange = new Topic($exchangeName);
// Create queue and bind it to the exchange
$queue = new Queue($queueName);
$queue->bindTo($exchange);
// Add routing keys
$routingKeys = $argv;
array_shift($routingKeys);
$queue->setRoutingKeys($routingKeys);
// Setup this consumer
$consumer = new Consumer($consumerName);
echo "{$consumerName} connected to {$queueName} and ready!" . PHP_EOL;
if (!empty($routingKeys)) {
    echo 'Waiting for keys: [ ' . implode(', ', $routingKeys) . ' ]' . PHP_EOL;
}
// Bind this consumer to the queue and define "what" code must be run for each message
// received from the queue
$consumer->listen($queue, function ($msg) {
    /** @var znk3r\MQlib\Message\Incoming $msg */
    try {
        // $msg is a Message\Incoming object