/** * @return bool */ public function rollbackTransaction() { if (!$this->isInitialized()) { $this->initialize(); } return $this->channel->rollbackTransaction(); }
/** * Test fail execute * * @expectedException \InvalidArgumentException * @expectedExceptionMessage Some exception */ public function testExecuteFail() { $this->channel->expects($this->at(0))->method('startTransaction'); $this->channel->expects($this->at(1))->method('rollbackTransaction'); $transactional = new AmqpTransactional($this->channel); $transactional->execute(function () { throw new \InvalidArgumentException('Some exception'); }); }
public function it_should_rollback_if_transaction_failed(\AMQPChannel $channel) { $channel->startTransaction()->shouldBeCalled(); $channel->commitTransaction()->shouldNotBeCalled(); $channel->rollbackTransaction()->shouldBeCalled(); $exception = new \Exception('Whhooops'); $process = function (Channel $channel) use($exception) { throw $exception; }; $this->shouldThrow(new HectorException('Transaction failed', 255, $exception))->during('transaction', [$process]); }
/** * @return \AMQPChannel */ protected function getChannel() { if ($this->channel === null) { $this->channel = new \AMQPChannel($this->getConnection()); $this->channel->setPrefetchCount(1); } return $this->channel; }
/** * Initialize if not yet initialized and return channel to AMQP broker * * @return \AMQPChannel * * @throws ConnectionException */ private function getChannel() { if ($this->channel && $this->channel->isConnected()) { return $this->channel; } try { $this->channel = null; if ($this->connection->isConnected()) { $this->connection->disconnect(); } $this->connection->connect(); $channel = new \AMQPChannel($this->connection); $channel->setPrefetchCount(self::DEFAULT_PREFETCH_COUNT); } catch (\AMQPException $e) { throw new ConnectionException(sprintf('AMQP connection error (%s:%s): %s', $this->connection->getHost(), $this->connection->getPort(), $e->getMessage())); } return $this->channel = $channel; }
public function tearDown() { if ($this->channel) { $this->channel->queue_delete($this->queueName . '1'); $this->channel->close(); } if ($this->connection) { $this->connection->close(); } }
public function __destruct() { $this->channel->close(); unset($this->channel); $this->connection->close(); unset($this->connection); unset($this->replyQueueName); unset($this->consumerTag); unset($this->outputMessages); }
/** * @inheritdoc */ public function waitForBasicReturn(float $timeout = 0.0) { try { $this->channel->waitForBasicReturn($timeout); } catch (\AMQPChannelException $e) { throw ChannelException::fromAmqpExtension($e); } catch (\AMQPQueueException $e) { throw ChannelException::fromAmqpExtension($e); } }
/** * @inheritdoc */ public function rollbackTransaction() { try { return $this->delegate->rollbackTransaction(); } catch (\AMQPChannelException $e) { throw new ChannelException($e->getMessage(), $e->getCode(), $e); } catch (\AMQPConnectionException $e) { throw new ConnectionException($e->getMessage(), $e->getCode(), $e); } }
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.!"); } }
public function close() { if ($this->closed != true) { if ($this->channel != null) { $this->channel->close(); $this->channel = null; } if ($this->connection != null) { $this->connection->close(); $this->connection = null; } $this->closed = true; } }
/** * Initialize connection, queue and exchange * * @return void */ protected function initConnection() { // Only initialize once if ($this->connection) { return; } $this->connection = new AMQPStreamConnection($this->connectionConfig['host'], $this->connectionConfig['port'], $this->connectionConfig['user'], $this->connectionConfig['password'], $this->connectionConfig['vhost']); $this->channel = $this->connection->channel(); if ($this->declaredQueue) { $this->channel->queue_declare($this->queueConfig['name'], $this->queueConfig['passive'], $this->queueConfig['durable'], $this->queueConfig['exclusive'], $this->queueConfig['autoDelete']); } $this->channel->exchange_declare($this->exchangeConfig['name'], $this->exchangeConfig['type'], $this->exchangeConfig['passive'], $this->exchangeConfig['durable'], $this->exchangeConfig['autoDelete']); if ($this->declaredQueue) { $this->channel->queue_bind($this->queueConfig['name'], $this->exchangeConfig['name'], $this->queueConfig['routingKey']); } $this->persistentDelivery = $this->exchangeConfig['durable'] || $this->queueConfig['durable']; }
/** * Fetches a channel object identified by the numeric channel_id, or * create that object if it doesn't already exist. * * @param int $channel_id * @return AMQPChannel */ public function channel(int $channel_id = null) { // garbage collect channels foreach ($this->channels as $i => $channel) { if ($channel->isClosed()) { unset($this->channels[$i]); } } // if (isset($this->channels[$channel_id])) { (yield $this->channels[$channel_id]); } else { $channel_id = $channel_id ? $channel_id : $this->getFreeChannelID(); $ch = new AMQPChannel($this, $channel_id); $this->channels[$channel_id] = $ch; (yield $ch->open()); (yield $ch); } }
/** * Delete AMQP Exchange * @param $name */ public function exchangeDelete($name) { $this->channel->exchange_delete($name); }
<?php /** * Created by PhpStorm. * User: seyfer * Date: 11/23/15 */ //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); $channel->setPrefetchCount(1); $routing_key = 'task_queue'; try { $queue = new AMQPQueue($channel); $queue->setName($routing_key); $queue->setFlags(AMQP_DURABLE); $queue->declareQueue(); } catch (Exception $ex) { print_r($ex); } //Read from stdin $message = implode(' ', array_slice($argv, 1)); if (empty($message)) { $message = "Hello World!"; } $exchange = new AMQPExchange($channel);
/** * Declare Channel */ protected function setChannel() { $this->channel = new AMQPChannel($this->connection); $this->channel->setPrefetchCount(1); }
/** * Process event - send AMQ message * * @param array $event */ protected function processEvent(&$event){ $msg = new AMQPMessage(json_encode($event), array('content_type' => 'text/json')); $this->amqChannel->basic_publish($msg, $this->config['amqp']['exchange']); }
<?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();
public static function castChannel(\AMQPChannel $c, array $a, Stub $stub, $isNested) { $prefix = Caster::PREFIX_VIRTUAL; $a += array($prefix . 'isConnected' => $c->isConnected(), $prefix . 'channelId' => $c->getChannelId(), $prefix . 'prefetchSize' => $c->getPrefetchSize(), $prefix . 'prefetchCount' => $c->getPrefetchCount(), $prefix . 'connection' => $c->getConnection()); return $a; }
/** * @param bool $requeue * * @return $this */ public function basicRecover($requeue = true) { $this->rawChannel->basicRecover($requeue); return $this; }
<?php /** * PHP amqp(RabbitMQ) Demo-2 * @author yuansir <yuansir@live.cn/yuansir-web.com> */ $exchangeName = 'queues'; $queueName = 'task_queue'; $routeKey = 'task_queue'; $connection = new AMQPConnection(array('host' => '127.0.0.1', 'port' => '5672', 'vhost' => '/', 'login' => 'guest', 'password' => 'guest')); $connection->connect() or die("Cannot connect to the broker!\n"); $channel = new AMQPChannel($connection); $exchange = new AMQPExchange($channel); $exchange->setName($exchangeName); $exchange->setType(AMQP_EX_TYPE_DIRECT); $exchange->declare(); $queue = new AMQPQueue($channel); $queue->setName($queueName); $queue->setFlags(AMQP_DURABLE); $queue->declare(); $queue->bind($exchangeName, $routeKey); var_dump('[*] Waiting for messages. To exit press CTRL+C'); while (TRUE) { $queue->consume('callback'); $channel->qos(0, 5); } $connection->disconnect(); function callback($envelope, $queue) { $msg = $envelope->getBody(); var_dump(" [x] Received:" . $msg);
/** * Get connection * * @param string $name Connection name * * @return array Return connection and channel */ protected function getConnection($name) { if (isset($this->connections[$name])) { return $this->connections[$name]; } $config = $this->getConfig('connection', $name); if (null == $config) { throw new \InvalidArgumentException("Connection '{$name}' doesn't exists."); } $connection = new \AMQPConnection($config); $connection->connect(); $channel = new \AMQPChannel($connection); if (isset($config['prefetch_count'])) { $channel->setPrefetchCount($config['prefetch_count']); } return $this->connections[$name] = ['connection' => $connection, 'channel' => $channel]; }
/** * Get the AMQPConnection object in use * * @return AMQPConnection */ public function getConnection() { return $this->channel->getConnection(); }
/** * {@inheritDoc} */ public function rollback($key = null) { $this->channel->rollbackTransaction(); }
/** * @return mixed */ public function close() { $this->channel->getConnection()->disconnect(); }
echo "<pre>"; $conn_args = array("host" => 'localhost', 'port' => 5672, 'login' => 'guest', "password" => 'guest', 'vhost' => '/'); $conn = new AMQPConnection($conn_args); if ($conn->connect()) { echo "Success\r\n"; } else { echo "Fail\r\n"; } $e_name = 'test.fanout'; //交换机名 $q_name = 'q_test5'; //队列名称 $r_key = 'key_test1'; //消息 $message = json_encode(array("HelloWorld")); //创建channel $channel = new AMQPChannel($conn); $ex = new AMQPExchange($channel); $ex->setName($e_name); //创建exchange的名字 echo "\n"; $channel->startTransaction(); //开启事务处理 echo "send: " . $ex->publish($message); //将数据发送 $channel->commitTransaction(); //提交事务 $channel->rollbackTransaction(); //回滚事务 $conn->disconnect();
$container->bind('parsley.payload_builder', 'Parsley\\Helpers\\PayloadBuilder', true); // AMQPy part $container->bind('parsley.brokers.rabbitmq.publisher', 'AMQPy\\Publisher', true); $container->bind('parsley.brokers.rabbitmq.listener', 'AMQPy\\Listener', true); $container->bind('AMQPy\\Serializers\\SerializersPool', function ($container) { $serializers_pool = new \AMQPy\Serializers\SerializersPool(); $serializers_pool->register($container['config']->get('parsley.amqpy.serializers', [])); return $serializers_pool; }, true); $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) {