Skip to content

Commit 75c1ca2

Browse files
Add producer support for default_routing_key
1 parent b6336e6 commit 75c1ca2

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-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', $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,

0 commit comments

Comments
 (0)