Skip to content

Commit

Permalink
Merge pull request #658 from nathanjrobertson/producer_default_routin…
Browse files Browse the repository at this point in the history
…g_key

Producer default routing key
  • Loading branch information
mihaileu authored Sep 16, 2021
2 parents 55d7879 + c7aeb5e commit d815829
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
1 change: 1 addition & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ protected function addProducers(ArrayNodeDefinition $node)
->scalarNode('class')->defaultValue('%old_sound_rabbit_mq.producer.class%')->end()
->scalarNode('enable_logger')->defaultFalse()->end()
->scalarNode('service_alias')->defaultValue(null)->end()
->scalarNode('default_routing_key')->defaultValue('')->end()
->end()
->end()
->end()
Expand Down
2 changes: 2 additions & 0 deletions DependencyInjection/OldSoundRabbitMqExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ protected function loadProducers()
->registerAliasForArgument($producerServiceName, $producer['class'], $argName)
->setPublic(false);
}

$definition->addMethodCall('setDefaultRoutingKey', array($producer['default_routing_key']));
}
} else {
foreach ($this->config['producers'] as $key => $producer) {
Expand Down
13 changes: 11 additions & 2 deletions RabbitMq/Producer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Producer extends BaseAmqp implements ProducerInterface
{
protected $contentType = 'text/plain';
protected $deliveryMode = 2;
protected $defaultRoutingKey = '';

public function setContentType($contentType)
{
Expand All @@ -27,6 +28,13 @@ public function setDeliveryMode($deliveryMode)
return $this;
}

public function setDefaultRoutingKey($defaultRoutingKey)
{
$this->defaultRoutingKey = $defaultRoutingKey;

return $this;
}

protected function getBasicProperties()
{
return array('content_type' => $this->contentType, 'delivery_mode' => $this->deliveryMode);
Expand All @@ -40,7 +48,7 @@ protected function getBasicProperties()
* @param array $additionalProperties
* @param array $headers
*/
public function publish($msgBody, $routingKey = '', $additionalProperties = array(), array $headers = null)
public function publish($msgBody, $routingKey = null, $additionalProperties = array(), array $headers = null)
{
if ($this->autoSetupFabric) {
$this->setupFabric();
Expand All @@ -53,7 +61,8 @@ public function publish($msgBody, $routingKey = '', $additionalProperties = arra
$msg->set('application_headers', $headersTable);
}

$this->getChannel()->basic_publish($msg, $this->exchangeOptions['name'], (string)$routingKey);
$real_routingKey = $routingKey !== null ? $routingKey : $this->defaultRoutingKey;
$this->getChannel()->basic_publish($msg, $this->exchangeOptions['name'], (string)$real_routingKey);
$this->logger->debug('AMQP message published', array(
'amqp' => array(
'body' => $msgBody,
Expand Down
8 changes: 8 additions & 0 deletions Tests/DependencyInjection/OldSoundRabbitMqExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,10 @@ public function testFooProducerDefinition()
'declare' => false,
)
)
),
array(
'setDefaultRoutingKey',
array('')
)
),
$definition->getMethodCalls()
Expand Down Expand Up @@ -394,6 +398,10 @@ public function testDefaultProducerDefinition()
'declare' => false,
)
)
),
array(
'setDefaultRoutingKey',
array('')
)
),
$definition->getMethodCalls()
Expand Down

0 comments on commit d815829

Please sign in to comment.