/**
  * {@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);
     }
 }
Beispiel #2
0
 /**
  * Declare amqp queue
  * @param $queueName
  * @param bool|false $passive
  * @param bool|false $durable
  * @param bool|false $exclusive
  * @param bool|false $auto_delete
  */
 public function declareQueue($queueName, $passive = false, $durable = false, $exclusive = false, $auto_delete = false)
 {
     if (!in_array($queueName, $this->declaredQueues)) {
         $this->channel->queue_declare($queueName, $passive, $durable, $exclusive, $auto_delete);
         $this->declaredQueues[] = $queueName;
     }
 }
 /**
  * @param AMQPStreamConnection $conn
  */
 public function __construct(AMQPStreamConnection $conn)
 {
     $this->conn = $conn;
     $this->channel = $conn->channel();
     list($this->queueName) = $this->channel->queue_declare('', false, true, true, false);
     $this->channel->exchange_declare(self::TOPIC_NAME, self::MESSAGE_TYPE, false, true, true, false);
 }
Beispiel #4
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);
 }
Beispiel #5
0
 /**
  * @param AMQPStreamConnection $connection
  * @param string $queueName
  */
 public function __construct(AMQPStreamConnection $connection, $queueName)
 {
     $this->connection = $connection;
     $this->channel = $this->connection->channel();
     $this->queueName = $queueName;
     $this->channel->queue_declare($this->queueName, false, true, false, false);
 }
Beispiel #6
0
 private function __construct()
 {
     $this->connection = new AMQPConnection($this->connection_host, $this->connection_port, $this->connection_user, $this->connection_password);
     $this->channel = $this->connection->channel();
     $this->channel->queue_declare('task_queue_persistent', false, true, false, false);
     return $this;
 }
 protected function setupConnection()
 {
     Yii::trace('Connecting to broker...', __METHOD__);
     $this->connection = new AMQPConnection($this->host, $this->port, $this->user, $this->password, $this->vhost, $this->insist, $this->login_method, $this->login_response, $this->locale, $this->connection_timeout, $this->read_write_timeout, $this->context);
     $this->channel = $this->connection->channel();
     $this->channel->queue_declare($this->queue, false, true, false, false);
 }
 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);
 }
 /**
  * @param string $title
  * @param boolean $durable
  * @param boolean $autoDelete
  * @param array $arguments
  * @throws Exception
  */
 public function createQueue($title, $durable = false, $autoDelete = false, $arguments = null)
 {
     if (!$this->channel) {
         throw new Exception("Channel didn't created");
     }
     $this->channel->queue_declare($title, false, $durable, false, $autoDelete, false, $arguments);
     $this->channel->basic_qos(null, 1, null);
 }
 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.#');
 }
Beispiel #11
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);
 }
 /**
  * @param string $queue
  * @param HttpRequest $httpRequest
  */
 public function start($queue, HttpRequest $httpRequest)
 {
     $this->queue = $queue;
     $this->httpRequest = $httpRequest;
     $this->connection = new AMQPConnection($this->host, $this->port, $this->user, $this->pass);
     $this->channel = $this->connection->channel();
     list(, , $consumer_count) = $this->channel->queue_declare($this->queue, false, false, false, false);
     $this->consumer_count = $consumer_count;
 }
Beispiel #13
0
 private function getChannel()
 {
     if ($this->channel) {
         return $this->channel;
     }
     $this->channel = $this->connection->channel();
     $this->channel->queue_declare($this->queue, false, false, false, false);
     return $this->channel;
 }
 /**
  * Creates (if not yet created) and returns an AMQP channel.
  *
  * @return \PhpAmqpLib\Channel\AMQPChannel
  */
 protected function getChannel()
 {
     if (null === $this->channel) {
         $this->channel = $this->connection->channel();
         $this->channel->queue_declare($this->queueName, false, false, false, false);
         $this->channel->basic_qos(null, 1, null);
     }
     return $this->channel;
 }
 /**
  * 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']);
 }
Beispiel #16
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);
 }
Beispiel #17
0
 /**
  * @return AMQPChannel
  */
 public function getAmqpChannel()
 {
     if ($this->amqp_channel) {
         return $this->amqp_channel;
     }
     $this->amqp_channel = $this->connection->getAmqpConnection()->channel();
     $this->amqp_channel->queue_declare($this->queue_config['queue_name'], false, $is_durable = true, false, false);
     $this->amqp_channel->basic_qos(null, $this->queue_config['fetch_count'], null);
     return $this->amqp_channel;
 }
 /**
  * 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 void
  */
 public function initialize()
 {
     if ($this->initialized) {
         return;
     }
     $this->initialized = true;
     $this->channel->queue_declare($this->name, $this->passive, $this->durable, $this->exclusive, $this->autoDelete, $this->noWait);
     foreach ($this->bindings as $binding) {
         $binding->initialize();
     }
 }
 /**
  * @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;
 }
Beispiel #21
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);
     }
 }
Beispiel #22
0
 public function testDeclaration()
 {
     try {
         $this->ch->queue_declare('pretty.queue', true, true);
         $this->fail('Should have raised an exception');
     } catch (AMQPProtocolException $e) {
         if ($e->getCode() == 404) {
             $this->ch2->queue_declare('pretty.queue', false, true, true, true);
         } else {
             $this->fail('Should have raised a 404 Error');
         }
     }
 }
Beispiel #23
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));
     }
 }
Beispiel #26
0
 /**
  * @param Message $msg
  */
 public function sendMessage(Message $msg)
 {
     /* Create the message */
     $amqpMessage = $msg->getAMQPMessage();
     /* Create queue */
     $this->channel->queue_declare($msg->queueName, false, true, false, false);
     /* Publish message */
     $this->channel->basic_publish($amqpMessage, '', $msg->queueName);
 }
Beispiel #27
0
 /**
  * Initializes the message queue class
  *
  * @param \PhpAmqpLib\Channel\AMQPChannel $channel AMQP channel
  * @param string $queue Message queue name
  * @throws \Aimeos\MW\MQueue\Exception
  */
 public function __construct(\PhpAmqpLib\Channel\AMQPChannel $channel, $queue)
 {
     try {
         $channel->queue_declare($queue, false, true, false, false);
         $channel->basic_qos(null, 1, null);
     } catch (\Exception $e) {
         throw new \Aimeos\MW\MQueue\Exception($e->getMessage());
     }
     $this->channel = $channel;
     $this->queue = $queue;
 }
Beispiel #28
0
 private function reportError($message)
 {
     try {
         $this->channel->queue_declare(self::ERROR_QUEUE_NAME, false, true, false, false);
         $msg = new AMQPMessage($message, array('content_type' => 'text/plain', 'delivery_mode' => 2));
         $this->channel->basic_publish($msg, '', self::ERROR_QUEUE_NAME);
     } catch (\Exception $e) {
         return false;
     }
     return true;
 }
Beispiel #29
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;
 }
Beispiel #30
-1
 public function __construct()
 {
     $this->connection = new AMQPStreamConnection('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, [$this, 'on_response']);
 }