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
@@ -81,17 +81,20 @@ public Response handle(Object reply) {
81
81
}
82
82
};
83
83
84
- <<<<<<< HEAD
85
84
private final RpcClientReplyHandler _replyHandler ;
86
- =======
87
- private final Function <Object , Response > _replyHandler ;
88
- >>>>>>> 102 cbbde1 ... 637 : Default generator should not be static
89
85
90
86
/** Map from request correlation ID to continuation BlockingCell */
91
87
private final Map <String , BlockingCell <Object >> _continuationMap = new HashMap <String , BlockingCell <Object >>();
92
- /** Contains the most recently-used request correlation ID */
88
+
89
+ /**
90
+ * Generates correlation ID for each request.
91
+ *
92
+ * @since 5.9.0
93
+ */
93
94
private final Supplier <String > _correlationIdGenerator ;
94
95
96
+ private String lastCorrelationId = "0" ;
97
+
95
98
/** Consumer attached to our reply queue */
96
99
private DefaultConsumer _consumer ;
97
100
@@ -115,7 +118,7 @@ public RpcClient(RpcClientParams params) throws
115
118
_timeout = params .getTimeout ();
116
119
_useMandatory = params .shouldUseMandatory ();
117
120
_replyHandler = params .getReplyHandler ();
118
- _correlationIdGenerator = params .getCorrelationIdGenerator ();
121
+ _correlationIdGenerator = params .getCorrelationIdSupplier ();
119
122
120
123
_consumer = setupConsumer ();
121
124
if (_useMandatory ) {
@@ -302,6 +305,7 @@ public Response doCall(AMQP.BasicProperties props, byte[] message, int timeout)
302
305
String replyId ;
303
306
synchronized (_continuationMap ) {
304
307
replyId = _correlationIdGenerator .get ();
308
+ lastCorrelationId = replyId ;
305
309
props = ((props ==null ) ? new AMQP .BasicProperties .Builder () : props .builder ())
306
310
.correlationId (replyId ).replyTo (_replyTo ).build ();
307
311
_continuationMap .put (replyId , k );
@@ -482,16 +486,21 @@ public Map<String, BlockingCell<Object>> getContinuationMap() {
482
486
}
483
487
484
488
/**
485
- * Retrieve the correlation id.
489
+ * Retrieve the last correlation id used.
490
+ * <p>
491
+ * Note as of 5.9.0, correlation IDs may not always be integers
492
+ * (by default, they are).
493
+ * This method will try to parse the last correlation ID string
494
+ * as an integer, so this may result in {@link NumberFormatException}
495
+ * if the correlation ID supplier provided by
496
+ * {@link RpcClientParams#correlationIdSupplier(Supplier)}
497
+ * does not generate appropriate IDs.
498
+ *
486
499
* @return the most recently used correlation id
487
- * @deprecated Only works for {@link IncrementingCorrelationIdGenerator}
500
+ * @see RpcClientParams#correlationIdSupplier(Supplier)
488
501
*/
489
502
public int getCorrelationId () {
490
- if (_correlationIdGenerator instanceof IncrementingCorrelationIdGenerator ) {
491
- return ((IncrementingCorrelationIdGenerator ) _correlationIdGenerator ).getCorrelationId ();
492
- } else {
493
- throw new UnsupportedOperationException ();
494
- }
503
+ return Integer .valueOf (this .lastCorrelationId );
495
504
}
496
505
497
506
/**
@@ -559,5 +568,47 @@ public interface RpcClientReplyHandler {
559
568
Response handle (Object reply );
560
569
561
570
}
571
+
572
+ /**
573
+ * Creates generation IDs as a sequence of integers.
574
+ *
575
+ * @return
576
+ * @see RpcClientParams#correlationIdSupplier(Supplier)
577
+ * @since 5.9.0
578
+ */
579
+ public static Supplier <String > incrementingCorrelationIdSupplier () {
580
+ return incrementingCorrelationIdSupplier ("" );
581
+ }
582
+
583
+ /**
584
+ * Creates generation IDs as a sequence of integers, with the provided prefix.
585
+ *
586
+ * @param prefix
587
+ * @return
588
+ * @see RpcClientParams#correlationIdSupplier(Supplier)
589
+ * @since 5.9.0
590
+ */
591
+ public static Supplier <String > incrementingCorrelationIdSupplier (String prefix ) {
592
+ return new IncrementingCorrelationIdSupplier (prefix );
593
+ }
594
+
595
+ /**
596
+ * @since 5.9.0
597
+ */
598
+ private static class IncrementingCorrelationIdSupplier implements Supplier <String > {
599
+
600
+ private final String prefix ;
601
+ private int correlationId ;
602
+
603
+ public IncrementingCorrelationIdSupplier (String prefix ) {
604
+ this .prefix = prefix ;
605
+ }
606
+
607
+ @ Override
608
+ public String get () {
609
+ return prefix + ++correlationId ;
610
+ }
611
+
612
+ }
562
613
}
563
614
0 commit comments