/** * @return PromiseInterface */ public function connect() { if (null === $this->connection) { $client = new Client($this->loop, $this->options); $this->connection = $client->connect()->then(function (Client $client) { return $client->channel(); }, function (Exception $exception) { $this->loop->stop(); throw new AsyncMessagingException(sprintf('Could not connect to rabbitmq: %s on line %s in file %s', $exception->getMessage(), $exception->getLine(), $exception->getFile()), 0, $exception); }); } return $this->connection; }
/** * Open a new channel and attribute it to given queues or exchanges * @param RabbitQueue[]|RabbitExchange[] $bind * @return Observable\AnonymousObservable */ public function channel($bind = []) { if (!is_array($bind)) { $bind = func_get_args(); } return $this->bunny->connect()->flatMap(function () { return $this->bunny->channel(); })->map(function (Channel $channel) use($bind) { foreach ($bind as $obj) { $obj->setChannel($channel); } return $channel; }); }
public function testConflictingQueueDeclareRejects() { $loop = Factory::create(); $loop->addTimer(5, function () { throw new TimeoutException(); }); $client = new Client($loop); $client->connect()->then(function (Client $client) { return $client->channel(); })->then(function (Channel $ch) { return Promise\all([$ch->queueDeclare("conflict", false, false), $ch->queueDeclare("conflict", false, true)]); })->then(function () use($loop) { $this->fail("Promise should get rejected"); $loop->stop(); }, function (\Exception $e) use($loop) { $this->assertInstanceOf("Bunny\\Exception\\ClientException", $e); $loop->stop(); }); $loop->run(); }
<?php namespace Bunny; use Bunny\Async\Client; use React\Promise; require_once __DIR__ . "/../vendor/autoload.php"; $c = new Client(); $c->connect()->then(function (Client $c) { return $c->channel(); })->then(function (Channel $ch) { return Promise\all([$ch, $ch->queueDeclare("bench_queue"), $ch->exchangeDeclare("bench_exchange"), $ch->queueBind("bench_queue", "bench_exchange")]); })->then(function ($r) { /** @var Channel $ch */ $ch = $r[0]; $t = null; $count = 0; return $ch->consume(function (Message $msg, Channel $ch, Client $c) use(&$t, &$count) { if ($t === null) { $t = microtime(true); } if ($msg->content === "quit") { printf("Pid: %s, Count: %s, Time: %.4f\n", getmypid(), $count, microtime(true) - $t); $c->disconnect()->then(function () use($c) { $c->stop(); }); } else { ++$count; } }, "bench_queue", "", false, true); });
public function testReturn() { $loop = Factory::create(); $loop->addTimer(1, function () { throw new TimeoutException(); }); $client = new Client($loop); /** @var Channel $channel */ $channel = null; /** @var Message $returnedMessage */ $returnedMessage = null; /** @var MethodBasicReturnFrame $returnedFrame */ $returnedFrame = null; $client->connect()->then(function (Client $client) { return $client->channel(); })->then(function (Channel $ch) use($loop, &$channel, &$returnedMessage, &$returnedFrame) { $channel = $ch; $channel->addReturnListener(function (Message $message, MethodBasicReturnFrame $frame) use($loop, &$returnedMessage, &$returnedFrame) { $returnedMessage = $message; $returnedFrame = $frame; $loop->stop(); }); return $channel->publish("xxx", [], "", "404", true); }); $loop->run(); $this->assertNotNull($returnedMessage); $this->assertInstanceOf("Bunny\\Message", $returnedMessage); $this->assertEquals("xxx", $returnedMessage->content); $this->assertEquals("", $returnedMessage->exchange); $this->assertEquals("404", $returnedMessage->routingKey); }
protected function execute(InputInterface $input, OutputInterface $output) { $loop = Factory::create(); $nope = function (\Exception $e) use($output) { $output->writeln("<bg=red>{$e->getMessage()}</>"); }; $c = new Client($loop, ['host' => $input->getOption('host'), 'port' => (int) $input->getOption('port'), 'user' => $input->getOption('user'), 'pass' => $input->getOption('pass'), 'vhost' => $input->getOption('vhost')]); $conn = $c->connect(); $conn->then(function () use($output) { $output->writeln('<bg=green>Connected...</>'); }); if (null !== ($destination = $input->getOption('pipe'))) { $conn->then(function (Client $c) { return $c->channel(); })->then(function (Channel $ch) use($loop, $destination, $input) { $stdin = new Stream(fopen('php://stdin', 'r+'), $loop); $bindings = new Bindings($destination); $dataParser = function ($data) use($input) { return '' !== $input->getOption('delimiter') ? explode($input->getOption('delimiter'), trim($data)) : [trim($data)]; }; $once = $input->getOption('once') ? function () use($ch) { $ch->close()->then(function () use($ch) { $ch->getClient()->disconnect(); }); } : function () { }; $stdin->on('data', function ($data) use($ch, $bindings, $dataParser, $once) { Promise\all(array_map(function ($message) use($ch, $bindings) { return $ch->publish($message, [], $bindings->exchange, $bindings->routingKey); }, array_filter($dataParser($data), function ($message) { return '' !== $message; })))->then($once); }); }, $nope); if ($this->defaultBinding === $input->getOption('bind')) { $input->setOption('bind', []); } } $consume = function (Message $msg) use($input, $output) { $this->consume($msg, $input, $output); }; if ([] !== $input->getOption('consume')) { $conn->then(function (Client $c) { return $c->channel(); })->then(function (Channel $ch) use($input, $consume) { return Promise\all(array_map(function ($queue) use($ch, $consume) { return $ch->consume($consume, $queue, '', false, true, true); }, $input->getOption('consume'))); }, $nope); if ($this->defaultBinding === $input->getOption('bind')) { $input->setOption('bind', []); } } if ([] !== $input->getOption('bind')) { $conn->then(function (Client $c) { return $c->channel(); })->then(function (Channel $ch) { return Promise\all([$ch, $ch->queueDeclare('', false, false, true, true)]); })->then(function ($r) use($input) { list($ch, $qr) = $r; return Promise\all(array_merge([$ch, $qr], array_map(function ($exKey) use($ch, $qr) { $bindings = new Bindings($exKey); return $ch->queueBind($qr->queue, $bindings->exchange, $bindings->routingKey, false, $bindings->headers); }, $input->getOption('bind')))); })->then(function ($r) use($consume) { return $r[0]->consume($consume, $r[1]->queue, '', false, true, true); }, $nope); } try { $c->run(); } catch (\Exception $e) { $nope($e); } }