Skip to content

Commit 4bfb678

Browse files
authored
Merge pull request #659 from nathanjrobertson/default_producer_variables
Add configuration support for default content_type and delivery_mode in Producers
2 parents a7e5801 + 25ad93a commit 4bfb678

File tree

5 files changed

+30
-6
lines changed

5 files changed

+30
-6
lines changed

DependencyInjection/Configuration.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace OldSound\RabbitMqBundle\DependencyInjection;
44

5+
use OldSound\RabbitMqBundle\RabbitMq\Producer;
56
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
67
use Symfony\Component\Config\Definition\ConfigurationInterface;
78
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
@@ -125,6 +126,8 @@ protected function addProducers(ArrayNodeDefinition $node)
125126
->scalarNode('enable_logger')->defaultFalse()->end()
126127
->scalarNode('service_alias')->defaultValue(null)->end()
127128
->scalarNode('default_routing_key')->defaultValue('')->end()
129+
->scalarNode('default_content_type')->defaultValue(Producer::DEFAULT_CONTENT_TYPE)->end()
130+
->integerNode('default_delivery_mode')->min(1)->max(2)->defaultValue(2)->end()
128131
->end()
129132
->end()
130133
->end()

DependencyInjection/OldSoundRabbitMqExtension.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,8 @@ protected function loadProducers()
187187
}
188188

189189
$definition->addMethodCall('setDefaultRoutingKey', array($producer['default_routing_key']));
190+
$definition->addMethodCall('setContentType', array($producer['default_content_type']));
191+
$definition->addMethodCall('setDeliveryMode', array($producer['default_delivery_mode']));
190192
}
191193
} else {
192194
foreach ($this->config['producers'] as $key => $producer) {

README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,12 @@ old_sound_rabbit_mq:
131131
url: 'amqp://guest:password@localhost:5672/vhost?lazy=1&connection_timeout=6'
132132
producers:
133133
upload_picture:
134-
connection: default
135-
exchange_options: {name: 'upload-picture', type: direct}
136-
service_alias: my_app_service # no alias by default
134+
connection: default
135+
exchange_options: {name: 'upload-picture', type: direct}
136+
service_alias: my_app_service # no alias by default
137+
default_routing_key: 'optional.routing.key' # defaults to '' if not set
138+
default_content_type: 'content/type' # defaults to 'text/plain'
139+
default_delivery_mode: 2 # optional. 1 means non-persistent, 2 means persistent. Defaults to "2".
137140
consumers:
138141
upload_picture:
139142
connection: default
@@ -284,8 +287,7 @@ As you can see, if in your configuration you have a producer called __upload\_pi
284287

285288
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.
286289

287-
You can use __setContentType__ and __setDeliveryMode__ methods in order to set the message content type and the message
288-
delivery mode respectively. Default values are __text/plain__ for content type and __2__ for delivery mode.
290+
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.
289291

290292
```php
291293
$this->get('old_sound_rabbit_mq.upload_picture_producer')->setContentType('application/json');

RabbitMq/Producer.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
*/
1111
class Producer extends BaseAmqp implements ProducerInterface
1212
{
13-
protected $contentType = 'text/plain';
13+
public const DEFAULT_CONTENT_TYPE = 'text/plain';
14+
protected $contentType = Producer::DEFAULT_CONTENT_TYPE;
1415
protected $deliveryMode = 2;
1516
protected $defaultRoutingKey = '';
1617

Tests/DependencyInjection/OldSoundRabbitMqExtensionTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,14 @@ public function testFooProducerDefinition()
317317
array(
318318
'setDefaultRoutingKey',
319319
array('')
320+
),
321+
array(
322+
'setContentType',
323+
array('text/plain')
324+
),
325+
array(
326+
'setDeliveryMode',
327+
array(2)
320328
)
321329
),
322330
$definition->getMethodCalls()
@@ -402,6 +410,14 @@ public function testDefaultProducerDefinition()
402410
array(
403411
'setDefaultRoutingKey',
404412
array('')
413+
),
414+
array(
415+
'setContentType',
416+
array('text/plain')
417+
),
418+
array(
419+
'setDeliveryMode',
420+
array(2)
405421
)
406422
),
407423
$definition->getMethodCalls()

0 commit comments

Comments
 (0)