Esempio n. 1
0
 /**
  * Bind queue to exchange using dispatcher event names as routing keys
  *
  * @return void
  * @throws AMQPExchangeException
  */
 public function bind(\AMQPQueue $queue, \AMQPExchange $exchange)
 {
     $events = preg_grep($this->pattern, array_keys($this->getDispatcher()->getListeners()));
     foreach ($events as $eventName) {
         $queue->bind($exchange->getName(), $eventName);
     }
     $this->dispatcher->dispatch(static::BIND_EVENT, new SymfonyEvent($events, ["pattern" => $this->pattern, "exchange" => $exchange->getName(), "queue" => $queue->getName()]));
 }
$connection = new AMQPConnection();
$connection->setHost('127.0.0.1');
$connection->setLogin('guest');
$connection->setPassword('guest');
$connection->connect();
//Create and declare channel
$channel = new AMQPChannel($connection);
try {
    //Declare Exchange
    $exchange_name = 'logs';
    $exchange = new AMQPExchange($channel);
    $exchange->setType(AMQP_EX_TYPE_FANOUT);
    $exchange->setName($exchange_name);
    $exchange->declareExchange();
    //Do not declasre the queue name by setting AMQPQueue::setName()
    $queue = new AMQPQueue($channel);
    $queue->setFlags(AMQP_EXCLUSIVE);
    $queue->declareQueue();
    $queue->bind($exchange_name, $queue->getName());
    echo sprintf("Queue Name: %s", $queue->getName()), PHP_EOL;
} catch (Exception $ex) {
    print_r($ex);
}
//Read from stdin
$message = implode(' ', array_slice($argv, 1));
if (empty($message)) {
    $message = "info: Hello World!";
}
$exchange->publish($message, '');
echo " [X] Sent {$message}", PHP_EOL;
$connection->disconnect();
Esempio n. 3
0
 public static function castQueue(\AMQPQueue $c, array $a, Stub $stub, $isNested)
 {
     $prefix = Caster::PREFIX_VIRTUAL;
     $a += array($prefix . 'name' => $c->getName(), $prefix . 'flags' => self::extractFlags($c->getFlags()), $prefix . 'arguments' => $c->getArguments(), $prefix . 'connection' => $c->getConnection(), $prefix . 'channel' => $c->getChannel());
     return $a;
 }
Esempio n. 4
0
 /**
  * @inheritdoc
  */
 public function getName() : string
 {
     return $this->queue->getName();
 }
Esempio n. 5
0
 /**
  * @inheritdoc
  */
 public function getName()
 {
     return $this->delegate->getName();
 }
$callback_func = function (AMQPEnvelope $message, AMQPQueue $q) use(&$max_jobs) {
    echo " [x] Received: ", $message->getBody(), PHP_EOL;
    sleep(1);
    $q->nack($message->getDeliveryTag());
};
try {
    //Declare Exchange
    $exchange_name = 'logs';
    $exchange = new AMQPExchange($channel);
    $exchange->setType(AMQP_EX_TYPE_FANOUT);
    $exchange->setName($exchange_name);
    $exchange->declareExchange();
    $queue = new AMQPQueue($channel);
    $queue->setFlags(AMQP_EXCLUSIVE);
    //allow server to define name
    $queue->declareQueue();
    $queue->bind($exchange_name, $queue->getName());
    echo ' [*] Waiting for logs. To exit press CTRL+C', PHP_EOL;
    $queue->consume($callback_func);
} catch (AMQPQueueException $ex) {
    print_r($ex);
} catch (AMQPExchangeException $ex) {
    print_r($ex);
} catch (AMQPChanncelException $ex) {
    print_r($ex);
} catch (AMQPConnectionException $ex) {
    print_r($ex);
} catch (Exception $ex) {
    print_r($ex);
}
$connection->disconnect();
 /**
  * @param $name
  * @param string $channelName
  * @return \AMQPQueue
  */
 public function getQueue($name = 'default', $channelName = 'default')
 {
     $key = $channelName . '_' . $name;
     if (!isset($this->queues[$key])) {
         $channel = $this->getChannel($channelName);
         if (!isset($this->queuesSettings[$name])) {
             throw new \LogicException('No settings for queue ' . $name);
         }
         $settings = $this->queuesSettings[$name];
         $queue = new \AMQPQueue($channel);
         if ($settings['name'] !== null) {
             $queue->setName($settings['name']);
         }
         $queueFlags = AMQP_NOPARAM;
         if ($settings['durable']) {
             $queueFlags |= AMQP_DURABLE;
         }
         if ($settings['exclusive']) {
             $queueFlags |= AMQP_EXCLUSIVE;
         }
         if ($settings['autoDelete']) {
             $queueFlags |= AMQP_AUTODELETE;
         }
         if ($settings['passive']) {
             $queueFlags |= AMQP_PASSIVE;
         }
         $queue->setFlags($queueFlags);
         $queue->declareQueue();
         $this->queuesSettings[$name]['name'] = $queue->getName();
         $this->queues[$key] = $queue;
         //Warning place possible circular links
         if ($settings['from_exchanges'] !== null) {
             foreach ($settings['from_exchanges'] as $binExchangeName => $routingKeys) {
                 $bindExchange = $this->getExchange($binExchangeName, $channelName);
                 foreach ($routingKeys as $routingKey) {
                     $queue->bind($bindExchange->getName(), $routingKey);
                 }
             }
         }
     }
     return $this->queues[$key];
 }
 /**
  * {@inheritDoc}
  */
 public function getQueueName()
 {
     return $this->queue->getName();
 }
Esempio n. 9
0
 /**
  * @param \AMQPQueue $queue
  *
  * @return \AMQPExchange
  */
 protected function getExchangeForQueue(\AMQPQueue $queue)
 {
     $amqpExchange = new \AMQPExchange($this->channel);
     $queueOptions = $this->getOptionsForQueue($queue->getName());
     // determine exchange to use
     if (array_key_exists('exchange', $queueOptions) && trim($queueOptions['exchange'])) {
         $amqpExchange->setName((string) $queueOptions['exchange']);
     }
     $options = $this->getOptionsForExchange($amqpExchange->getName());
     $flags = $this->getFlagsFromOptions(['durable', 'passive'], $options);
     $amqpExchange->setFlags($flags);
     $amqpExchange->setType(isset($options['type']) ? $options['type'] : AMQP_EX_TYPE_DIRECT);
     return $amqpExchange;
 }
Esempio n. 10
0
 public function execute(\AMQPEnvelope $envelope, \AMQPQueue $queue)
 {
     echo 'Receive `' . $envelope->getBody() . '` from queue `' . $queue->getName() . '`' . PHP_EOL;
 }
Esempio n. 11
0
 /**
  * Declare new queue in AMQP broker
  *
  * @param string $name Queue name
  * @param bool $durable Is queue durable
  * @param bool $exclusive Is queue exclusive
  *
  * @return string New queue name
  *
  * @throws ConnectionException
  * @throws \AMQPException
  */
 public function declareQueue($name, $durable = true, $exclusive = false)
 {
     try {
         $queue = new \AMQPQueue($this->getChannel());
         if ($name !== null) {
             $queue->setName((string) $name);
         }
         $flags = AMQP_NOPARAM;
         $flags |= $durable ? AMQP_DURABLE : AMQP_NOPARAM;
         $flags |= $exclusive ? AMQP_EXCLUSIVE : AMQP_NOPARAM;
         $queue->setFlags($flags);
         $queue->declareQueue();
         return $queue->getName();
     } catch (\AMQPConnectionException $e) {
         $this->channel = null;
         throw new ConnectionException($e->getMessage(), $e->getCode(), $e);
     } catch (\AMQPException $e) {
         $this->channel = null;
         throw $e;
     }
 }
Esempio n. 12
0
 /**
  * @return string
  */
 public function getName()
 {
     return $this->rawQueue->getName();
 }