@@ 7-43 (lines=37) @@ | ||
4 | ||
5 | use Ccovey\RabbitMQ\Connection\Connection; |
|
6 | ||
7 | class ExchangeDeclarer |
|
8 | { |
|
9 | /** |
|
10 | * @var array |
|
11 | */ |
|
12 | private $declaredExchanges = []; |
|
13 | ||
14 | /** |
|
15 | * @var Connection |
|
16 | */ |
|
17 | private $connection; |
|
18 | ||
19 | /** |
|
20 | * @var ChannelInterface |
|
21 | */ |
|
22 | private $channel; |
|
23 | ||
24 | public function __construct(Connection $connection, string $channelId = '') |
|
25 | { |
|
26 | $this->connection = $connection; |
|
27 | $this->channel = $this->connection->getChannel($channelId); |
|
28 | } |
|
29 | ||
30 | /* |
|
31 | * This method will be run each time we attempt to queue a message. |
|
32 | * We will cache locally which exchanges we have already declared. |
|
33 | * Declaring an exchange on each iteration of a worker consuming from |
|
34 | * a queue is really slow. |
|
35 | */ |
|
36 | public function declareExchange(Exchange $exchange) |
|
37 | { |
|
38 | if (!in_array($exchange, $this->declaredExchanges)) { |
|
39 | $this->channel->declareExchange($exchange); |
|
40 | $this->declaredExchanges[] = $exchange; |
|
41 | } |
|
42 | } |
|
43 | } |
|
44 |
@@ 7-43 (lines=37) @@ | ||
4 | ||
5 | use Ccovey\RabbitMQ\Connection\Connection; |
|
6 | ||
7 | class QueueDeclarer |
|
8 | { |
|
9 | /** |
|
10 | * @var array |
|
11 | */ |
|
12 | private $declaredQueues = []; |
|
13 | ||
14 | /** |
|
15 | * @var Connection |
|
16 | */ |
|
17 | private $connection; |
|
18 | ||
19 | /** |
|
20 | * @var ChannelInterface |
|
21 | */ |
|
22 | private $channel; |
|
23 | ||
24 | public function __construct(Connection $connection, string $channelId = '') |
|
25 | { |
|
26 | $this->connection = $connection; |
|
27 | $this->channel = $this->connection->getChannel($channelId); |
|
28 | } |
|
29 | ||
30 | /* |
|
31 | * This method will be run each time we attempt to queue a message. |
|
32 | * We will cache locally which queues we have already declared. |
|
33 | * Declaring a queue on each iteration of a worker consuming from |
|
34 | * a queue is really slow. |
|
35 | */ |
|
36 | public function declareQueue(Queue $queue) |
|
37 | { |
|
38 | if (!in_array($queue, $this->declaredQueues)) { |
|
39 | $this->channel->declareQueue($queue); |
|
40 | $this->declaredQueues[] = $queue; |
|
41 | } |
|
42 | } |
|
43 | } |
|
44 |