/**
  * {@inheritdoc}
  */
 public function runAndWait(Task $task, $wait = 0, callable $completed = null, callable $timedout = null, callable $errored = null)
 {
     $waitForResult = $wait > 0;
     // Event: before dispatching the task
     $this->triggerEvent(self::EVENT_BEFORE_TASK_DISPATCHED, [$task]);
     // Event: before serialization
     $this->triggerEvent(self::EVENT_BEFORE_TASK_SERIALIZATION, [$task]);
     $messageOptions = ['delivery_mode' => 2];
     $replyExchange = null;
     $replyQueue = null;
     if ($waitForResult) {
         // Create a temporary exchange (durable, autodelete) for communicating with the worker
         $replyExchange = uniqid('tmp');
         $this->channel->exchange_declare($replyExchange, 'fanout', false, true, true);
         // Create and bind a queue for the dispatcher (our queue) (exclusive queue)
         list($replyQueue, , ) = $this->channel->queue_declare('', false, false, true);
         $this->channel->queue_bind($replyQueue, $replyExchange);
         // Create and bind a queue for the worker (durable non-exclusive queue)
         list($workerReplyQueue, , ) = $this->channel->queue_declare('', false, true, false);
         $this->channel->queue_bind($workerReplyQueue, $replyExchange);
         $messageOptions['reply_to'] = $replyExchange . ';' . $workerReplyQueue;
     }
     $message = new AMQPMessage(serialize($task), $messageOptions);
     $this->channel->basic_publish($message, '', $this->queue);
     if ($waitForResult) {
         $this->waitForTask($wait, $replyExchange, $replyQueue, $completed, $timedout, $errored);
     }
 }
Example #2
0
 /**
  * @throws Exception\Configuration
  */
 public function setup()
 {
     $this->connect();
     $exchange = $this->getProperty('exchange');
     if (empty($exchange)) {
         throw new Exception\Configuration('Please check your settings, exchange is not defined.');
     }
     /*
         name: $exchange
         type: topic
         passive: false
         durable: true // the exchange will survive server restarts
         auto_delete: false //the exchange won't be deleted once the channel is closed.
     */
     $this->channel->exchange_declare($exchange, $this->getProperty('exchange_type'), $this->getProperty('exchange_passive'), $this->getProperty('exchange_durable'), $this->getProperty('exchange_auto_delete'), $this->getProperty('exchange_internal'), $this->getProperty('exchange_nowait'), $this->getProperty('exchange_properties'));
     $queue = $this->getProperty('queue');
     if (!empty($queue) || $this->getProperty('queue_force_declare')) {
         /*
             name: $queue
             passive: false
             durable: true // the queue will survive server restarts
             exclusive: false // queue is deleted when connection closes
             auto_delete: false //the queue won't be deleted once the channel is closed.
             nowait: false // Doesn't wait on replies for certain things.
             parameters: array // Extra data, like high availability params
         */
         /** @var ['queue name', 'message count',] queueInfo */
         $this->queueInfo = $this->channel->queue_declare($queue, $this->getProperty('queue_passive'), $this->getProperty('queue_durable'), $this->getProperty('queue_exclusive'), $this->getProperty('queue_auto_delete'), $this->getProperty('queue_nowait'), $this->getProperty('queue_properties'));
         $this->channel->queue_bind($queue ?: $this->queueInfo[0], $exchange, $this->getProperty('routing'));
     }
     // clear at shutdown
     register_shutdown_function([get_class(), 'shutdown'], $this->channel, $this->connection);
 }
 public function setUp()
 {
     $this->conn = $this->createConnection();
     $this->ch = $this->conn->channel();
     $this->ch->exchange_declare($this->exchange_name, 'direct', false, false, false);
     list($this->queue_name, , ) = $this->ch->queue_declare();
     $this->ch->queue_bind($this->queue_name, $this->exchange_name, $this->queue_name);
 }
 protected function setUpAmqp()
 {
     $this->conn = new AMQPSocketConnection($_SERVER['AMQP_HOST'], $_SERVER['AMQP_PORT'], $_SERVER['AMQP_USER'], $_SERVER['AMQP_PASS'], $_SERVER['AMQP_VHOST']);
     $this->channel = $this->conn->channel();
     $this->channel->exchange_declare('event_band.test.exchange', 'topic');
     $this->channel->queue_declare('event_band.test.event');
     $this->channel->queue_bind('event_band.test.event', 'event_band.test.exchange', 'event.#');
 }
Example #5
0
 public function setUp()
 {
     $this->conn = new AMQPConnection(HOST, PORT, USER, PASS, VHOST);
     $this->ch = $this->conn->channel();
     $this->ch->exchange_declare($this->exchange_name, 'direct', false, false, false);
     list($this->queue_name, , ) = $this->ch->queue_declare();
     $this->ch->queue_bind($this->queue_name, $this->exchange_name, $this->queue_name);
 }
 /**
  * @return void
  */
 public function initialize()
 {
     if ($this->initialized) {
         return;
     }
     $this->initialized = true;
     $this->exchange->initialize();
     $this->queue->initialize();
     $this->channel->queue_bind($this->queue->name(), $this->exchange->name());
 }
 /**
  * Set up AMQP connection.
  */
 public function setUp()
 {
     $container = $this->getContainer();
     $exchangeName = 'general';
     $connection = new AMQPConnection($container->getParameter('ongr_task_messenger.publisher.default.amqp.host'), $container->getParameter('ongr_task_messenger.publisher.default.amqp.port'), $container->getParameter('ongr_task_messenger.publisher.default.amqp.user'), $container->getParameter('ongr_task_messenger.publisher.default.amqp.password'));
     $this->channel = $connection->channel();
     list($queueName, , ) = $this->channel->queue_declare();
     $this->channel->queue_bind($queueName, $exchangeName, explode('.', gethostname())[0]);
     $this->channel->basic_consume($queueName, getmypid(), false, true, true, true, [$this, 'verifyMessage']);
 }
Example #8
0
 /**
  * @param $exchange_name
  */
 private function connect($exchange_name)
 {
     if (null !== $this->channel) {
         return;
     }
     $this->channel = $this->connection->channel();
     $this->channel->exchange_declare($exchange_name, 'fanout', false, true, false);
     $this->channel->queue_declare($exchange_name, false, true, false, false);
     $this->channel->queue_bind($exchange_name, $exchange_name);
 }
 /**
  * Create consumer.
  */
 private function initialize()
 {
     $this->messages = [];
     list($queue) = $this->channel->queue_declare('', false, false, true, true);
     $this->channel->queue_bind($queue, $this->exchange);
     $this->channel->basic_consume($queue, '', false, false, false, false, function (AMQPMessage $message) {
         $this->messages[] = $message;
         $this->channel->basic_cancel($message->delivery_info['consumer_tag']);
     });
 }
 /**
  * @return string
  */
 public function initialize()
 {
     $inbox = 'inbox.' . $this->handler->name();
     $queue = $this->handler->name();
     $this->channel->exchange_declare($inbox, 'topic', false, true, false);
     $this->channel->exchange_bind($inbox, 'inbox', '', false, new AMQPTable(['to' => '*']));
     $this->channel->exchange_bind($inbox, 'inbox', '', false, new AMQPTable(['to' => $this->handler->name()]));
     $this->channel->queue_declare($queue, false, true, false, false, false);
     $this->channel->queue_bind($queue, $inbox, '#');
     return $queue;
 }
Example #11
0
 private function declareComponents($routingKey, $exchange, $queue = null)
 {
     $this->channel->exchange_declare('dead_letters', 'topic', false, true, false);
     if ($queue !== null) {
         $this->channel->queue_declare('dead_letter:' . $queue, false, true, false, false);
         $this->channel->queue_bind('dead_letter:' . $queue, 'dead_letters', $routingKey . '.dead_letter');
     }
     $this->channel->exchange_declare($exchange, 'topic', false, true, false);
     if ($queue !== null) {
         $this->channel->queue_declare($queue, false, true, false, false, false, new AMQPTable(['x-dead-letter-exchange' => 'dead_letters', 'x-dead-letter-routing-key' => $routingKey . '.dead_letter']));
         $this->channel->queue_bind($queue, $exchange, $routingKey);
     }
 }
Example #12
0
 /**
  * @param $queue
  *
  * @return string
  */
 protected function getExchange($queue)
 {
     if (!array_key_exists($queue, $this->exchangeList)) {
         /**
          * queue and exchange the same
          */
         $exchange = $queue;
         $this->ch->queue_declare($queue, false, true, false, false);
         $this->ch->exchange_declare($exchange, 'direct', false, true, false);
         $this->ch->queue_bind($queue, $exchange);
         $this->exchangeList[$exchange] = $exchange;
     }
     return $queue;
 }
 /**
  * Setup the exchanges, and queues and channel
  */
 protected function setupChannel()
 {
     $this->ch = $this->conn->channel();
     $this->ch->exchange_declare($this->exchange, 'direct', false, false, false);
     $this->ch->queue_declare($this->queue);
     $this->ch->queue_bind($this->queue, $this->exchange, $this->queue);
 }
 protected function declareAndBindQueue($queue)
 {
     if (!$this->hasQueue($queue)) {
         $this->addQueue($queue);
         $this->channel->queue_declare($queue, false, true, false, false);
         $this->channel->queue_bind($queue, $this->getExchange(), $queue);
         $this->logger->info(sprintf("Queue %s declared.", $queue));
     }
 }
Example #15
0
 /**
  * {@inheritdoc}
  */
 public function delayed($queue, $delay, Job $job)
 {
     $this->exchangeDeclare('delay');
     $delay = $delay * 1000;
     $tmpQueue = $this->queueDeclare('', array('x-expires' => array('I', $delay + 2000), 'x-message-ttl' => array('I', $delay), 'x-dead-letter-exchange' => array('S', 'delay'), 'x-dead-letter-routing-key' => array('S', $queue)));
     $this->channel->queue_bind($queue, 'delay', $queue);
     $msg = $this->prepareMessage($queue, $job);
     return $this->channel->basic_publish($msg, '', reset($tmpQueue));
 }
Example #16
0
 /**
  * Define a queue in the message broker and bind it to an exchange.
  *
  * If the queue is already declared, the configuration MUST be the same. If the queue has not been
  * defined before, a new one will be created.
  *
  * @param $queue
  *
  * @return $this
  *
  * @throws BrokerException
  */
 public function declareQueue($queue)
 {
     if (!$queue instanceof Queue) {
         throw new BrokerException("The queue hasn't been defined");
     }
     $queueConf = $this->channel->queue_declare($queue->getName(), $queue->isPassive(), $queue->isDurable(), $queue->isExclusive(), $queue->isDeclaredAutoDelete(), $queue->isDeclaredAsNoWait(), $queue->getArguments());
     // For unnamed queues, set the random name assigned during the declaration
     if (!$queue->hasName()) {
         $queue->setName($queueConf[0]);
     }
     $routingKeys = $queue->getRoutingKeys();
     // getRoutingKeys() must have at least 1 element, which may be null
     if (count($routingKeys) < 1) {
         throw new BrokerException('Assert: there must be at least 1 routing key');
     }
     // This element will be used to bind the queue and the exchange together.
     foreach ($queue->getRoutingKeys() as $key) {
         $this->channel->queue_bind($queue->getName(), $queue->getExchange()->getName(), $key, $queue->isDeclaredAsNoWait());
     }
     return $this;
 }
Example #17
0
 /**
  * Initializes result objects: creates AMQP objects and binds result queue.
  *
  * @return void
  */
 public function initialize()
 {
     $this->channel = $this->connection->channel();
     // `auto-delete` argument makes sure that queue will be deleted after last queue
     // consumer disconnected. (Won't be deleted if there weren't any consumers)
     // `x-expires` queue option means that queue will be deleted if it's unused for the
     // specified time in milliseconds. Unused means that queue has no consumers,
     // the queue has not been redeclared, and basic.get has not been invoked.
     Util::declareQueue($this->connection, $this->taskId, ['exclusive' => false, 'durable' => true, 'auto_delete' => true, 'arguments' => ['x-expires' => self::EXPIRE_TIME]]);
     Util::declareExchange($this->connection, 'celeryresults', 'direct', ['exclusive' => false]);
     $this->channel->queue_bind($this->taskId, 'celeryresults', $this->taskId);
 }
Example #18
0
 private function init()
 {
     $this->connection = new AMQPStreamConnection($this->host, $this->port, $this->user, $this->pass, '/', false, 'AMQPLAIN', null, 'en_US', 0);
     $this->channel = $this->connection->channel();
     $this->channel->queue_declare(self::QUEUE_NAME, false, true, false, false, false);
     $this->channel->exchange_declare(self::EXCHEAGE_NAME, 'direct');
     $this->channel->queue_bind(self::QUEUE_NAME, self::EXCHEAGE_NAME);
     $this->channel->queue_declare(self::QUEUE_WITH_DELAY_NAME, false, true, false, false, false, array('x-message-ttl' => array('I', self::DELAY * 1000), 'x-dead-letter-exchange' => array('S', self::EXCHEAGE_NAME)));
     $this->channel->exchange_declare(self::EXCHANGE_WITH_DELAY_NAME, 'direct');
     $this->channel->queue_bind(self::QUEUE_WITH_DELAY_NAME, self::EXCHANGE_WITH_DELAY_NAME);
     $this->channel->basic_qos(null, 1, null);
     register_shutdown_function([$this, 'shutdown'], $this->channel, $this->connection);
 }
Example #19
0
 /**
  * Setup consumer.
  */
 protected function setUpConsumer()
 {
     if (isset($this->exchangeOptions['name'])) {
         $this->channel->exchange_declare($this->exchangeOptions['name'], $this->exchangeOptions['type'], $this->exchangeOptions['passive'], $this->exchangeOptions['durable'], $this->exchangeOptions['auto_delete'], $this->exchangeOptions['internal'], $this->exchangeOptions['nowait'], $this->exchangeOptions['arguments'], $this->exchangeOptions['ticket']);
         if (!empty($this->consumerOptions['qos'])) {
             $this->channel->basic_qos($this->consumerOptions['qos']['prefetch_size'], $this->consumerOptions['qos']['prefetch_count'], $this->consumerOptions['qos']['global']);
         }
     }
     list($queueName, , ) = $this->channel->queue_declare($this->queueOptions['name'], $this->queueOptions['passive'], $this->queueOptions['durable'], $this->queueOptions['exclusive'], $this->queueOptions['auto_delete'], $this->queueOptions['nowait'], $this->queueOptions['arguments'], $this->queueOptions['ticket']);
     if (isset($this->exchangeOptions['name'])) {
         $this->channel->queue_bind($queueName, $this->exchangeOptions['name'], $this->routingKey);
     }
     $this->channel->basic_consume($queueName, $this->getConsumerTag(), false, false, false, false, array($this, 'processMessage'));
 }
Example #20
0
 /**
  * @param string        $destination
  * @param \DateTime|int $delay
  *
  * @return array
  */
 protected function declareDelayedQueue(string $destination, $delay) : array
 {
     $delay = $this->getSeconds($delay);
     $destination = $this->getQueue($destination);
     $name = $this->getQueue($destination) . '_deferred_' . $delay;
     $destinationExchange = $this->configExchange['name'] ?: $destination;
     $exchange = $this->configExchange['name'] ?: $destination;
     // declare exchange
     $this->channel->exchange_declare($exchange, $this->configExchange['type'], $this->configExchange['passive'], $this->configExchange['durable'], $this->configExchange['auto_delete']);
     // declare queue
     $this->channel->queue_declare($name, $this->configQueue['passive'], $this->configQueue['durable'], $this->configQueue['exclusive'], $this->configQueue['auto_delete'], false, new AMQPTable(['x-dead-letter-exchange' => $destinationExchange, 'x-dead-letter-routing-key' => $destination, 'x-message-ttl' => $delay * 1000]));
     // bind queue to the exchange
     $this->channel->queue_bind($name, $exchange, $name);
     return [$name, $exchange];
 }
 public function __construct($name, Exchange $exchange, AMQPChannel $channel, $options)
 {
     $this->name = $name;
     $this->exchange = $exchange;
     $this->channel = $channel;
     $this->passive = $options["passive"];
     $this->durable = $options["durable"];
     $this->exclusive = $options["exclusive"];
     $this->auto_delete = $options["auto_delete"];
     $channel->queue_declare($name, $this->passive, $this->durable, $this->exclusive, $this->auto_delete);
     if ($exchange->getName()) {
         $routing_key = $exchange->getType() == "fanout" ? null : $name;
         $channel->queue_bind($name, $exchange->getName(), $routing_key);
     }
 }
 /**
  * @param string       $destination
  * @param DateTime|int $delay
  *
  * @return string
  */
 protected function declareDelayedQueue($destination, $delay)
 {
     $delay = $this->getSeconds($delay);
     $destination = $this->getQueueName($destination);
     $queueName = $destination . '_deferred_' . $delay;
     $exchange = $this->configExchange['name'] ?: $queueName;
     if (isset(static::$registeredDelayed[$queueName])) {
         return;
     }
     static::$registeredDelayed[$queueName] = true;
     $this->declareRabbitMQExchange($exchange);
     $this->declareRabbitMQQueue($queueName, new AMQPTable(['x-dead-letter-exchange' => $destination, 'x-dead-letter-routing-key' => $destination, 'x-message-ttl' => $delay * 1000]));
     // bind queue to the exchange
     $this->channel->queue_bind($queueName, $exchange, $queueName);
     return $queueName;
 }
 /**
  * @param ConsumerContainer $consumerContainer
  *
  * @throws ConsumerException
  */
 private function registerConsumerContainer(ConsumerContainer $consumerContainer)
 {
     $consumerName = $consumerContainer->getConsumerName();
     if (isset($this->consumerContainers[$consumerName])) {
         $currentConsumer = $this->consumerContainers[$consumerName];
         throw new ConsumerException(sprintf('Can not register consumer method [%s] because the consumer method [%s] already uses that name', $consumerContainer->getMethodName(), $currentConsumer->getMethodName()));
     }
     $this->channel->queue_declare($consumerName, false, true, false, false);
     foreach ($consumerContainer->getBindings() as $binding) {
         $this->channel->queue_bind($consumerName, $this->exchangeName, $binding);
     }
     $this->channel->basic_qos($consumerName, $consumerContainer->getPrefetchCount(), false);
     $this->channel->basic_consume($consumerName, '', false, false, false, false, function (AMQPMessage $message) use($consumerContainer) {
         $this->consume($consumerContainer, $message);
         $message->delivery_info['channel']->basic_ack($message->delivery_info['delivery_tag']);
     });
     $this->consumerContainers[$consumerName] = $consumerContainer;
 }
 /**
  * @test
  */
 public function register_consumer_should_bind_queues()
 {
     $consumer = new stdClass();
     $binding2 = 'binding2';
     $binding1 = 'binding1';
     $consumerName = 'myName';
     $bindings = [$binding1, $binding2];
     $containerMock = $this->prophesize(ConsumerContainer::class);
     $containerMock->getBindings()->willReturn($bindings);
     $containerMock->getConsumerName()->willReturn($consumerName);
     $containerMock->getPrefetchCount()->willReturn(1);
     $this->parser->getConsumerMethods($consumer)->willReturn([$containerMock]);
     $this->channel->basic_qos($consumerName, 1, false)->shouldBeCalled();
     $this->channel->basic_consume($consumerName, Argument::any(), Argument::any(), Argument::any(), Argument::any(), Argument::any(), Argument::any())->shouldBeCalled();
     $this->channel->queue_declare($consumerName, Argument::any(), Argument::any(), Argument::any(), Argument::any())->shouldBeCalled();
     $this->channel->queue_bind($consumerName, self::EXCHANGE_NAME, $binding1)->shouldBeCalled();
     $this->channel->queue_bind($consumerName, self::EXCHANGE_NAME, $binding2)->shouldBeCalled();
     $this->manager->registerConsumer($consumer);
 }
Example #25
0
 /**
  * Sit in loop waiting for incoming messages
  * Call event handlers to consume events
  */
 public function consume()
 {
     $handlers = $this->event_handlers;
     $callback = function ($message) use($handlers) {
         $event = json_decode($message->body, true);
         if (array_key_exists($event['name'], $handlers)) {
             try {
                 $handlers[$event['name']]($event);
             } catch (Exception $ex) {
                 print "Error handling " . $event['name'] . "\n\n" . $ex->getMessage();
             }
         }
     };
     $this->connect();
     list($queue_name, , ) = $this->channel->queue_declare("", false, false, true, false);
     $this->channel->queue_bind($queue_name, $this->channel_name);
     $this->channel->basic_consume($queue_name, '', false, true, false, false, $callback);
     while (count($this->channel->callbacks)) {
         $this->channel->wait();
     }
 }
Example #26
0
 /**
  * Declare and bind result queue
  * 
  * @param \PhpAmqpLib\Channel\AMQPChannel $channel AMQP channel
  */
 private function declareResultQueue($channel)
 {
     Util::declareQueue($this->connection, $this->taskId, ['exclusive' => false, 'auto_delete' => true, 'arguments' => ["x-expires" => 3600000]]);
     Util::declareExchange($this->connection, 'celeryresults', 'direct', ['exclusive' => false]);
     $channel->queue_bind($this->taskId, 'celeryresults');
 }
 /**
  * @param callable $callback
  * @param string   $routingKey, default to all ('#')
  */
 public function consume(callable $callback, $routingKey = '#')
 {
     $this->channel->queue_bind($this->queueName, self::CHANNEL_NAME, $routingKey);
     $this->channel->basic_consume($this->queueName, '', false, true, false, false, $callback);
 }
 /**
  * Create a queue.
  *
  * @param string $queueName
  */
 public function createQueue($queueName)
 {
     $this->channel->exchange_declare($this->exchange, 'direct', false, true, false);
     $this->channel->queue_declare($queueName, false, true, false, false);
     $this->channel->queue_bind($queueName, $this->exchange);
 }
Example #29
0
 /**
  * Binds queue to an exchange
  *
  * @param string $queueName
  * @param string $exchangeName
  * @param string $routingKey
  *
  * @return mixed|null
  */
 public function bindQueueExchanger($queueName, $exchangeName, $routingKey = '')
 {
     $this->_channel->queue_bind($queueName, $exchangeName, $routingKey);
 }
 protected function declareQueue()
 {
     $this->channel->queue_declare((string) $this->queueName, $passive = false, $durable = true, $exclusive = false, $autoDelete = false);
     $this->channel->queue_bind($this->queueName, $this->exchangeName, $routingKey = '#');
 }