Skip to content

Commit d815829

Browse files
authored
Merge pull request #658 from nathanjrobertson/producer_default_routing_key
Producer default routing key
2 parents 55d7879 + c7aeb5e commit d815829

File tree

4 files changed

+22
-2
lines changed

4 files changed

+22
-2
lines changed

DependencyInjection/Configuration.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ protected function addProducers(ArrayNodeDefinition $node)
124124
->scalarNode('class')->defaultValue('%old_sound_rabbit_mq.producer.class%')->end()
125125
->scalarNode('enable_logger')->defaultFalse()->end()
126126
->scalarNode('service_alias')->defaultValue(null)->end()
127+
->scalarNode('default_routing_key')->defaultValue('')->end()
127128
->end()
128129
->end()
129130
->end()

DependencyInjection/OldSoundRabbitMqExtension.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,8 @@ protected function loadProducers()
185185
->registerAliasForArgument($producerServiceName, $producer['class'], $argName)
186186
->setPublic(false);
187187
}
188+
189+
$definition->addMethodCall('setDefaultRoutingKey', array($producer['default_routing_key']));
188190
}
189191
} else {
190192
foreach ($this->config['producers'] as $key => $producer) {

RabbitMq/Producer.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class Producer extends BaseAmqp implements ProducerInterface
1212
{
1313
protected $contentType = 'text/plain';
1414
protected $deliveryMode = 2;
15+
protected $defaultRoutingKey = '';
1516

1617
public function setContentType($contentType)
1718
{
@@ -27,6 +28,13 @@ public function setDeliveryMode($deliveryMode)
2728
return $this;
2829
}
2930

31+
public function setDefaultRoutingKey($defaultRoutingKey)
32+
{
33+
$this->defaultRoutingKey = $defaultRoutingKey;
34+
35+
return $this;
36+
}
37+
3038
protected function getBasicProperties()
3139
{
3240
return array('content_type' => $this->contentType, 'delivery_mode' => $this->deliveryMode);
@@ -40,7 +48,7 @@ protected function getBasicProperties()
4048
* @param array $additionalProperties
4149
* @param array $headers
4250
*/
43-
public function publish($msgBody, $routingKey = '', $additionalProperties = array(), array $headers = null)
51+
public function publish($msgBody, $routingKey = null, $additionalProperties = array(), array $headers = null)
4452
{
4553
if ($this->autoSetupFabric) {
4654
$this->setupFabric();
@@ -53,7 +61,8 @@ public function publish($msgBody, $routingKey = '', $additionalProperties = arra
5361
$msg->set('application_headers', $headersTable);
5462
}
5563

56-
$this->getChannel()->basic_publish($msg, $this->exchangeOptions['name'], (string)$routingKey);
64+
$real_routingKey = $routingKey !== null ? $routingKey : $this->defaultRoutingKey;
65+
$this->getChannel()->basic_publish($msg, $this->exchangeOptions['name'], (string)$real_routingKey);
5766
$this->logger->debug('AMQP message published', array(
5867
'amqp' => array(
5968
'body' => $msgBody,

Tests/DependencyInjection/OldSoundRabbitMqExtensionTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,10 @@ public function testFooProducerDefinition()
313313
'declare' => false,
314314
)
315315
)
316+
),
317+
array(
318+
'setDefaultRoutingKey',
319+
array('')
316320
)
317321
),
318322
$definition->getMethodCalls()
@@ -394,6 +398,10 @@ public function testDefaultProducerDefinition()
394398
'declare' => false,
395399
)
396400
)
401+
),
402+
array(
403+
'setDefaultRoutingKey',
404+
array('')
397405
)
398406
),
399407
$definition->getMethodCalls()

0 commit comments

Comments
 (0)