1- // Copyright (c) 2007-Present Pivotal Software, Inc. All rights reserved.
1+ // Copyright (c) 2007-2020 Pivotal Software, Inc. All rights reserved.
22//
33// This software, the RabbitMQ Java client library, is triple-licensed under the
44// Mozilla Public License 1.1 ("MPL"), the GNU General Public License version 2
@@ -81,17 +81,20 @@ public Response handle(Object reply) {
8181 }
8282 };
8383
84- <<<<<<< HEAD
8584 private final RpcClientReplyHandler _replyHandler ;
86- =======
87- private final Function <Object , Response > _replyHandler ;
88- >>>>>>> 102 cbbde1 ... 637 : Default generator should not be static
8985
9086 /** Map from request correlation ID to continuation BlockingCell */
9187 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+ */
9394 private final Supplier <String > _correlationIdGenerator ;
9495
96+ private String lastCorrelationId = "0" ;
97+
9598 /** Consumer attached to our reply queue */
9699 private DefaultConsumer _consumer ;
97100
@@ -115,7 +118,7 @@ public RpcClient(RpcClientParams params) throws
115118 _timeout = params .getTimeout ();
116119 _useMandatory = params .shouldUseMandatory ();
117120 _replyHandler = params .getReplyHandler ();
118- _correlationIdGenerator = params .getCorrelationIdGenerator ();
121+ _correlationIdGenerator = params .getCorrelationIdSupplier ();
119122
120123 _consumer = setupConsumer ();
121124 if (_useMandatory ) {
@@ -302,6 +305,7 @@ public Response doCall(AMQP.BasicProperties props, byte[] message, int timeout)
302305 String replyId ;
303306 synchronized (_continuationMap ) {
304307 replyId = _correlationIdGenerator .get ();
308+ lastCorrelationId = replyId ;
305309 props = ((props ==null ) ? new AMQP .BasicProperties .Builder () : props .builder ())
306310 .correlationId (replyId ).replyTo (_replyTo ).build ();
307311 _continuationMap .put (replyId , k );
@@ -482,16 +486,21 @@ public Map<String, BlockingCell<Object>> getContinuationMap() {
482486 }
483487
484488 /**
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+ *
486499 * @return the most recently used correlation id
487- * @deprecated Only works for {@link IncrementingCorrelationIdGenerator}
500+ * @see RpcClientParams#correlationIdSupplier(Supplier)
488501 */
489502 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 );
495504 }
496505
497506 /**
@@ -559,5 +568,47 @@ public interface RpcClientReplyHandler {
559568 Response handle (Object reply );
560569
561570 }
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+ }
562613}
563614
0 commit comments