1
- // Copyright (c) 2007-Present Pivotal Software, Inc. All rights reserved.
1
+ // Copyright (c) 2007-2020 Pivotal Software, Inc. All rights reserved.
2
2
//
3
3
// This software, the RabbitMQ Java client library, is triple-licensed under the
4
4
// Mozilla Public License 1.1 ("MPL"), the GNU General Public License version 2
@@ -84,9 +84,16 @@ public class RpcClient {
84
84
85
85
/** Map from request correlation ID to continuation BlockingCell */
86
86
private final Map <String , BlockingCell <Object >> _continuationMap = new HashMap <String , BlockingCell <Object >>();
87
- /** Contains the most recently-used request correlation ID */
87
+
88
+ /**
89
+ * Generates correlation ID for each request.
90
+ *
91
+ * @since 5.9.0
92
+ */
88
93
private final Supplier <String > _correlationIdGenerator ;
89
94
95
+ private String lastCorrelationId = "0" ;
96
+
90
97
/** Consumer attached to our reply queue */
91
98
private DefaultConsumer _consumer ;
92
99
@@ -110,7 +117,7 @@ public RpcClient(RpcClientParams params) throws
110
117
_timeout = params .getTimeout ();
111
118
_useMandatory = params .shouldUseMandatory ();
112
119
_replyHandler = params .getReplyHandler ();
113
- _correlationIdGenerator = params .getCorrelationIdGenerator ();
120
+ _correlationIdGenerator = params .getCorrelationIdSupplier ();
114
121
115
122
_consumer = setupConsumer ();
116
123
if (_useMandatory ) {
@@ -210,6 +217,7 @@ public Response doCall(AMQP.BasicProperties props, byte[] message, int timeout)
210
217
String replyId ;
211
218
synchronized (_continuationMap ) {
212
219
replyId = _correlationIdGenerator .get ();
220
+ lastCorrelationId = replyId ;
213
221
props = ((props ==null ) ? new AMQP .BasicProperties .Builder () : props .builder ())
214
222
.correlationId (replyId ).replyTo (_replyTo ).build ();
215
223
_continuationMap .put (replyId , k );
@@ -390,16 +398,21 @@ public Map<String, BlockingCell<Object>> getContinuationMap() {
390
398
}
391
399
392
400
/**
393
- * Retrieve the correlation id.
401
+ * Retrieve the last correlation id used.
402
+ * <p>
403
+ * Note as of 5.9.0, correlation IDs may not always be integers
404
+ * (by default, they are).
405
+ * This method will try to parse the last correlation ID string
406
+ * as an integer, so this may result in {@link NumberFormatException}
407
+ * if the correlation ID supplier provided by
408
+ * {@link RpcClientParams#correlationIdSupplier(Supplier)}
409
+ * does not generate appropriate IDs.
410
+ *
394
411
* @return the most recently used correlation id
395
- * @deprecated Only works for {@link IncrementingCorrelationIdGenerator}
412
+ * @see RpcClientParams#correlationIdSupplier(Supplier)
396
413
*/
397
414
public int getCorrelationId () {
398
- if (_correlationIdGenerator instanceof IncrementingCorrelationIdGenerator ) {
399
- return ((IncrementingCorrelationIdGenerator ) _correlationIdGenerator ).getCorrelationId ();
400
- } else {
401
- throw new UnsupportedOperationException ();
402
- }
415
+ return Integer .valueOf (this .lastCorrelationId );
403
416
}
404
417
405
418
/**
@@ -447,5 +460,47 @@ public byte[] getBody() {
447
460
return body ;
448
461
}
449
462
}
463
+
464
+ /**
465
+ * Creates generation IDs as a sequence of integers.
466
+ *
467
+ * @return
468
+ * @see RpcClientParams#correlationIdSupplier(Supplier)
469
+ * @since 5.9.0
470
+ */
471
+ public static Supplier <String > incrementingCorrelationIdSupplier () {
472
+ return incrementingCorrelationIdSupplier ("" );
473
+ }
474
+
475
+ /**
476
+ * Creates generation IDs as a sequence of integers, with the provided prefix.
477
+ *
478
+ * @param prefix
479
+ * @return
480
+ * @see RpcClientParams#correlationIdSupplier(Supplier)
481
+ * @since 5.9.0
482
+ */
483
+ public static Supplier <String > incrementingCorrelationIdSupplier (String prefix ) {
484
+ return new IncrementingCorrelationIdSupplier (prefix );
485
+ }
486
+
487
+ /**
488
+ * @since 5.9.0
489
+ */
490
+ private static class IncrementingCorrelationIdSupplier implements Supplier <String > {
491
+
492
+ private final String prefix ;
493
+ private int correlationId ;
494
+
495
+ public IncrementingCorrelationIdSupplier (String prefix ) {
496
+ this .prefix = prefix ;
497
+ }
498
+
499
+ @ Override
500
+ public String get () {
501
+ return prefix + ++correlationId ;
502
+ }
503
+
504
+ }
450
505
}
451
506
0 commit comments