Beispiel #1
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);
 }
 /**
  * @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);
 }
 /**
  * {@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);
     }
 }
 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.#');
 }
 /**
  * Gets publisher AMQ channel
  *
  * @return AMQPChannel
  */
 protected function getChannel()
 {
     if (null === $this->channel) {
         $this->channel = $this->amq->channel();
         $this->channel->exchange_declare($this->exchange, 'topic', false, true, false);
     }
     return $this->channel;
 }
 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);
 }
Beispiel #8
0
 /**
  * Declare a exchange in the message broker.
  *
  * If the exchange is already declared, the configuration MUST match. If the exchange has not been
  * defined before, it'll be created.
  *
  * @param AbstractExchange $exchange
  *
  * @return $this
  *
  * @throws BrokerException
  */
 public function declareExchange($exchange)
 {
     if (!$exchange instanceof AbstractExchange) {
         throw new BrokerException("The exchange hasn't been defined");
     }
     $this->channel->exchange_declare($exchange->getName(), $exchange->getExchangeType(), $exchange->isPassive(), $exchange->isDurable(), $exchange->isDeclaredAutoDelete(), $exchange->isInternal(), $exchange->isDeclaredAsNoWait(), $exchange->getArguments());
     return $this;
 }
Beispiel #9
0
 /**
  *
  */
 private function connect()
 {
     if (!$this->connection) {
         $this->connection = new AMQPStreamConnection($this->host, $this->port, 'guest', 'guest');
         $this->channel = $this->connection->channel();
         $this->channel->exchange_declare($this->channel_name, 'fanout', false, false, false);
     }
 }
Beispiel #10
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);
 }
 /**
  * @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;
 }
 /**
  * @return void
  */
 public function initialize()
 {
     if ($this->initialized) {
         return;
     }
     $this->initialized = true;
     $this->channel->exchange_declare($this->name, $this->type, $this->passive, $this->durable, $this->autoDelete);
     foreach ($this->bindings as $binding) {
         $binding->initialize();
     }
 }
Beispiel #13
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 #14
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;
 }
 /**
  * {@inheritdoc}
  */
 protected function setUp()
 {
     if (!isset($_SERVER['AMQP_HOST'])) {
         $this->markTestSkipped('This test require AMQP Server, use AMQP_HOST, AMQP_PORT, AMQP_USER, AMQP_PASSWORD, AMQP_VHOST environment variables to set connection details');
     }
     $this->exchange = uniqid('phpunit.');
     $env = function ($key, $default = null) {
         return isset($_SERVER[$key]) ? $_SERVER[$key] : $default;
     };
     $this->connection = new AMQPStreamConnection($env('AMQP_HOST', 'localhost'), $env('AMQP_PORT', 5672), $env('AMQP_USER', 'guest'), $env('AMQP_PASSWORD', 'guest'), $env('AMQP_VHOST', '/'));
     $this->channel = $this->connection->channel();
     $this->channel->exchange_declare($this->exchange, 'fanout');
     $this->client = new ServiceBus(new Driver($this->channel, $this->exchange, new JsonEncoder()));
     $this->initialize();
 }
 /**
  * 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);
 }
 /**
  *
  * Bootstraps the connection and the channel
  *
  * @throws \GraphAware\SimpleMQ\Exception\SimpleMQException
  */
 public function run()
 {
     $conn = $this->getExchange()->getConnection();
     try {
         $this->connection = new AMQPConnection($conn->getHost(), $conn->getPort(), $conn->getUser(), $conn->getPassword(), $conn->getVhost());
         $this->channel = $this->connection->channel();
     } catch (AMQPRuntimeException $e) {
         throw new SimpleMQException($e->getMessage());
     } catch (AMQPProtocolException $e) {
         throw new SimpleMQException($e->getMessage());
     }
     $this->channel->exchange_declare($this->getExchange()->getName(), $this->getExchange()->getType(), false, $this->getExchange()->isIsDurable());
     if ($this->getExchange()->isIsDurable()) {
         $this->properties['delivery_mode'] = 2;
     }
 }
Beispiel #18
0
 /**
  */
 public function initialize()
 {
     if ($this->initialized) {
         return;
     }
     $this->initialized = true;
     $this->channel->exchange_declare($this->name, $this->type, $this->passive, $this->durable, $this->autoDelete, $this->internal, $this->noWait, $this->arguments ? new AMQPTable($this->arguments) : null, $this->ticket);
     foreach ($this->bindings as $binding) {
         $binding->initialize();
     }
 }
 public function __construct($name, AMQPChannel $channel, $options)
 {
     $this->name = $name;
     $this->channel = $channel;
     $this->type = isset($options["type"]) ? $options["type"] : "direct";
     $this->passive = isset($options["passive"]) ? $options["passive"] : false;
     $this->durable = isset($options["durable"]) ? $options["durable"] : true;
     $this->auto_delete = isset($options["auto_delete"]) ? $options["auto_delete"] : false;
     if ($name != "") {
         $channel->exchange_declare($name, $this->type, $this->passive, $this->durable, $this->auto_delete);
     }
 }
Beispiel #20
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);
 }
Beispiel #21
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'));
 }
 /**
  * @inheritdoc
  */
 public function init()
 {
     parent::init();
     if ($this->amqp == null) {
         $this->amqp = Yii::$app->get('amqp');
     }
     $this->exchange = $this->amqp->defaultExchange;
     if (isset($this->amqp->exchanges[$this->exchange])) {
         if (empty($this->exchangeType)) {
             $this->exchangeType = isset($this->amqp->exchanges[$this->exchange]['type']) ? $this->amqp->exchanges[$this->exchange]['type'] : self::EXCHANGE_TYPE_TOPIC;
         }
         if (isset($this->amqp->exchanges[$this->exchange]['options'])) {
             $this->exchangeOptions = ArrayHelper::merge($this->amqp->exchanges[$this->exchange]['options'], $this->exchangeOptions);
         }
     }
     $exchangeTypes = [self::EXCHANGE_TYPE_DIRECT, self::EXCHANGE_TYPE_TOPIC, self::EXCHANGE_TYPE_FANOUT];
     if (!in_array($this->exchangeType, $exchangeTypes)) {
         throw new InvalidConfigException("Invalid exchange type {$this->exchangeType}");
     }
     if ($this->channel == null) {
         $this->channel = $this->amqp->getConnection()->channel($this->channelId);
     }
     $this->channel->exchange_declare($this->exchange, $this->exchangeType, isset($this->exchangeOptions['passive']) ? $this->exchangeOptions['passive'] : false, isset($this->exchangeOptions['durable']) ? $this->exchangeOptions['durable'] : false, isset($this->exchangeOptions['autoDelete']) ? $this->exchangeOptions['autoDelete'] : false);
 }
Beispiel #23
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];
 }
Beispiel #24
0
 /**
  * Declares exchange
  *
  * @param string $exchange
  * @param string $type
  * @param bool $passive
  * @param bool $durable     The exchange will survive server restarts.
  * @param bool $autoDelete  The exchange won't be deleted once the channel is closed.
  *
  * @return mixed|null
  */
 public function declareExchange($exchange, $type = 'direct', $passive = false, $durable = true, $autoDelete = false)
 {
     return $this->_channel->exchange_declare($exchange, $type, $passive, $durable, $autoDelete);
 }
 /**
  * @param string $name
  */
 protected function declareRabbitMQExchange($name)
 {
     $this->channel->exchange_declare($name, $this->configExchange['type'], $this->configExchange['passive'], $this->configExchange['durable'], $this->configExchange['auto_delete']);
 }
 /**
  * Calls declare_exchange on channel with arguments defined in descriptor
  * @param AMQPChannel $channel
  */
 public function declareExchange(AMQPChannel $channel)
 {
     $channel->exchange_declare($this->getName(), $this->getType(), $this->isPassive(), $this->isDurable(), $this->isAutoDelete(), $this->isInternal(), $this->isNowait(), $this->getArguments(), $this->getTicket());
 }
Beispiel #27
0
 /**
  * Declares a new exchange
  *
  * @param string $exchange
  * @param string $type
  *
  * @codeCoverageIgnore
  */
 protected function exchangeDeclare($exchange, $type = 'direct')
 {
     return $this->channel->exchange_declare($exchange, $type, false, $this->persistent, false);
 }
Beispiel #28
0
 /**
  * @Given there is an exchange :exchange
  */
 public function thereIsAnExchange($exchange)
 {
     $this->channel->exchange_declare($exchange, 'direct');
 }
 protected function exchangeDeclare()
 {
     $this->logger->info(sprintf("Declaring %s exchange with name %s", $this->getExchangeType(), $this->getExchange()));
     $this->channel->exchange_declare($this->getExchange(), $this->getExchangeType(), false, true, false);
     return $this;
 }
 /**
  * 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);
 }