本文整理汇总了PHP中AMQPConnection::connect方法的典型用法代码示例。如果您正苦于以下问题:PHP AMQPConnection::connect方法的具体用法?PHP AMQPConnection::connect怎么用?PHP AMQPConnection::connect使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类AMQPConnection
的用法示例。
在下文中一共展示了AMQPConnection::connect方法的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: connect
/**
* @inheritdoc
*/
public function connect()
{
if ($this->options->getPersistent()) {
$this->connection->pconnect();
} else {
$this->connection->connect();
}
}
示例4: _getConnection
/**
* Connect to server
* @return AMQPConnection
*/
protected function _getConnection()
{
if (!$this->_connection) {
$this->_connection = new \AMQPConnection($this->_config);
if (!$this->_connection->connect()) {
throw new \Exception("Cannot connect to the broker \n");
}
}
return $this->_connection;
}
示例5: __construct
/**
* @param string $protocol
* @param string $encoding
* @param bool $synchronous
* @param array $endpoint
*/
public function __construct($protocol, $encoding, $synchronous = false, array $endpoint = [])
{
parent::__construct($protocol, $encoding, $synchronous, $endpoint);
list($exchangeName, $routingKey) = array_values($this->endpoint);
$credentials = array_filter($this->endpoint);
$this->connection = new \AMQPConnection($credentials);
$this->connection->connect();
$this->channel = new \AMQPChannel($this->connection);
$this->exchange = new \AMQPExchange($this->channel);
$this->exchange->setName($exchangeName);
}
示例6: __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);
}
示例7: init
/**
* @inheritdoc
*/
public function init()
{
if (empty($this->user)) {
throw new \Exception("Parameter 'user' was not set for AMQP connection.");
}
if (empty(self::$ampqConnection)) {
self::$ampqConnection = new \AMQPConnection();
self::$ampqConnection->setHost($this->host);
self::$ampqConnection->setPort($this->port);
self::$ampqConnection->setLogin($this->user);
self::$ampqConnection->setPassword($this->password);
self::$ampqConnection->setVhost($this->vhost);
self::$ampqConnection->connect();
}
}
示例8: setUpAmqp
/**
* {@inheritDoc}
*/
protected function setUpAmqp()
{
$this->conn = new \AMQPConnection(['host' => $_SERVER['AMQP_HOST'], 'port' => $_SERVER['AMQP_PORT'], 'login' => $_SERVER['AMQP_USER'], 'password' => $_SERVER['AMQP_PASS'], 'vhost' => $_SERVER['AMQP_VHOST']]);
$this->conn->connect();
$channel = new \AMQPChannel($this->conn);
$exchange = new \AMQPExchange($channel);
$exchange->setName('event_band.test.exchange');
$exchange->setFlags(AMQP_AUTODELETE);
$exchange->setType(AMQP_EX_TYPE_TOPIC);
$exchange->declareExchange();
$queue = new \AMQPQueue($channel);
$queue->setName('event_band.test.event');
$queue->declareQueue();
$queue->bind('event_band.test.exchange', 'event.#');
}
示例9: 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;
}
示例10: connection
/**
* Returns AMQP connection
*
* @return AMQPConnection
*/
public function connection()
{
if (!$this->connection) {
if (!$this->host || !$this->port) {
throw new \InvalidArgumentException(sprintf("Remote host or port undefined '%s:%s' ", $this->host, $this->port));
}
$this->connection = new \AMQPConnection();
$this->connection->setHost($this->host);
$this->connection->setPort($this->port);
$this->connection->setLogin($this->user);
$this->connection->setPassword($this->password);
$this->vhost and $this->connection->setVhost($this->vhost);
$this->connection->connect();
}
return $this->connection;
}
示例11: resolve
public function resolve(Command $command, InputInterface $input, OutputInterface $output, array $args)
{
$options = array_merge(array('host' => 'localhost', 'port' => 5763, 'login' => null, 'password' => null), $args);
$conn = new \AMQPConnection($options);
$conn->connect();
$channel = new \AMQPChannel($conn);
return new AmqpHandler(new \AMQPExchange($channel), $this->replacePlaceholders($args['name']));
}
示例12: 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);
}
}
示例13: connect
/**
* @inheritdoc
*/
public function connect()
{
try {
return $this->delegate->connect();
} catch (\AMQPConnectionException $e) {
throw new ConnectionException($e->getMessage(), $e->getCode(), $e);
}
}
示例14: getAMQPExchange
protected function getAMQPExchange()
{
$connection = new \AMQPConnection(array('vhost' => 'swarrot'));
$connection->connect();
$channel = new \AMQPChannel($connection);
$exchange = new \AMQPExchange($channel);
$exchange->setName('exchange');
return $exchange;
}
示例15: getAMQPConnection
/**
AMQP Connection
*/
protected function getAMQPConnection()
{
$connection = new AMQPConnection();
$connection->setHost('127.0.0.1');
$connection->setLogin('guest');
$connection->setPassword('guest');
$connection->connect();
return $connection;
}