public function it_should_reuse_existing_exchange_when_its_same_channel(ConnectionRegistry $connectionRegistry, Connection $connection, ChannelFactory $channelFactory, Channel $channel, Exchange $exchange, QueueFactory $queueFactory, Queue $queue, ExchangeRegistry $exchangeRegistry, QueueRegistry $queueRegistry) { $queueRegistry->hasQueue('queue', $channel, $exchange)->willReturn(false); $connectionRegistry->getConnection('default')->willReturn($connection); $channelFactory->createFromConnection($connection, Argument::type(Identity::class))->willReturn($channel); $exchangeRegistry->hasExchange('exchange', $channel)->willReturn(true); $exchangeRegistry->getExchange('exchange', $channel)->willReturn($exchange); $queueFactory->createNamed('queue', $channel, $exchange)->willReturn($queue); $consumer = $this->createFromConnection($connection, 'exchange', 'queue'); $consumer->shouldBeAnInstanceOf(Consumer::class); }
/** * @param Connection $connection * @param string $exchangeName * @param string $queueName * * @return Consumer */ public function createFromConnection(Connection $connection, $exchangeName, $queueName) { $identity = new Identity(); $channel = $this->channelFactory->createFromConnection($connection, $identity); if (!$this->exchangeRegistry->hasExchange($exchangeName, $channel)) { $exchange = $this->exchangeFactory->createNamed($exchangeName, $channel); } else { $exchange = $this->exchangeRegistry->getExchange($exchangeName, $channel); } if (!$this->queueRegistry->hasQueue($queueName, $channel, $exchange)) { $queue = $this->queueFactory->createNamed($queueName, $channel, $exchange); } else { $queue = $this->queueRegistry->getQueue($queueName, $channel, $exchange); } $consumer = new Consumer($identity, $connection, $channel, $exchange, $queue); return $consumer; }
$connectionFactory = new ConnectionFactory($connectionRegistry, ['default' => ['host' => 'rabbitmq', 'port' => 5672, 'login' => 'admin', 'password' => 'admin', 'vhost' => '/', 'read_timeout' => 0.5, 'write_timeout' => 0.5, 'conntect_timeout' => 0.5]]); $channelFactory = new ChannelFactory($connectionRegistry, $channelRegistry, $channelFactory); $connection = $connectionFactory->createNamed('default'); $contextRegistry->addExchangeContext('exchange_a', new Context(\AMQP_DURABLE)); $contextRegistry->addExchangeContext('exchange_b', new Context(\AMQP_DURABLE)); $contextRegistry->addExchangeContext('exchange_c', new Context(\AMQP_DURABLE)); $contextRegistry->addQueueContext('queue_a_foo', new \Evaneos\Hector\Queue\Context('a.foo')); $contextRegistry->addQueueContext('queue_a_bar', new \Evaneos\Hector\Queue\Context('a.bar')); $contextRegistry->addQueueContext('queue_b', new \Evaneos\Hector\Queue\Context()); $exchangeFactory = new ExchangeFactory($contextRegistry, $exchangeRegistry); // A channel is a virtual connection to insulate work process inside her. // Our worker will process across 3 queues from different exchange, all communication between the broker and worker will be through this channel $channel = $channelFactory->createFromConnectionName('default', new Identity()); $exchangeA = $exchangeFactory->createNamed('exchange_a', $channel); $exchangeB = $exchangeFactory->createNamed('exchange_b', $channel); $queueFactory = new QueueFactory($contextRegistry, $queueRegistry); /** * exchange_a is bind to queue_a_foo and queue_a_bar and communicate over the same channel * * If you have a worker who consume from multiple queue via get() with an event loop, you should aggregate in only one channel */ $queueAFoo = $queueFactory->createNamed('queue_a_foo', $channel, $exchangeA); $queueABar = $queueFactory->createNamed('queue_a_bar', $channel, $exchangeA); $queueB = $queueFactory->createNamed('queue_b', $channel, $exchangeB); $consumerFactory = new ConsumerFactory($channelFactory, $queueFactory, $exchangeFactory, $connectionRegistry, $exchangeRegistry, $queueRegistry); //all of these consumer work through the same channel $consumerAFoo = $consumerFactory->create('default', 'exchange_a', 'queue_a_foo'); $consumerABar = $consumerFactory->create('default', 'exchange_a', 'queue_a_bar'); $consumerB = $consumerFactory->create('default', 'exchange_b', 'queue_b'); //$consumerB->getQueue()->getWrappedQueue()->get()) //$consumerB->getQueue()->getWrappedQueue()->consume())