@@ -129,14 +129,41 @@ public class CachingConnectionFactory extends AbstractConnectionFactory
129
129
* The cache mode.
130
130
*/
131
131
public enum CacheMode {
132
+
132
133
/**
133
134
* Cache channels - single connection.
134
135
*/
135
136
CHANNEL ,
137
+
136
138
/**
137
139
* Cache connections and channels within each connection.
138
140
*/
139
141
CONNECTION
142
+
143
+ }
144
+
145
+ /**
146
+ * The type of publisher confirms to use.
147
+ */
148
+ public enum ConfirmType {
149
+
150
+ /**
151
+ * Use {@code RabbitTemplate#waitForConfirms()} (or {@code waitForConfirmsOrDie()}
152
+ * within scoped operations.
153
+ */
154
+ SIMPLE ,
155
+
156
+ /**
157
+ * Use with {@code CorrelationData} to correlate confirmations with sent
158
+ * messsages.
159
+ */
160
+ CORRELATED ,
161
+
162
+ /**
163
+ * Publisher confirms are disabled (default).
164
+ */
165
+ NONE
166
+
140
167
}
141
168
142
169
private final Set <ChannelCachingConnectionProxy > allocatedConnections = new HashSet <>();
@@ -176,9 +203,7 @@ public enum CacheMode {
176
203
177
204
private int connectionLimit = Integer .MAX_VALUE ;
178
205
179
- private boolean publisherConfirms ;
180
-
181
- private boolean simplePublisherConfirms ;
206
+ private ConfirmType confirmType = ConfirmType .NONE ;
182
207
183
208
private boolean publisherReturns ;
184
209
@@ -356,7 +381,7 @@ public void setConnectionLimit(int connectionLimit) {
356
381
357
382
@ Override
358
383
public boolean isPublisherConfirms () {
359
- return this .publisherConfirms ;
384
+ return ConfirmType . CORRELATED . equals ( this .confirmType ) ;
360
385
}
361
386
362
387
@ Override
@@ -372,36 +397,50 @@ public void setPublisherReturns(boolean publisherReturns) {
372
397
}
373
398
374
399
/**
375
- * Use full publisher confirms, with correlation data and a callback for each message.
400
+ * Use full (correlated) publisher confirms, with correlation data and a callback for
401
+ * each message.
376
402
* @param publisherConfirms true for full publisher returns,
377
403
* @since 1.1
404
+ * @deprecated in favor of {@link #setPublisherConfirmType(ConfirmType)}.
378
405
* @see #setSimplePublisherConfirms(boolean)
379
406
*/
407
+ @ Deprecated
380
408
public void setPublisherConfirms (boolean publisherConfirms ) {
381
- Assert .isTrue (!this .simplePublisherConfirms , "Cannot set both publisherConfirms and simplePublisherConfirms" );
382
- this .publisherConfirms = publisherConfirms ;
383
- if (this .publisherConnectionFactory != null ) {
384
- this .publisherConnectionFactory .setPublisherConfirms (publisherConfirms );
385
- }
409
+ Assert .isTrue (!ConfirmType .SIMPLE .equals (this .confirmType ),
410
+ "Cannot set both publisherConfirms and simplePublisherConfirms" );
411
+ setPublisherConfirmType (ConfirmType .CORRELATED );
386
412
}
387
413
388
414
/**
389
415
* Use simple publisher confirms where the template simply waits for completion.
390
416
* @param simplePublisherConfirms true for confirms.
391
417
* @since 2.1
418
+ * @deprecated in favor of {@link #setPublisherConfirmType(ConfirmType)}.
392
419
* @see #setPublisherConfirms(boolean)
393
420
*/
421
+ @ Deprecated
394
422
public void setSimplePublisherConfirms (boolean simplePublisherConfirms ) {
395
- Assert .isTrue (!this .publisherConfirms , "Cannot set both publisherConfirms and simplePublisherConfirms" );
396
- this .simplePublisherConfirms = simplePublisherConfirms ;
397
- if (this .publisherConnectionFactory != null ) {
398
- this .publisherConnectionFactory .setSimplePublisherConfirms (simplePublisherConfirms );
399
- }
423
+ Assert .isTrue (!ConfirmType .CORRELATED .equals (this .confirmType ),
424
+ "Cannot set both publisherConfirms and simplePublisherConfirms" );
425
+ setPublisherConfirmType (ConfirmType .SIMPLE );
400
426
}
401
427
402
428
@ Override
403
429
public boolean isSimplePublisherConfirms () {
404
- return this .simplePublisherConfirms ;
430
+ return this .confirmType .equals (ConfirmType .SIMPLE );
431
+ }
432
+
433
+ /**
434
+ * Set the confirm type to use; default {@link ConfirmType#NONE}.
435
+ * @param confirmType the confirm type.
436
+ * @since 2.2
437
+ */
438
+ public void setPublisherConfirmType (ConfirmType confirmType ) {
439
+ Assert .notNull (confirmType , "'confirmType' cannot be null" );
440
+ this .confirmType = confirmType ;
441
+ if (this .publisherConnectionFactory != null ) {
442
+ this .publisherConnectionFactory .setPublisherConfirmType (confirmType );
443
+ }
405
444
}
406
445
407
446
/**
@@ -630,7 +669,7 @@ private ChannelProxy getCachedChannelProxy(ChannelCachingConnectionProxy connect
630
669
}
631
670
getChannelListener ().onCreate (targetChannel , transactional );
632
671
Class <?>[] interfaces ;
633
- if (this .publisherConfirms || this .publisherReturns ) {
672
+ if (ConfirmType . CORRELATED . equals ( this .confirmType ) || this .publisherReturns ) {
634
673
interfaces = new Class <?>[] { ChannelProxy .class , PublisherCallbackChannel .class };
635
674
}
636
675
else {
@@ -672,15 +711,15 @@ else if (this.cacheMode == CacheMode.CONNECTION) {
672
711
673
712
private Channel doCreateBareChannel (ChannelCachingConnectionProxy conn , boolean transactional ) {
674
713
Channel channel = conn .createBareChannel (transactional );
675
- if (this . publisherConfirms || this .simplePublisherConfirms ) {
714
+ if (! ConfirmType . NONE . equals ( this .confirmType ) ) {
676
715
try {
677
716
channel .confirmSelect ();
678
717
}
679
718
catch (IOException e ) {
680
719
logger .error ("Could not configure the channel to receive publisher confirms" , e );
681
720
}
682
721
}
683
- if ((this .publisherConfirms || this .publisherReturns )
722
+ if ((ConfirmType . CORRELATED . equals ( this .confirmType ) || this .publisherReturns )
684
723
&& !(channel instanceof PublisherCallbackChannelImpl )) {
685
724
channel = this .publisherChannelFactory .createChannel (channel , getChannelsExecutor ());
686
725
}
@@ -1037,9 +1076,10 @@ private final class CachedChannelInvocationHandler implements InvocationHandler
1037
1076
1038
1077
private final boolean transactional ;
1039
1078
1040
- private final boolean confirmSelected = CachingConnectionFactory .this .simplePublisherConfirms ;
1079
+ private final boolean confirmSelected = ConfirmType . SIMPLE . equals ( CachingConnectionFactory .this .confirmType ) ;
1041
1080
1042
- private final boolean publisherConfirms = CachingConnectionFactory .this .publisherConfirms ;
1081
+ private final boolean publisherConfirms =
1082
+ ConfirmType .CORRELATED .equals (CachingConnectionFactory .this .confirmType );
1043
1083
1044
1084
private volatile Channel target ;
1045
1085
@@ -1285,7 +1325,7 @@ private void physicalClose(Object proxy) throws IOException, TimeoutException {
1285
1325
boolean async = false ;
1286
1326
try {
1287
1327
if (CachingConnectionFactory .this .active &&
1288
- (CachingConnectionFactory .this .publisherConfirms ||
1328
+ (ConfirmType . CORRELATED . equals ( CachingConnectionFactory .this .confirmType ) ||
1289
1329
CachingConnectionFactory .this .publisherReturns )) {
1290
1330
async = true ;
1291
1331
asyncClose (proxy );
@@ -1317,7 +1357,7 @@ private void asyncClose(Object proxy) {
1317
1357
try {
1318
1358
executorService .execute (() -> {
1319
1359
try {
1320
- if (CachingConnectionFactory .this .publisherConfirms ) {
1360
+ if (ConfirmType . CORRELATED . equals ( CachingConnectionFactory .this .confirmType ) ) {
1321
1361
channel .waitForConfirmsOrDie (ASYNC_CLOSE_TIMEOUT );
1322
1362
}
1323
1363
else {
0 commit comments