Skip to content

Commit d2a17ab

Browse files
authored
Add "no_ack" consumer option (issue-717) (#718)
* Add no_ack consumer option * Replace consumer_options to options * Add options for anon consumer * Add Unit tests for DI * Update README.md * Update CHANGELOG
1 parent e105112 commit d2a17ab

10 files changed

+127
-3
lines changed

CHANGELOG

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
- 2023-11-07
2+
* Add consumer option `no_ack`
3+
14
- 2021-05-15
25
* Add possibility to use multiple RabbitMQ hosts
36

DependencyInjection/Configuration.php

+30
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,12 @@ protected function addConsumers(ArrayNodeDefinition $node)
185185
->end()
186186
->end()
187187
->scalarNode('auto_setup_fabric')->defaultTrue()->end()
188+
->arrayNode('options')
189+
->canBeUnset()
190+
->children()
191+
->booleanNode('no_ack')->defaultFalse()->end()
192+
->end()
193+
->end()
188194
->arrayNode('qos_options')
189195
->canBeUnset()
190196
->children()
@@ -217,6 +223,12 @@ protected function addMultipleConsumers(ArrayNodeDefinition $node)
217223
->scalarNode('idle_timeout_exit_code')->end()
218224
->scalarNode('timeout_wait')->end()
219225
->scalarNode('auto_setup_fabric')->defaultTrue()->end()
226+
->arrayNode('options')
227+
->canBeUnset()
228+
->children()
229+
->booleanNode('no_ack')->defaultFalse()->end()
230+
->end()
231+
->end()
220232
->arrayNode('graceful_max_execution')
221233
->canBeUnset()
222234
->children()
@@ -265,6 +277,12 @@ protected function addDynamicConsumers(ArrayNodeDefinition $node)
265277
->end()
266278
->end()
267279
->scalarNode('auto_setup_fabric')->defaultTrue()->end()
280+
->arrayNode('options')
281+
->canBeUnset()
282+
->children()
283+
->booleanNode('no_ack')->defaultFalse()->end()
284+
->end()
285+
->end()
268286
->arrayNode('qos_options')
269287
->canBeUnset()
270288
->children()
@@ -311,6 +329,12 @@ protected function addBatchConsumers(ArrayNodeDefinition $node)
311329
->end()
312330
->end()
313331
->scalarNode('auto_setup_fabric')->defaultTrue()->end()
332+
->arrayNode('options')
333+
->canBeUnset()
334+
->children()
335+
->booleanNode('no_ack')->defaultFalse()->end()
336+
->end()
337+
->end()
314338
->arrayNode('qos_options')
315339
->children()
316340
->scalarNode('prefetch_size')->defaultValue(0)->end()
@@ -339,6 +363,12 @@ protected function addAnonConsumers(ArrayNodeDefinition $node)
339363
->children()
340364
->scalarNode('connection')->defaultValue('default')->end()
341365
->scalarNode('callback')->isRequired()->end()
366+
->arrayNode('options')
367+
->canBeUnset()
368+
->children()
369+
->booleanNode('no_ack')->defaultFalse()->end()
370+
->end()
371+
->end()
342372
->end()
343373
->end()
344374
->end()

DependencyInjection/OldSoundRabbitMqExtension.php

+33
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,12 @@ protected function loadConsumers()
257257
if (!$consumer['auto_setup_fabric']) {
258258
$definition->addMethodCall('disableAutoSetupFabric');
259259
}
260+
if (isset($consumer['options'])) {
261+
$definition->addMethodCall(
262+
'setConsumerOptions',
263+
[$this->normalizeArgumentKeys($consumer['options'])]
264+
);
265+
}
260266

261267
$this->injectConnection($definition, $consumer['connection']);
262268
if ($this->collectorEnabled) {
@@ -349,6 +355,12 @@ protected function loadMultipleConsumers()
349355
if (!$consumer['auto_setup_fabric']) {
350356
$definition->addMethodCall('disableAutoSetupFabric');
351357
}
358+
if (isset($consumer['options'])) {
359+
$definition->addMethodCall(
360+
'setConsumerOptions',
361+
[$this->normalizeArgumentKeys($consumer['options'])]
362+
);
363+
}
352364

353365
$this->injectConnection($definition, $consumer['connection']);
354366
if ($this->collectorEnabled) {
@@ -424,6 +436,12 @@ protected function loadDynamicConsumers()
424436
if (!$consumer['auto_setup_fabric']) {
425437
$definition->addMethodCall('disableAutoSetupFabric');
426438
}
439+
if (isset($consumer['options'])) {
440+
$definition->addMethodCall(
441+
'setConsumerOptions',
442+
[$this->normalizeArgumentKeys($consumer['options'])]
443+
);
444+
}
427445

428446
$this->injectConnection($definition, $consumer['connection']);
429447
if ($this->collectorEnabled) {
@@ -485,6 +503,13 @@ protected function loadBatchConsumers()
485503
$definition->addMethodCall('disableAutoSetupFabric');
486504
}
487505

506+
if (isset($consumer['options'])) {
507+
$definition->addMethodCall(
508+
'setConsumerOptions',
509+
[$this->normalizeArgumentKeys($consumer['options'])]
510+
);
511+
}
512+
488513
if ($consumer['keep_alive']) {
489514
$definition->addMethodCall('keepAlive');
490515
}
@@ -512,6 +537,14 @@ protected function loadAnonConsumers()
512537
->addTag('old_sound_rabbit_mq.anon_consumer')
513538
->addMethodCall('setExchangeOptions', [$this->normalizeArgumentKeys($anon['exchange_options'])])
514539
->addMethodCall('setCallback', [[new Reference($anon['callback']), 'execute']]);
540+
541+
if (isset($anon['options'])) {
542+
$definition->addMethodCall(
543+
'setConsumerOptions',
544+
[$this->normalizeArgumentKeys($anon['options'])]
545+
);
546+
}
547+
515548
$this->injectConnection($definition, $anon['connection']);
516549
if ($this->collectorEnabled) {
517550
$this->injectLoggedChannel($definition, $key, $anon['connection']);

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ old_sound_rabbit_mq:
145145
exchange_options: {name: 'upload-picture', type: direct}
146146
queue_options: {name: 'upload-picture'}
147147
callback: upload_picture_service
148+
options:
149+
no_ack: false # optional. If set to "true", automatic acknowledgement mode will be used by this consumer. Default "false". See https://www.rabbitmq.com/confirms.html for details.
148150
```
149151
150152
Here we configure the connection service and the message endpoints that our application will have. In this example your service container will contain the service `old_sound_rabbit_mq.upload_picture_producer` and `old_sound_rabbit_mq.upload_picture_consumer`. The later expects that there's a service called `upload_picture_service`.

RabbitMq/BaseAmqp.php

+13
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ abstract class BaseAmqp
4949
'declare' => true,
5050
];
5151

52+
protected $consumerOptions = [
53+
'no_ack' => false,
54+
];
55+
5256
/**
5357
* @var EventDispatcherInterface|null
5458
*/
@@ -155,6 +159,15 @@ public function setQueueOptions(array $options = [])
155159
$this->queueOptions = array_merge($this->queueOptions, $options);
156160
}
157161

162+
/**
163+
* @param array $options
164+
* @return void
165+
*/
166+
public function setConsumerOptions(array $options = [])
167+
{
168+
$this->consumerOptions = array_merge($this->consumerOptions, $options);
169+
}
170+
158171
/**
159172
* @param string $routingKey
160173
* @return void

RabbitMq/BaseConsumer.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ protected function setupConsumer()
6868
if ($this->autoSetupFabric) {
6969
$this->setupFabric();
7070
}
71-
$this->getChannel()->basic_consume($this->queueOptions['name'], $this->getConsumerTag(), false, false, false, false, [$this, 'processMessage']);
71+
$this->getChannel()->basic_consume($this->queueOptions['name'], $this->getConsumerTag(), false, $this->consumerOptions['no_ack'], false, false, [$this, 'processMessage']);
7272
}
7373

7474
public function processMessage(AMQPMessage $msg)

RabbitMq/BatchConsumer.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ protected function setupConsumer()
355355
$this->setupFabric();
356356
}
357357

358-
$this->getChannel()->basic_consume($this->queueOptions['name'], $this->getConsumerTag(), false, false, false, false, [$this, 'processMessage']);
358+
$this->getChannel()->basic_consume($this->queueOptions['name'], $this->getConsumerTag(), false, $this->consumerOptions['no_ack'], false, false, [$this, 'processMessage']);
359359
}
360360

361361
/**

RabbitMq/MultipleConsumer.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ protected function setupConsumer()
6464
//PHP 5.3 Compliant
6565
$currentObject = $this;
6666

67-
$this->getChannel()->basic_consume($name, $this->getQueueConsumerTag($name), false, false, false, false, function (AMQPMessage $msg) use ($currentObject, $name) {
67+
$this->getChannel()->basic_consume($name, $this->getQueueConsumerTag($name), false, $this->consumerOptions['no_ack'], false, false, function (AMQPMessage $msg) use ($currentObject, $name) {
6868
$currentObject->processQueueMessage($name, $msg);
6969
});
7070
}

Tests/DependencyInjection/Fixtures/test.yml

+11
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ old_sound_rabbit_mq:
122122
- 'android.#.upload'
123123
- 'iphone.upload'
124124
callback: foo.callback
125+
options:
126+
no_ack: true
125127

126128
default_consumer:
127129
exchange_options:
@@ -169,6 +171,8 @@ old_sound_rabbit_mq:
169171
- 'iphone.upload'
170172
callback: foo.multiple_test2.callback
171173
queues_provider: foo.queues_provider
174+
options:
175+
no_ack: true
172176

173177
dynamic_consumers:
174178
foo_dyn_consumer:
@@ -178,16 +182,21 @@ old_sound_rabbit_mq:
178182
type: direct
179183
callback: foo.dynamic.callback
180184
queue_options_provider: foo.dynamic.provider
185+
options:
186+
no_ack: true
187+
181188
bar_dyn_consumer:
182189
connection: bar_default
183190
exchange_options:
184191
name: bar_dynamic_exchange
185192
type: direct
186193
callback: bar.dynamic.callback
187194
queue_options_provider: bar.dynamic.provider
195+
188196
bindings:
189197
- {exchange: foo, destination: bar, routing_key: baz}
190198
- {exchange: moo, connection: default2, destination: cow, nowait: true, destination_is_exchange: true, arguments: {moo: cow}}
199+
191200
anon_consumers:
192201
foo_anon_consumer:
193202
connection: foo_connection
@@ -202,6 +211,8 @@ old_sound_rabbit_mq:
202211
arguments: null
203212
ticket: null
204213
callback: foo_anon.callback
214+
options:
215+
no_ack: true
205216

206217
default_anon_consumer:
207218
exchange_options:

Tests/DependencyInjection/OldSoundRabbitMqExtensionTest.php

+32
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,14 @@ public function testFooConsumerDefinition()
486486
'setTimeoutWait',
487487
[3],
488488
],
489+
[
490+
'setConsumerOptions',
491+
[
492+
[
493+
'no_ack' => true,
494+
],
495+
],
496+
],
489497
],
490498
$definition->getMethodCalls()
491499
);
@@ -670,6 +678,14 @@ public function testMultipleConsumerDefinition()
670678
'setTimeoutWait',
671679
[3],
672680
],
681+
[
682+
'setConsumerOptions',
683+
[
684+
[
685+
'no_ack' => true,
686+
],
687+
],
688+
],
673689
],
674690
$definition->getMethodCalls()
675691
);
@@ -714,6 +730,14 @@ public function testDynamicConsumerDefinition()
714730
new Reference('foo.dynamic.provider'),
715731
],
716732
],
733+
[
734+
'setConsumerOptions',
735+
[
736+
[
737+
'no_ack' => true,
738+
],
739+
],
740+
],
717741
],
718742
$definition->getMethodCalls()
719743
);
@@ -750,6 +774,14 @@ public function testFooAnonConsumerDefinition()
750774
'setCallback',
751775
[[new Reference('foo_anon.callback'), 'execute']],
752776
],
777+
[
778+
'setConsumerOptions',
779+
[
780+
[
781+
'no_ack' => true,
782+
],
783+
],
784+
],
753785
],
754786
$definition->getMethodCalls()
755787
);

0 commit comments

Comments
 (0)