/** * @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); }
/** * @param \AMQPExchange|null $exchange * * @throws HectorException */ public function initialize(\AMQPExchange $exchange = null) { if (null == $exchange) { $exchange = new \AMQPExchange($this->channel->getWrappedChannel()); } $this->exchange = $exchange; $this->exchange->setName($this->getName()); $this->exchange->setType($this->context->getType()); $this->exchange->setArguments($this->context->getArguments()); $this->exchange->setFlags((int) $this->context->getFlags()); $this->exchange->declareExchange(); $this->initialized = 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(); }
public function send($msg) { $ex = new \AMQPExchange($this->amqpChannel); $ex->setName($this->name); $ex->publish($msg); return $this; }
protected function execute(InputInterface $input, OutputInterface $output) { $output->writeln(sprintf('Move messages from queue "%s" (vhost: "%s") to exchange "%s" with routingKey "%s" (vhost: "%s")', $input->getArgument('from_queue'), $input->getArgument('from_vhost'), $input->getArgument('to_exchange'), $input->getArgument('to_routing_key'), $input->getArgument('to_vhost'))); $fromChannel = $this->getChannel($input->getArgument('from_connection'), $input->getArgument('from_vhost')); if (null === ($toConnectionName = $input->getOption('to_connection'))) { $toChannel = $this->getChannel($input->getArgument('from_connection'), $input->getArgument('to_vhost')); } else { $toChannel = $this->getChannel($input->getOption('to_connection'), $input->getArgument('to_vhost')); } $queue = new \AMQPQueue($fromChannel); $queue->setName($input->getArgument('from_queue')); $exchange = new \AMQPExchange($toChannel); $exchange->setName($input->getArgument('to_exchange')); $messageProvider = new PeclPackageMessageProvider($queue); $messagePublisher = new PeclPackageMessagePublisher($exchange); $options = array(); $stack = new \Swarrot\Processor\Stack\Builder(); if (0 !== ($max = (int) $input->getOption('max-messages'))) { $stack->push('Swarrot\\Processor\\MaxMessages\\MaxMessagesProcessor'); $options['max_messages'] = $max; } $stack->push('Swarrot\\Processor\\Insomniac\\InsomniacProcessor'); $stack->push('Swarrot\\Processor\\Ack\\AckProcessor', $messageProvider); $processor = $stack->resolve(new MoveProcessor($messagePublisher, $input->getArgument('to_routing_key'))); $consumer = new Consumer($messageProvider, $processor); $consumer->consume($options); }
/** * 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; }
/** * 广播形式写入队列 * * @Author tianyunzi * @DateTime 2015-08-19T18:44:42+0800 */ public function setBroadcast($exName, $routingKey, $value) { //创建交换机 $ex = new \AMQPExchange($this->channel); $ex->setName($exName); //入队列 $ex->publish($value, $routingKey, AMQP_NOPARAM, array('delivery_mode' => '2')); }
/** * Post a task to exchange specified in $details * @param AMQPConnection $connection Connection object * @param array $details Array of connection details * @param string $task JSON-encoded task * @param array $params AMQP message parameters */ function PostToExchange($connection, $details, $task, $params) { $ch = $connection->channel; $xchg = new AMQPExchange($ch); $xchg->setName($details['exchange']); $success = $xchg->publish($task, $details['binding'], 0, $params); return $success; }
public function declareExchange($name, $type = AMQP_EX_TYPE_TOPIC, $flags = AMQP_DURABLE) { $exchange = new \AMQPExchange($this->channel); $exchange->setName($name); $exchange->setType($type); $exchange->setFlags($flags); $exchange->declareExchange(); return $exchange; }
/** * @inheritdoc */ public function createExchange(ChannelInterface $channel, $name, $type = ExchangeInterface::TYPE_DIRECT, $flags = null, array $args = []) { $delegate = new \AMQPExchange($channel->getDelegate()); $delegate->setName($name); $delegate->setType($type); $delegate->setFlags(Exchange::convertToDelegateFlags($flags)); $delegate->setArguments($args); return new Exchange($delegate, $channel); }
protected function getAMQPExchange() { $connection = new \AMQPConnection(array('vhost' => 'swarrot')); $connection->connect(); $channel = new \AMQPChannel($connection); $exchange = new \AMQPExchange($channel); $exchange->setName('exchange'); return $exchange; }
/** * {@inheritDoc} */ public function register(Container $c) { $c['amqp.connections.initializer'] = function ($c) { $config = $c['amqp.options']; $connections = array(); if (isset($config['connections'])) { foreach ($config['connections'] as $name => $options) { $connections[$name] = new \AMQPConnection($options); } return $connections; } if (isset($config['connection'])) { return array('default' => new \AMQPConnection($config['connection'])); } throw new \LogicException('No connection defined'); }; $c['queue.factory'] = function ($c) { $connections = $c['amqp.connections.initializer']; return function ($queueName, $connectionName = null) use($connections) { $names = array_keys($connections); if (null === $connectionName) { $connectionName = reset($names); } if (!array_key_exists($connectionName, $connections)) { throw new \InvalidArgumentException(sprintf('Unknown connection "%s". Available: [%s]', $connectionName, implode(', ', $names))); } $connection = $connections[$connectionName]; if (!$connection->isConnected()) { $connection->connect(); } $channel = new \AMQPChannel($connection); $queue = new \AMQPQueue($channel); $queue->setName($queueName); return $queue; }; }; $c['exchange.factory'] = function ($c) { $connections = $c['amqp.connections.initializer']; return function ($exchangeName, $connectionName = null) use($connections) { $names = array_keys($connections); if (null === $connectionName) { $connectionName = reset($names); } if (!array_key_exists($connectionName, $connections)) { throw new \InvalidArgumentException(sprintf('Unknown connection "%s". Available: [%s]', $connectionName, implode(', ', $names))); } $connection = $connections[$connectionName]; if (!$connection->isConnected()) { $connection->connect(); } $channel = new \AMQPChannel($connection); $exchange = new \AMQPExchange($channel); $exchange->setName($exchangeName); return $exchange; }; }; }
/** * Publishes a message * * @param AMQPMessage $msg * @param string $exchange * @param string $routing_key * @param bool $mandatory * @param bool $immediate * @param null $ticket */ public function basic_publish($msg, $exchange = '', $routing_key = '', $mandatory = false, $immediate = false, $ticket = null) { $flags = AMQP_NOPARAM; $flags += $mandatory ? AMQP_MANDATORY : 0; $flags += $immediate ? AMQP_IMMEDIATE : 0; $xchange = new \AMQPExchange($this->channel); $xchange->setName($exchange); $xchange->publish($msg->body, $routing_key, $flags, $msg->get_properties()); }
/** * @param string $name * * @return \AMQPExchange */ protected function getExchange($name) { if (!isset($this->exchanges[$name])) { $exchange = new \AMQPExchange($this->getChannel()); $exchange->setName($name); $this->exchanges[$name] = $exchange; } return $this->exchanges[$name]; }
/** * getExchange * * @param string $name * @param string $connection * * @return \AMQPExchange */ public function getExchange($name, $connection) { if (!isset($this->exchanges[$connection][$name])) { if (!isset($this->exchanges[$connection])) { $this->exchanges[$connection] = array(); } $exchange = new \AMQPExchange($this->getChannel($connection)); $exchange->setName($name); $this->exchanges[$connection][$name] = $exchange; } return $this->exchanges[$connection][$name]; }
/** * Get transport exchange for current channel. * @return AMQPExchange */ protected function _getExchange() { if (!$this->_exchange) { $this->_exchange = new \AMQPExchange($this->_getChannel()); //设置exchange名称 $this->_exchange->setName($this->_params['exchange']); //设置exchange类型 $this->_exchange->setType(AMQP_EX_TYPE_DIRECT); //设置exchange参数 $this->_exchange->setFlags(AMQP_DURABLE | AMQP_AUTODELETE); } return $this->_exchange; }
/** * Sends a message to the specified exchange. * ATM, exchange must already exists on the broker. * * @param string $msg * @param string $exchangeName * @param string $routingKey * @param boolean $mandatory * @param boolean $immediate * @param array $attributes * @return boolean * @see http://www.php.net/manual/en/amqpexchange.publish.php */ public function send($msg, $exchangeName = '', $routingKey = '', $mandatory = false, $immediate = false, array $attributes = array()) { $exchange = new AMQPExchange($this->_channel); $exchange->setName($exchangeName); $flags = AMQP_NOPARAM; if ($mandatory) { $flags |= AMQP_MANDATORY; } if ($immediate) { $flags |= AMQP_IMMEDIATE; } return $exchange->publish((string) $msg, (string) $routingKey, $flags, $attributes); }
/** * @return \AMQPExchange * @throws \Exception */ protected function getExchange() { if (!$this->exchange) { $this->exchange = new \AMQPExchange($this->getChannel()); $this->exchange->setName($this->exchangeConfig['name']); $this->exchange->setType($this->exchangeConfig['type']); $this->exchange->setArguments($this->exchangeConfig['arguments']); $this->exchange->setFlags($this->exchangeConfig['flags']); if (!$this->exchange->declareExchange()) { throw new \Exception('Can not declare exchange ' . $this->exchange->getName()); } } return $this->exchange; }
/** * {@inheritDoc} */ public function createExchange() { // Create AMQP connection $amqpConnection = new \AMQPConnection(['host' => $this->host, 'port' => $this->port, 'vhost' => $this->vhost, 'login' => $this->login, 'password' => $this->password]); $amqpConnection->connect(); // Create channel $channel = new \AMQPChannel($amqpConnection); // Create exchange $exchange = new \AMQPExchange($channel); $exchange->setName($this->exchangeName); $exchange->setType($this->exchangeType); $exchange->setFlags(AMQP_DURABLE); $exchange->declareExchange(); return $exchange; }
public static function setupConnection(\AMQPConnection $connection, $config) { $channel = new \AMQPChannel($connection); $exchange = new \AMQPExchange($channel); $exchange->setName($config['exchange_name']); $exchange->setType(AMQP_EX_TYPE_DIRECT); $exchange->setFlags(AMQP_DURABLE); $exchange->declareExchange(); $queue = new \AMQPQueue($channel); $queue->setName($config['queue_name']); $queue->setFlags(AMQP_DURABLE); $queue->declareQueue(); // $exchange->bind($config['queue_name'], 'gos.websocket.pusher'); return [$channel, $exchange, $queue]; }
protected function exchangeDeclare() { if ($this->exchange === null) { $this->exchange = new AMQPExchange($this->channel); } $this->exchange->setName($this->exchangeOptions['name']); $this->exchange->setType($this->exchangeOptions['type']); $this->exchange->setFlags($this->getFlagsFromOptions()); $this->exchange->setArguments($this->exchangeOptions['arguments']); $this->exchange->declareExchange(); foreach ($this->exchangeOptions['binding'] as $bindOpt) { $this->exchange->bind($bindOpt['name'], $bindOpt['routing-keys']); } $this->exchangeDeclared = true; }
/** * {@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.#'); }
public function __construct($AMQPConfiguration, $exchangeName = '') { if (!class_exists('AMQPConnection')) { throw new \LogicException("Can't find AMQP lib"); } $ampq = new \AMQPConnection($AMQPConfiguration); if ($ampq->connect()) { $channel = new \AMQPChannel($ampq); $exchange = new \AMQPExchange($channel); $exchange->setName($exchangeName); $exchange->declare(); $this->channel = $channel; $this->exchange = $exchange; } else { throw new \RuntimeException("Can't connect to AMQP server"); } }
public static function main() { self::$pool = new Pool(MAX_CONCURRENCY_JOB, \CALLWorker::class, []); $conn_args = (require 'mq.config.php'); $e_name = 'e_linvo'; //交换机名 $q_name = 'q_linvo'; //队列名 $k_route = 'key_1'; //路由key //创建连接和channel $conn = new AMQPConnection($conn_args); if (!$conn->connect()) { write_log('mq.hx9999.com 无法连接上'); return; } $channel = new AMQPChannel($conn); //创建交换机 $ex = new AMQPExchange($channel); $ex->setName($e_name); $ex->setType(AMQP_EX_TYPE_DIRECT); //direct类型 $ex->setFlags(AMQP_DURABLE); //持久化 //创建队列 $q = new AMQPQueue($channel); $q->setName($q_name); $q->setFlags(AMQP_DURABLE); //持久化 while (True) { $q->consume(function ($envelope, $queue) { $msg = $envelope->getBody(); $queue->ack($envelope->getDeliveryTag()); //手动发送ACK应答 self::$pool->submit(new Sendsms($msg)); }); } self::$pool->shutdown(); $conn->disconnect(); //睡眠 sleep(5); }
public function send($msg) { $channel = new \AMQPChannel($this->connection); $exchange = new \AMQPExchange($channel); $exchange->setFlags(AMQP_DURABLE); $exchange->setName('exchange2'); $exchange->setType('direct'); //$exchange->declare(); $exchange->declareExchange(); $queue = new \AMQPQueue($channel); $queue->setName('series'); $queue->setFlags(AMQP_DURABLE | AMQP_AUTODELETE); //$queue->declare(); $queue->declareQueue(); $queue->bind('exchange2', 'series'); $channel->startTransaction(); $message = $exchange->publish(json_encode($msg), 'series', AMQP_MANDATORY, array('content_type' => 'application/json', 'delivery_mode' => 2)); $channel->commitTransaction(); if (!$message) { throw new \SYSTEM\LOG\ERROR("Error: Message '" . $message . "' was not sent to queue.!"); } }
/** * 入RabbitMQ队列 * @param [type] $exName [交换机名] * @param [type] $routingKey [路由名] * @param [type] $value [队列的值] * @param [type] $dbType [数据库类型,默认为mysql] * 按照此规则生成的默认队列名称为 exName_routeKey_dbType;值为value */ public function set($exName, $routingKey, $value, $dbType = 'mysql') { //创建交换机,设置交换机名 $ex = new \AMQPExchange($this->channel); $ex->setName($exName); $ex->setType(AMQP_EX_TYPE_DIRECT); //广播模式 $ex->setFlags(AMQP_DURABLE); //交换器进行持久化,即 RabbitMQ 重启后会自动重建 // $ex->declareExchange(); //设置队列名 $queue = new \AMQPQueue($this->channel); $queue->setName($exName . '_' . $routingKey . '_' . $dbType); $queue->setFlags(AMQP_DURABLE); //队列进行持久化,即 RabbitMQ 重启后会自动重建 $queue->declareQueue(); //交换机和路由绑定到队列 $queue->bind($exName, $routingKey); //入队列 if (is_array($value)) { $value = json_encode($value); } $ex->publish($value, $routingKey, AMQP_NOPARAM, array('delivery_mode' => '2')); }
/** * 批量打入v3work_seo_pdrec数据,用于预热本企业推荐产品 */ function writeRMQ($exchangeName = 'v3www', $router, $data = '') { $cfg = array('host' => '192.168.8.18', 'port' => '5672', 'login' => 'v3work', 'password' => 'gc7232275', 'vhost' => '/'); if (empty($exchangeName) || empty($data) || empty($cfg)) { return false; } $reTryNum = 3; $errMsg = array(); do { try { //建立到rmq服务器链接 $connection = new AMQPConnection($cfg); $connection->connect(); //建立channel对象 $channel = new AMQPChannel($connection); //建立交换机 $exchange = new AMQPExchange($channel); $exchange->setName($exchangeName); $exchange->setType(AMQP_EX_TYPE_DIRECT); $exchange->setFlags(AMQP_DURABLE); $exchange->declare(); $routingKey = $router; //发消息 $ret = $exchange->publish($data, $routingKey, AMQP_NOPARAM, array('delivery_mode' => '2')); //关闭链接 $connection->disconnect(); return $ret; } catch (Exception $e) { $connection->disconnect(); $errMsg[] = $e->getMessage(); usleep(5000); $reTryNum -= 1; } } while ($reTryNum); return false; }
/** * @param \AMQPExchange $exchange AMQP exchange, ready for use * @param string $exchangeName * @param int $level * @param bool $bubble Whether the messages that are handled can bubble up the stack or not */ public function __construct(\AMQPExchange $exchange, $exchangeName = 'log', $level = Logger::DEBUG, $bubble = true) { $this->exchange = $exchange; $this->exchange->setName($exchangeName); parent::__construct($level, $bubble); }
<?php $cnn = new AMQPConnection(); $ret = $cnn->connect(); $ch = new AMQPChannel($cnn); $ex = new AMQPExchange($ch); $ex->setName('exchange-' . microtime(true)); $ex->setType(AMQP::TYPE_FANOUT); $ex->setArguments(array("x-ha-policy" => "all")); $ex->setFlags(AMQP::PASSIVE | AMQP::DURABLE | AMQP::AUTODELETE | AMQP::INTERNAL); var_dump($ex); $ex->setFlags(AMQP::NOPARAM); echo $ex->getFlags(); $ex->setFlags(2); echo $ex->getFlags(); $ex->setFlags(0); echo $ex->getFlags(); $ex->setFlags(2); echo $ex->getFlags(); // --EXPECT-- 0202 $cnn->disconnect();
<?php include 'web.php'; include 'image_analyzer.php'; include 'storage.php'; /** * Подключаемся к брокеру и точке обмена сообщениями */ $rabbit = new AMQPConnection(array('host' => '127.0.0.1', 'port' => '5672', 'login' => 'guest', 'password' => 'guest')); $rabbit->connect(); $channel = new AMQPChannel($rabbit); $queue = new AMQPExchange($channel); $queue->setName('amq.direct'); /** * Добавляем очередь откуда будем брать страницы */ $q = new AMQPQueue($channel); $q->setName('images_to_scan'); $q->declare(); $q->bind('amq.direct', 'analyze_image'); /** * Обрабатываем пока в очереди не закончатся сообщения */ while (true) { $image = $q->get(); if ($image) { $url = $image->getBody(); echo "Checking: {$url}\n"; $analyzer = new ImageAnalyzer($url); /** * Если картинка еще не была проанализирована, обрабатываем и добавляем в индекс