/** * @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 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); }
/** * 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; }
/** * @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; }
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.#'); }
/** * {@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 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.!"); } }
/** * 批量打入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; }
/** * 入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')); }
//Establish connection to AMQP $connection = new AMQPConnection(); $connection->setHost('127.0.0.1'); $connection->setLogin('guest'); $connection->setPassword('guest'); $connection->connect(); //Declare Channel $channel = new AMQPChannel($connection); $routing_key = $severity = isset($argv[1]) && !empty($argv[1]) ? $argv[1] : 'info'; $message = implode(' ', array_slice($argv, 2)); if (empty($message)) { $message = 'Hello World!'; } try { //Declare Exchange $exchange_name = 'direct_logs'; $exchange = new AMQPExchange($channel); $exchange->setType(AMQP_EX_TYPE_DIRECT); $exchange->setName($exchange_name); $exchange->declareExchange(); $queue = new AMQPQueue($channel); $queue->setFlags(AMQP_EXCLUSIVE); $queue->setName('monitor.1'); $queue->declareQueue(); $queue->bind($exchange_name, $routing_key); $exchange->publish($message, $routing_key); echo " [x] Sent {$severity}:{$message}", PHP_EOL; } catch (Exception $ex) { print_r($ex); } $connection->disconnect();
<?php //Establish connection to AMQP $connection = new AMQPConnection(); $connection->setHost('127.0.0.1'); $connection->setLogin('guest'); $connection->setPassword('guest'); $connection->connect(); //Create and declare channel $channel = new AMQPChannel($connection); try { //Declare Exchange $exchange_name = 'logs'; $exchange = new AMQPExchange($channel); $exchange->setType(AMQP_EX_TYPE_FANOUT); $exchange->setName($exchange_name); $exchange->declareExchange(); //Do not declasre the queue name by setting AMQPQueue::setName() $queue = new AMQPQueue($channel); $queue->setFlags(AMQP_EXCLUSIVE); $queue->declareQueue(); $queue->bind($exchange_name, $queue->getName()); echo sprintf("Queue Name: %s", $queue->getName()), PHP_EOL; } catch (Exception $ex) { print_r($ex); } //Read from stdin $message = implode(' ', array_slice($argv, 1)); if (empty($message)) { $message = "info: Hello World!"; }
<?php require 'config.php'; $connection = new AMQPConnection($connect); $connection->connect(); $channel = new AMQPChannel($connection); $exchange = new AMQPExchange($channel); $exchange->setName($exname); $exchange->setType(AMQP_EX_TYPE_TOPIC); $exchange->setFlags(AMQP_DURABLE); $exchange->declare(); $queue = new AMQPQueue($channel); $queue->setName($qname); $queue->setFlags(AMQP_DURABLE); $queue->declare(); $queue->bind($exname, $routekey); $channel->startTransaction(); $message = json_encode(array('Hello World! 测试消息!', 'TOPIC')); $exchange->publish($message, $routekey); $channel->commitTransaction(); $connection->disconnect();
} $e_name = 'ex_test5'; //交换机名 $q_name = 'q_test5'; //队列名称 $r_key = 'key_test1'; //消息 $message = json_encode(array("HelloWorld")); //创建channel $channel = new AMQPChannel($conn); //注:队列跟exchange 都需要基于channel channel基于connection $ex = new AMQPExchange($channel); //新建exchange $ex->setName($e_name); //创建exchange的名字 $ex->setType(AMQP_EX_TYPE_DIRECT); //TPYE DIRECT 添加进绑定key的所有的队列,FANOUT 添加进绑定的所有队列 TOPIC 自定义的规则 . 连接多个队列 #为通配符 $ex->setFlags(AMQP_DURABLE | AMQP_AUTODELETE); echo "exchange status:" . $ex->declare(); echo "\n"; //创建队列 $q = new AMQPQueue($channel); //新建队列 //设置队列的名字 如果不存在则自动添加 $q->setName($q_name); $q->setFlags(AMQP_DURABLE | AMQP_AUTODELETE); //添加属性 DURABLE持久的 AUTODELEETE没有连接自动删除 echo "queue status:" . $q->declare(); //声明队列 echo "\n"; echo 'queue bind:' . $q->bind($e_name, $r_key);
/** * Get exchange * * @param string $name The exchange name * * @return \AMQPExchange */ protected function getExchange($name) { if (isset($this->exchanges[$name])) { return $this->exchanges[$name]; } $config = $this->getConfig('exchange', $name); if (null === $config) { throw new \InvalidArgumentException("Exchange definition '{$name}' doesn't exists."); } $connection = $this->getConnection($config['connection']); $this->exchanges[$name] = $exchange = new \AMQPExchange($connection['channel']); $exchange->setName(is_callable($config['name']) ? call_user_func($config['name']) : $config['name']); $exchange->setType(isset($config['type']) ? $config['type'] : 'topic'); $exchange->setFlags(Helper\Options::toFlags($config)); $exchange->declareExchange(); if (isset($config['bindings'])) { foreach ($config['bindings'] as $binding) { try { $this->getExchange($binding['exchange']); } catch (\InvalidArgumentException $e) { } $exchange->bind($binding['exchange'], $binding['routing_key'], isset($binding['arguments']) ? $binding['arguments'] : []); } } if (isset($config['arguments'])) { $exchange->setArguments($config['arguments']); } return $exchange; }
/** * 批量打入v3work_seo_pdrec数据,用于预热本企业推荐产品 */ function writeRMQ($exchangeName = 'v3cb', $routingKey, $data = '') { global $cfg; 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(); //发消息 $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; }
public function getExchange($name = 'default', $channelName = 'default') { $key = $channelName . '_' . $name; if (!isset($this->exchanges[$key])) { $channel = $this->getChannel($channelName); if (!isset($this->exchangesSettings[$name])) { throw new \LogicException('No settings for exchange ' . $name); } $settings = $this->exchangesSettings[$name]; $exchange = new \AMQPExchange($channel); $exchange->setName($settings['name']); if (strpos($settings['name'], 'amq.') !== 0) { $queueFlags = AMQP_NOPARAM; if ($settings['durable']) { $queueFlags |= AMQP_DURABLE; } if ($settings['passive']) { $queueFlags |= AMQP_PASSIVE; } if ($settings['autoDelete']) { $queueFlags |= AMQP_AUTODELETE; } $exchange->setType($this->getExchangeType($settings['type'])); $exchange->setFlags($queueFlags); $exchange->declareExchange(); } $this->exchanges[$key] = $exchange; //Warning place possible circular links if ($settings['to_exchanges'] !== null) { foreach ($settings['to_exchanges'] as $binExchangeName => $routingKeys) { $bindExchange = $this->getExchange($binExchangeName, $channelName); foreach ($routingKeys as $routingKey) { $bindExchange->bind($exchange->getName(), $routingKey); } } } } return $this->exchanges[$key]; }
/** * @param AMQPConnection $AMQPConnection * @param $exchangeName * @param $exchangeType * @return AMQPExchange * @throws AMQPConnectionException * @throws AMQPExchangeException * @throws Exception */ protected function createAMQPExchange($AMQPConnection, $exchangeName, $exchangeType) { $channel = new AMQPChannel($AMQPConnection); $exchange = new AMQPExchange($channel); $exchange->setName($exchangeName); $exchange->setType($exchangeType); $exchange->setFlags(AMQP_DURABLE); // Since php_amqp 1.2.0: deprecate AMQPExchange::declare() in favor of AMQPExchange::declareExchange() $declareMethodName = method_exists($exchange, 'declareExchange') ? 'declareExchange' : 'declare'; if (!$exchange->{$declareMethodName}()) { throw new Exception('Cannot declare exchange'); } return $exchange; }
$container->bind('AMQPConnection', function ($container) { $connection = new AMQPConnection($container['config']->get('parsley.amqpy.credentials', [])); $connection->connect(); return $connection; }, true); $container->bind('AMQPChannel', function ($container) { $ch = new AMQPChannel($container->make('AMQPConnection')); $qos = $container['config']->get('parsley.amqpy.qos'); if ($qos != null) { $ch->setPrefetchCount($qos); } return $ch; }, true); $container->bind('AMQPExchange', function ($container) { /** @var AMQPExchange $exchange */ $exchange = new AMQPExchange($container->make('AMQPChannel')); $exchange->setName($container['config']->get('parsley.amqpy.exchange', 'parsley')); $exchange->setType($container['config']->get('parsley.amqpy.exchange_type', AMQP_EX_TYPE_TOPIC)); $exchange->declareExchange(); return $exchange; }, true); $container->bind('AMQPQueue', function ($container) { /** @var AMQPQueue $queue */ $queue = new AMQPQueue($container->make('AMQPChannel')); /** @var \Illuminate\Config\Repository $config */ $config = $container['config']; $name = $config->get('parsley.amqpy.queue', 'parsley') . '.' . $config->get('parsley.priority', 'default'); $queue->setName($name); $queue->declareQueue(); return $queue; }, true);
<?php // print_r( get_loaded_extensions() ); $cnn = new AMQPConnection(); echo "------ connect ... --------\n"; $ret = $cnn->connect(); echo "------ connect Ok --------\n"; $ch = new AMQPChannel($cnn); $ex = new AMQPExchange($ch); $q = new AMQPQueue($ch); echo "------ exchange Ok --------\n"; // $cnn->disconnect(AMQP_NOPARAM); $ex->setName('test_e'); $ex->setType(AMQP::TYPE_DIRECT); $ex->declare(); echo "------ declare Ok --------\n"; $q->setName('test_q'); $q->declare(); $res = $ex->bind("test_q", "kkk"); echo "------ binding Ok --------\n"; var_dump($res); $cnn->disconnect();
/** * @param \AMQPQueue $queue * * @return \AMQPExchange */ protected function getExchangeForQueue(\AMQPQueue $queue) { $amqpExchange = new \AMQPExchange($this->channel); $queueOptions = $this->getOptionsForQueue($queue->getName()); // determine exchange to use if (array_key_exists('exchange', $queueOptions) && trim($queueOptions['exchange'])) { $amqpExchange->setName((string) $queueOptions['exchange']); } $options = $this->getOptionsForExchange($amqpExchange->getName()); $flags = $this->getFlagsFromOptions(['durable', 'passive'], $options); $amqpExchange->setFlags($flags); $amqpExchange->setType(isset($options['type']) ? $options['type'] : AMQP_EX_TYPE_DIRECT); return $amqpExchange; }
/** * Returns exchange * Exchange types: direct, topic, headers and fanout. * AMQP_EX_TYPE_DIRECT AMQP_EX_TYPE_FANOUT AMQP_EX_TYPE_TOPIC AMQP_EX_TYPE_HEADER * AMQP_DURABLE Durable exchanges will survive a broker restart, * AMQP_PASSIVE Passive exchanges will not be redeclared, but the broker will throw * an error if the exchange does not exist. * * @return AMQPExchange * @throws AMQPChannelException * @throws AMQPExchangeException */ public function exchange($flags = null, $name = null, $type = null) { $exchange = new \AMQPExchange($this->channel()); $flags and $exchange->setFlags($flags); $type and $exchange->setType($type); $name and $exchange->setName($name); return $exchange; }
protected function getAMQPExchange() { if (is_null(self::$_AMQPExchange)) { $channel = new AMQPChannel($this->getAMQPConnection()); $exchange = new AMQPExchange($channel); $exchange->setName($this->getExchangeName()); $exchange->setType($this->getExchangeType()); $exchange->setFlags(AMQP_DURABLE); $exchange->declare(); $queue = new AMQPQueue($channel); $queue->setName($this->getQueueName()); $queue->setFlags(AMQP_DURABLE); $queue->setArgument('x-ha-policy', 'all'); $queue->declare(); $queue->bind($this->getExchangeName(), $this->getRoutingKey()); self::$_AMQPExchange = $exchange; } return self::$_AMQPExchange; }
$res = $rabbit->connect(); printf("--------- %d ---------\n", __LINE__); var_dump($res); if (!$res) { die("connection false\n"); } printf("--------- %d ---------\n", __LINE__); $testChannel = new AMQPChannel($rabbit); printf("--------- %d ---- Queue-----\n", __LINE__); $q2 = new AMQPQueue($testChannel); printf("--------- %d ---- Exchange -----\n", __LINE__); $testExchange = new AMQPExchange($testChannel); printf("--------- %d ---------\n", __LINE__); $testExchange->setName('test_e2'); // $testExchange->delete(); $testExchange->setType('direct'); printf("--------- %d ---------\n", __LINE__); $testExchange->declare(); printf("--------- %d ---------\n", __LINE__); // unset($testChannel); // printf("--------- %d ---------\n", __LINE__); // $testChannel = new AMQPChannel($rabbit); printf("--------- %d ---- Queue-----\n", __LINE__); $q = new AMQPQueue($testChannel); printf("--------- %d ---------\n", __LINE__); $q->setName('test_q'); printf("--------- %d ---------\n", __LINE__); $q->declare(); printf("--------- %d ---------\n", __LINE__); $q->bind('test_e', 'scan'); printf("--------- %d ---------\n", __LINE__);
<?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();
/** * @param $exchangeType * * @return bool */ public function setType($exchangeType) { return $this->rawExchange->setType($exchangeType); }