Skip to content

Commit 975a986

Browse files
author
Ville Mattila
committed
Producer without explicit exchange definition defaults to AMQP Default Exchange.
1 parent dc6e65b commit 975a986

File tree

5 files changed

+55
-15
lines changed

5 files changed

+55
-15
lines changed

DependencyInjection/Configuration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,6 @@ protected function getExchangeConfiguration()
207207
$node = new ArrayNodeDefinition('exchange_options');
208208

209209
return $node
210-
->isRequired()
211210
->children()
212211
->scalarNode('name')->isRequired()->end()
213212
->scalarNode('type')->isRequired()->end()
@@ -216,6 +215,7 @@ protected function getExchangeConfiguration()
216215
->booleanNode('auto_delete')->defaultValue(false)->end()
217216
->booleanNode('internal')->defaultValue(false)->end()
218217
->booleanNode('nowait')->defaultValue(false)->end()
218+
->booleanNode('declare')->defaultValue(true)->end()
219219
->variableNode('arguments')->defaultNull()->end()
220220
->scalarNode('ticket')->defaultNull()->end()
221221
->end()

DependencyInjection/OldSoundRabbitMqExtension.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,13 @@ protected function loadProducers()
9393
$definition = new Definition($producer['class']);
9494
$definition->addTag('old_sound_rabbit_mq.base_amqp');
9595
$definition->addTag('old_sound_rabbit_mq.producer');
96+
//this producer doesn't define an exchange -> using AMQP Default
97+
if (!isset($producer['exchange_options'])) {
98+
$producer['exchange_options']['name'] = '';
99+
$producer['exchange_options']['type'] = 'direct';
100+
$producer['exchange_options']['passive'] = true;
101+
$producer['exchange_options']['declare'] = false;
102+
}
96103
$definition->addMethodCall('setExchangeOptions', array($this->normalizeArgumentKeys($producer['exchange_options'])));
97104
//this producer doesn't define a queue
98105
if (!isset($producer['queue_options'])) {

RabbitMq/BaseAmqp.php

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ abstract class BaseAmqp
2323
'internal' => false,
2424
'nowait' => false,
2525
'arguments' => null,
26-
'ticket' => null
26+
'ticket' => null,
27+
'declare' => true,
2728
);
2829

2930
protected $queueOptions = array(
@@ -96,7 +97,7 @@ public function setChannel(AMQPChannel $ch)
9697
*/
9798
public function setExchangeOptions(array $options = array())
9899
{
99-
if (empty($options['name'])) {
100+
if (!isset($options['name'])) {
100101
throw new \InvalidArgumentException('You must provide an exchange name');
101102
}
102103

@@ -127,18 +128,20 @@ public function setRoutingKey($routingKey)
127128

128129
protected function exchangeDeclare()
129130
{
130-
$this->getChannel()->exchange_declare(
131-
$this->exchangeOptions['name'],
132-
$this->exchangeOptions['type'],
133-
$this->exchangeOptions['passive'],
134-
$this->exchangeOptions['durable'],
135-
$this->exchangeOptions['auto_delete'],
136-
$this->exchangeOptions['internal'],
137-
$this->exchangeOptions['nowait'],
138-
$this->exchangeOptions['arguments'],
139-
$this->exchangeOptions['ticket']);
140-
141-
$this->exchangeDeclared = true;
131+
if ($this->exchangeOptions['declare']) {
132+
$this->getChannel()->exchange_declare(
133+
$this->exchangeOptions['name'],
134+
$this->exchangeOptions['type'],
135+
$this->exchangeOptions['passive'],
136+
$this->exchangeOptions['durable'],
137+
$this->exchangeOptions['auto_delete'],
138+
$this->exchangeOptions['internal'],
139+
$this->exchangeOptions['nowait'],
140+
$this->exchangeOptions['arguments'],
141+
$this->exchangeOptions['ticket']);
142+
143+
$this->exchangeDeclared = true;
144+
}
142145
}
143146

144147
protected function queueDeclare()
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
old_sound_rabbit_mq:
2+
3+
connections:
4+
default:
5+
6+
producers:
7+
producer:
8+
connection: default

Tests/DependencyInjection/OldSoundRabbitMqExtensionTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ public function testFooProducerDefinition()
7575
'nowait' => true,
7676
'arguments' => null,
7777
'ticket' => null,
78+
'declare' => true,
7879
)
7980
)
8081
),
@@ -114,6 +115,7 @@ public function testDefaultProducerDefinition()
114115
'nowait' => false,
115116
'arguments' => null,
116117
'ticket' => null,
118+
'declare' => true,
117119
)
118120
)
119121
),
@@ -153,6 +155,7 @@ public function testFooConsumerDefinition()
153155
'nowait' => true,
154156
'arguments' => null,
155157
'ticket' => null,
158+
'declare' => true,
156159
)
157160
)
158161
),
@@ -204,6 +207,7 @@ public function testDefaultConsumerDefinition()
204207
'nowait' => false,
205208
'arguments' => null,
206209
'ticket' => null,
210+
'declare' => true,
207211
)
208212
)
209213
),
@@ -279,6 +283,7 @@ public function testMultipleConsumerDefinition()
279283
'nowait' => false,
280284
'arguments' => null,
281285
'ticket' => null,
286+
'declare' => true,
282287
)
283288
)
284289
),
@@ -343,6 +348,7 @@ public function testFooAnonConsumerDefinition()
343348
'nowait' => true,
344349
'arguments' => null,
345350
'ticket' => null,
351+
'declare' => true,
346352
)
347353
)
348354
),
@@ -378,6 +384,7 @@ public function testDefaultAnonConsumerDefinition()
378384
'nowait' => false,
379385
'arguments' => null,
380386
'ticket' => null,
387+
'declare' => true,
381388
)
382389
)
383390
),
@@ -499,6 +506,21 @@ public function testExchangeArgumentsAreArray()
499506
$this->assertEquals(array('name' => 'bar'), $options[0]['arguments']);
500507
}
501508

509+
public function testProducerWithoutExplicitExchangeOptionsConnectsToAMQPDefault()
510+
{
511+
$container = $this->getContainer('no_exchange_options.yml');
512+
513+
$definition = $container->getDefinition('old_sound_rabbit_mq.producer_producer');
514+
$calls = $definition->getMethodCalls();
515+
$this->assertEquals('setExchangeOptions', $calls[0][0]);
516+
$options = $calls[0][1];
517+
518+
$this->assertEquals('', $options[0]['name']);
519+
$this->assertEquals('direct', $options[0]['type']);
520+
$this->assertEquals(false, $options[0]['declare']);
521+
$this->assertEquals(true, $options[0]['passive']);
522+
}
523+
502524
private function getContainer($file, $debug = false)
503525
{
504526
$container = new ContainerBuilder(new ParameterBag(array('kernel.debug' => $debug)));

0 commit comments

Comments
 (0)