本文整理汇总了PHP中AMQPConnection::isConnected方法的典型用法代码示例。如果您正苦于以下问题:PHP AMQPConnection::isConnected方法的具体用法?PHP AMQPConnection::isConnected怎么用?PHP AMQPConnection::isConnected使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类AMQPConnection
的用法示例。
在下文中一共展示了AMQPConnection::isConnected方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: subscribe
/**
* @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);
}
示例2: getConnection
/**
* Returns an AMQPConnection instance, ensuring that it is connected
* @return \AMQPConnection
*/
private function getConnection()
{
if (!$this->connection->isConnected()) {
$this->connection->connect();
}
return $this->connection;
}
示例3: it_should_give_connection_status
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);
}
示例4: __construct
/**
* 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);
}
示例5: sendActivityNotice
/**
* 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;
}
示例6: publish
/**
* @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);
}
}
示例7: getChannel
/**
* @return \AMQPChannel
*/
private function getChannel()
{
if (!$this->channel) {
if (!$this->connection->isConnected()) {
$this->connection->connect();
}
$this->channel = new \AMQPChannel($this->connection);
}
return $this->channel;
}
示例8: main
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();
}
}
示例9: getBrokerStatus
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;
}
示例10:
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();
}
示例11: checkRabbitMQ
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();
}
示例12: isConnected
/**
* Check the channel connection.
*
* @return bool Indicates whether the channel is connected.
*/
public function isConnected()
{
return $this->connection->isConnected();
}
示例13: function
<?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;
});
示例14: isConnected
/**
* @inheritdoc
*/
public function isConnected()
{
return $this->delegate->isConnected();
}
示例15: connected
/**
* Returns true if the connection is active
*
* @return boolean
*/
public function connected()
{
return $this->_connection !== null && $this->_connection->isConnected();
}