Skip to content

Commit 0182f8e

Browse files
authored
Merge pull request #365 from shustrik/master
Add shutdown method. When called kernel shutdown, we must close opene…
2 parents 01258ee + 95da24c commit 0182f8e

File tree

2 files changed

+47
-26
lines changed

2 files changed

+47
-26
lines changed

OldSoundRabbitMqBundle.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,17 @@ public function build(ContainerBuilder $container)
1616
$container->addCompilerPass(new RegisterPartsPass());
1717
$container->addCompilerPass(new InjectEventDispatcherPass());
1818
}
19+
20+
/**
21+
* {@inheritDoc}
22+
*/
23+
public function shutdown()
24+
{
25+
parent::shutdown();
26+
$partHolder = $this->container->get('old_sound_rabbit_mq.parts_holder');
27+
$connections = $partHolder->getParts("old_sound_rabbit_mq.base_amqp");
28+
foreach ($connections as $connection) {
29+
$connection->close();
30+
}
31+
}
1932
}

RabbitMq/BaseAmqp.php

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
namespace OldSound\RabbitMqBundle\RabbitMq;
4+
45
use OldSound\RabbitMqBundle\Event\AMQPEvent;
56
use PhpAmqpLib\Channel\AMQPChannel;
67
use PhpAmqpLib\Connection\AbstractConnection;
@@ -74,6 +75,11 @@ public function __construct(AbstractConnection $conn, AMQPChannel $ch = null, $c
7475
}
7576

7677
public function __destruct()
78+
{
79+
$this->close();
80+
}
81+
82+
public function close()
7783
{
7884
if ($this->ch) {
7985
try {
@@ -115,6 +121,7 @@ public function getChannel()
115121

116122
/**
117123
* @param AMQPChannel $ch
124+
*
118125
* @return void
119126
*/
120127
public function setChannel(AMQPChannel $ch)
@@ -158,6 +165,33 @@ public function setRoutingKey($routingKey)
158165
$this->routingKey = $routingKey;
159166
}
160167

168+
public function setupFabric()
169+
{
170+
if (!$this->exchangeDeclared) {
171+
$this->exchangeDeclare();
172+
}
173+
174+
if (!$this->queueDeclared) {
175+
$this->queueDeclare();
176+
}
177+
}
178+
179+
/**
180+
* disables the automatic SetupFabric when using a consumer or producer
181+
*/
182+
public function disableAutoSetupFabric()
183+
{
184+
$this->autoSetupFabric = false;
185+
}
186+
187+
/**
188+
* @param LoggerInterface $logger
189+
*/
190+
public function setLogger($logger)
191+
{
192+
$this->logger = $logger;
193+
}
194+
161195
/**
162196
* Declares exchange
163197
*/
@@ -217,32 +251,6 @@ protected function queueBind($queue, $exchange, $routing_key)
217251
}
218252
}
219253

220-
public function setupFabric()
221-
{
222-
if (!$this->exchangeDeclared) {
223-
$this->exchangeDeclare();
224-
}
225-
226-
if (!$this->queueDeclared) {
227-
$this->queueDeclare();
228-
}
229-
}
230-
231-
/**
232-
* disables the automatic SetupFabric when using a consumer or producer
233-
*/
234-
public function disableAutoSetupFabric() {
235-
$this->autoSetupFabric = false;
236-
}
237-
238-
/**
239-
* @param LoggerInterface $logger
240-
*/
241-
public function setLogger($logger)
242-
{
243-
$this->logger = $logger;
244-
}
245-
246254
/**
247255
* @param EventDispatcherInterface $eventDispatcher
248256
*

0 commit comments

Comments
 (0)