Skip to content

Producer default routing key #658

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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