Example #1
0
 /**
  * @param \PhpAmqpLib\Connection\AbstractConnection $connection
  * @param null $channel_id
  * @param bool $auto_decode
  * @throws \Exception
  */
 public function __construct($connection, $channel_id = null, $auto_decode = true)
 {
     if ($channel_id == null) {
         $channel_id = $connection->get_free_channel_id();
     }
     parent::__construct($connection, $channel_id);
     $this->publish_cache = array();
     $this->publish_cache_max_size = 100;
     if ($this->debug) {
         MiscHelper::debug_msg('using channel_id: ' . $channel_id);
     }
     $this->default_ticket = 0;
     $this->is_open = false;
     $this->active = true;
     // Flow control
     $this->alerts = array();
     $this->callbacks = array();
     $this->auto_decode = $auto_decode;
     try {
         $this->x_open();
     } catch (\Exception $e) {
         $this->close();
         throw $e;
     }
 }
 /**
  * Shut it down, delete exchanges, queues, close connections and channels
  */
 public function tearDown()
 {
     if ($this->ch) {
         $this->ch->exchange_delete($this->exchange);
         $this->ch->queue_delete($this->queue);
         $this->ch->close();
     }
     if ($this->conn) {
         $this->conn->close();
     }
 }
 public function __construct($host, $port, $user, $password, $vhost = "/", $insist = false, $login_method = "AMQPLAIN", $login_response = null, $locale = "en_US", $connection_timeout = 3, $read_write_timeout = 3, $context = null)
 {
     $this->construct_params = func_get_args();
     if ($user && $password) {
         $login_response = new AMQPWriter();
         $login_response->write_table(array("LOGIN" => array('S', $user), "PASSWORD" => array('S', $password)));
         $login_response = substr($login_response->getvalue(), 4);
         //Skip the length
     } else {
         $login_response = null;
     }
     $d = self::$LIBRARY_PROPERTIES;
     while (true) {
         $this->channels = array();
         // The connection object itself is treated as channel 0
         parent::__construct($this, 0);
         $this->channel_max = 65535;
         $this->frame_max = 131072;
         $errstr = $errno = null;
         $this->sock = null;
         //TODO clean up
         if ($context) {
             $remote = sprintf('ssl://%s:%s', $host, $port);
             $this->sock = @stream_socket_client($remote, $errno, $errstr, $connection_timeout, STREAM_CLIENT_CONNECT, $context);
         } else {
             $remote = sprintf('tcp://%s:%s', $host, $port);
             $this->sock = @stream_socket_client($remote, $errno, $errstr, $connection_timeout, STREAM_CLIENT_CONNECT);
         }
         if (!$this->sock) {
             throw new AMQPRuntimeException("Error Connecting to server({$errno}): {$errstr} ");
         }
         if (!stream_set_timeout($this->sock, $read_write_timeout)) {
             throw new \Exception("Timeout could not be set");
         }
         stream_set_blocking($this->sock, 1);
         $this->input = new AMQPReader(null, $this->sock);
         $this->write(self::$AMQP_PROTOCOL_HEADER);
         $this->wait(array("10,10"));
         $this->x_start_ok($d, $login_method, $login_response, $locale);
         $this->wait_tune_ok = true;
         while ($this->wait_tune_ok) {
             $this->wait(array("10,20", "10,30"));
         }
         $host = $this->x_open($vhost, "", $insist);
         if (!$host) {
             return;
             // we weren't redirected
         }
         // we were redirected, close the socket, loop and try again
         $this->close_socket();
     }
 }
 /**
  * @test
  */
 public function it_logs_messages_when_rejecting_a_message()
 {
     $context = [];
     $context['correlation_id'] = new StringLiteral('my-correlation-id-123');
     $this->logger->expects($this->at(0))->method('info')->with('received message with content-type application/vnd.cultuurnet.udb3-events.dummy-event+json', $context);
     $this->logger->expects($this->at(1))->method('error')->with('Deserializerlocator error', $context + ['exception' => new \InvalidArgumentException('Deserializerlocator error')]);
     $this->logger->expects($this->at(2))->method('info')->with('message rejected', $context);
     $this->deserializerLocator->expects($this->once())->method('getDeserializerForContentType')->with(new StringLiteral('application/vnd.cultuurnet.udb3-events.dummy-event+json'))->willThrowException(new \InvalidArgumentException('Deserializerlocator error'));
     $this->channel->expects($this->once())->method('basic_reject')->with('my-delivery-tag');
     $messageProperties = ['content_type' => 'application/vnd.cultuurnet.udb3-events.dummy-event+json', 'correlation_id' => 'my-correlation-id-123'];
     $messageBody = '';
     $message = new AMQPMessage($messageBody, $messageProperties);
     $message->delivery_info['channel'] = $this->channel;
     $message->delivery_info['delivery_tag'] = 'my-delivery-tag';
     $this->eventBusForwardingConsumer->consume($message);
 }
 public function __construct($connection, $channel_id = null, $auto_decode = true)
 {
     if ($channel_id == null) {
         $channel_id = $connection->get_free_channel_id();
     }
     parent::__construct($connection, $channel_id);
     if ($this->debug) {
         MiscHelper::debug_msg("using channel_id: " . $channel_id);
     }
     $this->default_ticket = 0;
     $this->is_open = false;
     $this->active = true;
     // Flow control
     $this->alerts = array();
     $this->callbacks = array();
     $this->auto_decode = $auto_decode;
     $this->x_open();
 }
 public function __construct($user, $password, $vhost = "/", $insist = false, $login_method = "AMQPLAIN", $login_response = null, $locale = "en_US", AbstractIO $io)
 {
     // save the params for the use of __clone
     $this->construct_params = func_get_args();
     $this->wait_frame_reader = new AMQPReader(null);
     if ($user && $password) {
         $login_response = new AMQPWriter();
         $login_response->write_table(array("LOGIN" => array('S', $user), "PASSWORD" => array('S', $password)));
         $login_response = substr($login_response->getvalue(), 4);
         //Skip the length
     } else {
         $login_response = null;
     }
     $this->prepare_content_cache = array();
     $this->prepare_content_cache_max_size = 100;
     $d = self::$LIBRARY_PROPERTIES;
     while (true) {
         $this->channels = array();
         // The connection object itself is treated as channel 0
         parent::__construct($this, 0);
         $this->io = $io;
         $this->input = new AMQPReader(null, $this->io);
         $this->write($this->amqp_protocol_header);
         $this->wait(array($this->waitHelper->get_wait('connection.start')));
         $this->x_start_ok($d, $login_method, $login_response, $locale);
         $this->wait_tune_ok = true;
         while ($this->wait_tune_ok) {
             $this->wait(array($this->waitHelper->get_wait('connection.secure'), $this->waitHelper->get_wait('connection.tune')));
         }
         $host = $this->x_open($vhost, "", $insist);
         if (!$host) {
             return;
             // we weren't redirected
         }
         // we were redirected, close the socket, loop and try again
         $this->close_socket();
     }
 }
Example #7
0
 /**
  * Connects to the AMQP server
  */
 protected function connect()
 {
     try {
         // Loop until we connect
         while (!$this->isConnected()) {
             // Assume we will connect, until we dont
             $this->setIsConnected(true);
             // Connect the socket
             $this->getIO()->connect();
             $this->channels = array();
             // The connection object itself is treated as channel 0
             parent::__construct($this, 0);
             $this->input = new AMQPReader(null, $this->getIO());
             $this->write($this->amqp_protocol_header);
             $this->wait(array($this->waitHelper->get_wait('connection.start')));
             $this->x_start_ok(self::$LIBRARY_PROPERTIES, $this->login_method, $this->login_response, $this->locale);
             $this->wait_tune_ok = true;
             while ($this->wait_tune_ok) {
                 $this->wait(array($this->waitHelper->get_wait('connection.secure'), $this->waitHelper->get_wait('connection.tune')));
             }
             $host = $this->x_open($this->vhost, '', $this->insist);
             if (!$host) {
                 return null;
                 // we weren't redirected
             }
             $this->setIsConnected(false);
             $this->closeChannels();
             // we were redirected, close the socket, loop and try again
             $this->close_socket();
         }
     } catch (\Exception $e) {
         // Something went wrong, set the connection status
         $this->setIsConnected(false);
         $this->closeChannels();
         throw $e;
         // Rethrow exception
     }
 }
 /**
  * @return string
  */
 public static final function getProtocol()
 {
     if (self::$_protocol === null) {
         self::$_protocol = defined('AMQP_STRICT_FLD_TYPES') && AMQP_STRICT_FLD_TYPES ? AbstractChannel::getProtocolVersion() : self::PROTOCOL_RBT;
     }
     return self::$_protocol;
 }