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 Run() { // Declare a new exchange $ex = new AMQPExchange($this->cnn); $ex->declare('game', AMQP_EX_TYPE_FANOUT); // Create a new queue $q1 = new AMQPQueue($this->cnn); $q1->declare('queue1'); $q2 = new AMQPQueue($this->cnn); $q2->declare('queue2'); // Bind it on the exchange to routing.key //$ex->bind('queue1', 'broadcast=true,target=queue1,x-match=any'); $ex->bind('queue1', ''); $ex->bind('queue2', ''); $msgBody = 'hello'; // Publish a message to the exchange with a routing key $ex->publish($msgBody, 'foo'); // Read from the queue $msg = $q1->consume(); $this->AssertEquals(count($msg), 1); $this->AssertEquals($msg[0]['message_body'], $msgBody, 'message not equal'); // Read from the queue $msg = $q2->consume(); $this->AssertEquals(count($msg), 1); $this->AssertEquals($msg[0]['message_body'], $msgBody, 'message not equal'); $this->AddMessage(var_export($msg[0], true)); }
public function Run() { // Declare a new exchange $ex = new AMQPExchange($this->cnn); $ex->declare('game', AMQP_EX_TYPE_TOPIC); // Create a new queue $q1 = new AMQPQueue($this->cnn); $q1->declare('queue1'); $q1->purge('queue1'); $q2 = new AMQPQueue($this->cnn); $q2->declare('queue2'); $q1->purge('queue2'); $q3 = new AMQPQueue($this->cnn); $q3->declare('queue3'); $q3->purge('queue3'); $options = array('min' => 0, 'max' => 10, 'ack' => true); // Bind it on the exchange to routing.key $ex->bind('queue1', 'game1.#'); $ex->bind('queue2', 'game1.#'); $ex->bind('queue3', 'game1.#'); $ex->bind('queue3', 'queue3.#'); $msgbody1 = 'hello'; // Publish a message to the exchange with a routing key $result = $ex->publish($msgbody1, 'game1.msg'); $this->AssertEquals($result, TRUE, 'publish message failed'); $msgbody2 = 'world'; // Publish a message to the exchange with a routing key $result = $ex->publish($msgbody2, 'game1.msg'); $this->AssertEquals($result, TRUE, 'publish message failed'); $msgbody3 = 'hello player3'; // Publish a message to the exchange with a routing key $result = $ex->publish($msgbody3, 'queue3.command'); $this->AssertEquals($result, TRUE, 'publish message failed'); // Read from the queue $msg = $q1->consume($options); $this->AddMessage(var_export($msg, true)); $this->AssertEquals(count($msg), 2); $this->AssertEquals($msg[0]['message_body'], $msgbody1, 'message not equal'); $this->AssertEquals($msg[1]['message_body'], $msgbody2, 'message not equal'); // Read from the queue $msg = $q2->consume($options); $this->AssertEquals(count($msg), 2); $this->AssertEquals($msg[0]['message_body'], $msgbody1, 'message not equal'); $this->AssertEquals($msg[1]['message_body'], $msgbody2, 'message not equal'); // Read from the queue $msg = $q3->consume($options); $this->AddMessage(var_export($msg, true)); $this->AssertEquals(count($msg), 3); $this->AssertEquals($msg[0]['message_body'], $msgbody1, 'message not equal'); $this->AssertEquals($msg[1]['message_body'], $msgbody2, 'message not equal'); $this->AssertEquals($msg[2]['message_body'], $msgbody3, 'message not equal'); $msg = $q3->consume($options); $this->AssertEquals(count($msg), 0); }
public function Run() { // Declare a new exchange $ex = new AMQPExchange($this->cnn); $ex->declare('game', AMQP_EX_TYPE_HEADER); // Create a new queue $q1 = new AMQPQueue($this->cnn); $q1->declare('queue1'); $q1->purge('queue1'); $q2 = new AMQPQueue($this->cnn); $q2->declare('queue2'); $q1->purge('queue2'); $q3 = new AMQPQueue($this->cnn); $q3->declare('queue3'); $q3->purge('queue3'); $options = array('min' => 0, 'max' => 10, 'ack' => true); // Bind it on the exchange to routing.key $ex->bind('queue1', 'broadcast=1,target=1,x-match=any'); $ex->bind('queue2', 'broadcast=1,target=2,x-match=any'); $ex->bind('queue3', 'broadcast=1,target=3,x-match=any'); $msgbody1 = 'hello'; // Publish a message to the exchange with a routing key $result = $ex->publish($msgbody1, NULL, AMQP_IMMEDIATE, array('headers' => array('broadcast' => 1))); $this->AssertEquals($result, TRUE, 'publish message failed'); $msgbody2 = 'world'; // Publish a message to the exchange with a routing key $result = $ex->publish($msgbody2, NULL, AMQP_IMMEDIATE, array('headers' => array('broadcast' => 1))); $this->AssertEquals($result, TRUE, 'publish message failed'); $msgbody3 = 'queue3'; // Publish a message to the exchange with a routing key $result = $ex->publish($msgbody1, NULL, AMQP_IMMEDIATE, array('headers' => array('target' => 3))); $this->AssertEquals($result, TRUE, 'publish message failed'); // Read from the queue $msg = $q1->consume($options); $this->AddMessage(var_export($msg, true)); $this->AssertEquals(count($msg), 2); //$this->AssertEquals($msg[0]['message_body'], $msgbody1, 'message not equal'); //$this->AssertEquals($msg[1]['message_body'], $msgbody2, 'message not equal'); // Read from the queue $msg = $q2->consume($options); $this->AssertEquals(count($msg), 2); //$this->AssertEquals($msg[0]['message_body'], $msgbody1, 'message not equal'); //$this->AssertEquals($msg[1]['message_body'], $msgbody2, 'message not equal'); // Read from the queue $msg = $q3->consume($options); $this->AssertEquals(count($msg), 3); //$this->AssertEquals($msg[0]['message_body'], $msgbody1, 'message not equal'); //$this->AssertEquals($msg[1]['message_body'], $msgbody2, 'message not equal'); //$this->AssertEquals($msg[2]['message_body'], $msgbody3, 'message not equal'); }
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"); } }
/** * 批量打入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; }
<?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();
$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); //将队列绑定到routingKey echo "\n"; $channel->startTransaction();
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; }
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__); $rabbit->disconnect();
/** * 批量打入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; }
<?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();
/** * Declares an exchange on the broker * * available options: * * - type: string * - passive: boolean * - durable: boolean * - auto_delete: boolean * - internal: boolean * * @param string $name * @param array $options * * @return CakeAmqpBase */ public function declareExchange($name, $options = array()) { if (!$this->connected()) { throw new CakeException(__d('cake_amqp', 'Not connected to broker')); } if (isset($this->_exchanges[$name])) { throw new CakeException(__d('cake_amqp', 'Exchange already exists: %s', $name)); } $defaults = array('type' => 'direct', 'passive' => false, 'durable' => false, 'auto_delete' => true, 'internal' => false); $options = array_merge($defaults, $options); if (!array_key_exists($options['type'], $this->_exchangeTypes)) { throw new CakeException(__d('cake_amqp', 'Exchange type not supported: %s', $options['type'])); } $exchange = new AMQPExchange($this->_channel); $exchange->setName($name); $exchange->setType($this->_exchangeTypes[$options['type']]); $exchange->setFlags($this->__getExchangeFlags($options)); $exchange->declare(); $this->_exchanges[$name] = $exchange; return $this; }