It does so by checking the return status of the last connect-command.
public isConnected ( ) : boolean | ||
Результат | boolean | True if connected, false otherwise. |
/** * @param callable $callback * @throws NotSubscribedListenerException * @throws \Exception * @return void */ public function subscribe(callable $callback) { $this->callback = $callback; //Check whether connection open or not if (!$this->connection->isConnected()) { $this->connection->connect(); } /** Declare exchange. In case it is already exists it could change it's options */ $this->declareExchange(); //Binding queue to correct exchange if (!$this->getQueue()->bind($this->exchangeConfig['name'])) { throw new NotSubscribedListenerException("Can not bind " . $this->getQueue()->getName() . " to an exchange " . $this->exchangeConfig); } $callback = function (\AMQPEnvelope $message) { switch ($message->getType()) { //Stop consumer by special event case 'eventBus.consumer-stop': $this->getQueue()->ack($message->getDeliveryTag()); call_user_func($this->callback, 'eventBus.consumer-stop'); $this->stopConsumer(); break; default: //If publisher is same BC as subscriber, don't proceed if ($message->getAppId() !== $this->getQueue()->getName()) { call_user_func($this->callback, $this->unpackMessage($message)); } } //Answering to RabbitMQ, that event processed $this->getQueue()->ack($message->getDeliveryTag()); }; $callback->bindTo($this); //Listen events $this->getQueue()->consume($callback); }
/** * Returns an AMQPConnection instance, ensuring that it is connected * @return \AMQPConnection */ private function getConnection() { if (!$this->connection->isConnected()) { $this->connection->connect(); } return $this->connection; }
public function it_should_give_connection_status(\AMQPConnection $connection) { $connection->isConnected()->willReturn(true); $this->isConnected()->shouldReturn(true); $connection->isConnected()->willReturn(false); $this->isConnected()->shouldReturn(false); }
/** * Retrieves connection params from config, then inits connection and channel. * * @see http://www.php.net/manual/en/class.amqpconnection.php * @see http://www.php.net/manual/en/class.amqpchannel.php */ public function __construct() { if (!extension_loaded('amqp')) { Mage::throwException('AMQP extension does not appear to be loaded'); } $config = $this->getConfig(); $this->_connection = new AMQPConnection($config); $this->_connection->connect(); if (!$this->_connection->isConnected()) { Mage::throwException(sprintf("Unable to authenticate to 'amqp://%s:%d' (vhost: %s, user: %s, password: %s)", $config['host'], $config['port'], $config['vhost'], $config['user'], $config['password'])); } $this->_channel = new AMQPChannel($this->_connection); }
/** * Send an activity notice using AMQP * @param ActivityNotice $notice * @return bool */ public function sendActivityNotice($notice) { if (!isset($notice)) { return false; } /** @var array $setting */ $setting = $this->params['amqpSetting']; try { if ($this->amqpClientLibrary == "PhpAmqpLib") { $connection = new AMQPStreamConnection($setting['host'], $setting['port'], $setting['user'], $setting['password']); $channel = $connection->channel(); $msg = new AMQPMessage(JsonHelper::encode($notice)); $channel->basic_publish($msg, $setting['exchangeName'], $setting['routingKey']); $channel->close(); $connection->close(); } elseif ($this->amqpClientLibrary == "PECL") { $connection = new \AMQPConnection(['host' => $setting['host'], 'port' => $setting['port'], 'login' => $setting['user'], 'password' => $setting['password']]); $connection->connect(); if ($connection->isConnected()) { $channel = new \AMQPChannel($connection); $exchange = new \AMQPExchange($channel); $exchange->setName($setting['exchangeName']); $exchange->publish(JsonHelper::encode($notice), $setting['routingKey']); $connection->disconnect(); } } else { return false; } } catch (\Exception $e) { return false; } return true; }
/** * @param $event * @return mixed * @throws NotDeliveredEventException */ public function publish($event) { try { //Prepare message for sending to RabbitMQ list($message, $attributes) = $this->prepareMessage($event); //Check connection, if no reconnect to RabbitMQ if (!$this->connection->isConnected()) { $this->connection->connect(); } $result = $this->getExchange()->publish($message, null, AMQP_NOPARAM, $attributes); $this->exchange = null; //$this->channel = null; return $result; } catch (\Exception $e) { throw new NotDeliveredEventException("Event not delivered to queue", 0, $e); } }
/** * @return \AMQPChannel */ private function getChannel() { if (!$this->channel) { if (!$this->connection->isConnected()) { $this->connection->connect(); } $this->channel = new \AMQPChannel($this->connection); } return $this->channel; }
public function main() { $connection = new \AMQPConnection($this->config->get('mq')); try { $connection->connect(); if (!$connection->isConnected()) { $this->logging->exception("Cannot connect to the broker!" . PHP_EOL); } $this->channel = new \AMQPChannel($connection); $this->exchange = new \AMQPExchange($this->channel); $this->exchange->setName($this->exchangeName); $this->exchange->setType(AMQP_EX_TYPE_DIRECT); //direct类型 $this->exchange->setFlags(AMQP_DURABLE); //持久化 $this->exchange->declareExchange(); //echo "Exchange Status:".$this->exchange->declare()."\n"; //创建队列 $this->queue = new \AMQPQueue($this->channel); $this->queue->setName($this->queueName); $this->queue->setFlags(AMQP_DURABLE); //持久化 $this->queue->declareQueue(); $bind = $this->queue->bind($this->exchangeName, $this->routeKey); //echo "Message Total:".$this->queue->declare()."\n"; //绑定交换机与队列,并指定路由键 //echo 'Queue Bind: '.$bind."\n"; //阻塞模式接收消息 //while(true){ //for($i=0; $i< self::loop ;$i++){ //$this->queue->consume('processMessage', AMQP_AUTOACK); //自动ACK应答 $this->queue->consume(function ($envelope, $queue) { //print_r($envelope); //print_r($queue); $speed = microtime(true); $msg = $envelope->getBody(); $result = $this->loader($msg); $queue->ack($envelope->getDeliveryTag()); //手动发送ACK应答 //$this->logging->info(''.$msg.' '.$result) $this->logging->debug('Protocol: ' . $msg . ' '); $this->logging->debug('Result: ' . $result . ' '); $this->logging->debug('Time: ' . (microtime(true) - $speed) . ''); }); $this->channel->qos(0, 1); //echo "Message Total:".$this->queue->declare()."\n"; //} } catch (\AMQPConnectionException $e) { $this->logging->exception($e->__toString()); } catch (\Exception $e) { $this->logging->exception($e->__toString()); $connection->disconnect(); } }
function getBrokerStatus() { $amqpConnection = new AMQPConnection(); $amqpConnection->setLogin("guest"); $amqpConnection->setPassword("guest"); $amqpConnection->setVhost("/"); $amqpConnection->connect(); if (!$amqpConnection->isConnected()) { log("info", "Cannot connect to the broker!"); return false; } $amqpConnection->disconnect(); return true; }
function __construct() { $connection = new \AMQPConnection(); $connection->connect(); if (!$connection->isConnected()) { throw new \AMQPConnectionException('Rabbit is not connected'); } $this->channel = new \AMQPChannel($connection); $this->exchange = new \AMQPExchange($this->channel); //$this->exchange->delete('Celium'); $this->exchange->setName('Celium'); $this->exchange->setType('direct'); //$this->exchange->setFlags(\AMQP_DURABLE); $this->exchange->declare(); }
private function checkRabbitMQ() { $amqpConnection = new AMQPConnection(); $amqpConnection->setHost($this->mqHost); $amqpConnection->setLogin($this->mqUser); $amqpConnection->setPassword($this->mqPass); $amqpConnection->setVhost($this->mqVhost); $amqpConnection->setPort($this->mqPort); $amqpConnection->connect(); return $amqpConnection->isConnected(); }
/** * Check the channel connection. * * @return bool Indicates whether the channel is connected. */ public function isConnected() { return $this->connection->isConnected(); }
<?php require_once __DIR__ . '/../vendor/autoload.php'; $injector = new Auryn\Injector(); $injector->share('AMQPConnection'); $injector->delegate('AMQPConnection', function () { $connection = new AMQPConnection(['host' => 'localhost', 'port' => 5672, 'login' => 'guest', 'password' => 'guest']); if (!$connection->isConnected()) { $connection->connect(); } return $connection; });
/** * @inheritdoc */ public function isConnected() { return $this->delegate->isConnected(); }
/** * Returns true if the connection is active * * @return boolean */ public function connected() { return $this->_connection !== null && $this->_connection->isConnected(); }