Skip to content

Add configuration support for default content_type and delivery_mode in Producers #659

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 3 commits into from
Oct 27, 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
3 changes: 3 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace OldSound\RabbitMqBundle\DependencyInjection;

use OldSound\RabbitMqBundle\RabbitMq\Producer;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
Expand Down Expand Up @@ -125,6 +126,8 @@ protected function addProducers(ArrayNodeDefinition $node)
->scalarNode('enable_logger')->defaultFalse()->end()
->scalarNode('service_alias')->defaultValue(null)->end()
->scalarNode('default_routing_key')->defaultValue('')->end()
->scalarNode('default_content_type')->defaultValue(Producer::DEFAULT_CONTENT_TYPE)->end()
->integerNode('default_delivery_mode')->min(1)->max(2)->defaultValue(2)->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 @@ -187,6 +187,8 @@ protected function loadProducers()
}

$definition->addMethodCall('setDefaultRoutingKey', array($producer['default_routing_key']));
$definition->addMethodCall('setContentType', array($producer['default_content_type']));
$definition->addMethodCall('setDeliveryMode', array($producer['default_delivery_mode']));
}
} else {
foreach ($this->config['producers'] as $key => $producer) {
Expand Down
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,12 @@ old_sound_rabbit_mq:
url: 'amqp://guest:password@localhost:5672/vhost?lazy=1&connection_timeout=6'
producers:
upload_picture:
connection: default
exchange_options: {name: 'upload-picture', type: direct}
service_alias: my_app_service # no alias by default
connection: default
exchange_options: {name: 'upload-picture', type: direct}
service_alias: my_app_service # no alias by default
default_routing_key: 'optional.routing.key' # defaults to '' if not set
default_content_type: 'content/type' # defaults to 'text/plain'
default_delivery_mode: 2 # optional. 1 means non-persistent, 2 means persistent. Defaults to "2".
consumers:
upload_picture:
connection: default
Expand Down Expand Up @@ -284,8 +287,7 @@ As you can see, if in your configuration you have a producer called __upload\_pi

Besides the message itself, the `OldSound\RabbitMqBundle\RabbitMq\Producer#publish()` method also accepts an optional routing key parameter and an optional array of additional properties. The array of additional properties allows you to alter the properties with which an `PhpAmqpLib\Message\AMQPMessage` object gets constructed by default. This way, for example, you can change the application headers.

You can use __setContentType__ and __setDeliveryMode__ methods in order to set the message content type and the message
delivery mode respectively. Default values are __text/plain__ for content type and __2__ for delivery mode.
You can use __setContentType__ and __setDeliveryMode__ methods in order to set the message content type and the message delivery mode respectively, overriding any default set in the "producers" config section. If not overriden by either the "producers" configuration or an explicit call to these methods (as per the below example), the default values are __text/plain__ for content type and __2__ for delivery mode.

```php
$this->get('old_sound_rabbit_mq.upload_picture_producer')->setContentType('application/json');
Expand Down
3 changes: 2 additions & 1 deletion RabbitMq/Producer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
*/
class Producer extends BaseAmqp implements ProducerInterface
{
protected $contentType = 'text/plain';
public const DEFAULT_CONTENT_TYPE = 'text/plain';
protected $contentType = Producer::DEFAULT_CONTENT_TYPE;
protected $deliveryMode = 2;
protected $defaultRoutingKey = '';

Expand Down
16 changes: 16 additions & 0 deletions Tests/DependencyInjection/OldSoundRabbitMqExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,14 @@ public function testFooProducerDefinition()
array(
'setDefaultRoutingKey',
array('')
),
array(
'setContentType',
array('text/plain')
),
array(
'setDeliveryMode',
array(2)
)
),
$definition->getMethodCalls()
Expand Down Expand Up @@ -402,6 +410,14 @@ public function testDefaultProducerDefinition()
array(
'setDefaultRoutingKey',
array('')
),
array(
'setContentType',
array('text/plain')
),
array(
'setDeliveryMode',
array(2)
)
),
$definition->getMethodCalls()
Expand Down