@@ -47,6 +47,7 @@ class NatsConnection implements Connection {
4747 public static final double NANOS_PER_SECOND = 1_000_000_000.0 ;
4848
4949 private final Options options ;
50+ final boolean forceFlushOnRequest ;
5051
5152 private final StatisticsCollector statistics ;
5253
@@ -112,6 +113,7 @@ class NatsConnection implements Connection {
112113 timeTraceLogger .trace ("creating connection object" );
113114
114115 this .options = options ;
116+ forceFlushOnRequest = options .forceFlushOnRequest ();
115117
116118 advancedTracking = options .isTrackAdvancedStats ();
117119 this .statistics = options .getStatisticsCollector () == null ? new NatsStatistics () : options .getStatisticsCollector ();
@@ -1205,15 +1207,15 @@ else if (future.isDone()) {
12051207 */
12061208 @ Override
12071209 public Message request (String subject , byte [] body , Duration timeout ) throws InterruptedException {
1208- return requestInternal (subject , null , body , timeout , cancelAction , true );
1210+ return requestInternal (subject , null , body , timeout , cancelAction , true , forceFlushOnRequest );
12091211 }
12101212
12111213 /**
12121214 * {@inheritDoc}
12131215 */
12141216 @ Override
12151217 public Message request (String subject , Headers headers , byte [] body , Duration timeout ) throws InterruptedException {
1216- return requestInternal (subject , headers , body , timeout , cancelAction , true );
1218+ return requestInternal (subject , headers , body , timeout , cancelAction , true , forceFlushOnRequest );
12171219 }
12181220
12191221 /**
@@ -1222,11 +1224,12 @@ public Message request(String subject, Headers headers, byte[] body, Duration ti
12221224 @ Override
12231225 public Message request (Message message , Duration timeout ) throws InterruptedException {
12241226 validateNotNull (message , "Message" );
1225- return requestInternal (message .getSubject (), message .getHeaders (), message .getData (), timeout , cancelAction , false );
1227+ return requestInternal (message .getSubject (), message .getHeaders (), message .getData (), timeout , cancelAction , false , forceFlushOnRequest );
12261228 }
12271229
1228- Message requestInternal (String subject , Headers headers , byte [] data , Duration timeout , CancelAction cancelAction , boolean validateSubjectAndReplyTo ) throws InterruptedException {
1229- CompletableFuture <Message > incoming = requestFutureInternal (subject , headers , data , timeout , cancelAction , validateSubjectAndReplyTo );
1230+ Message requestInternal (String subject , Headers headers , byte [] data , Duration timeout ,
1231+ CancelAction cancelAction , boolean validateSubjectAndReplyTo , boolean flushImmediatelyAfterPublish ) throws InterruptedException {
1232+ CompletableFuture <Message > incoming = requestFutureInternal (subject , headers , data , timeout , cancelAction , validateSubjectAndReplyTo , flushImmediatelyAfterPublish );
12301233 try {
12311234 return incoming .get (timeout .toNanos (), TimeUnit .NANOSECONDS );
12321235 } catch (TimeoutException | ExecutionException | CancellationException e ) {
@@ -1239,31 +1242,31 @@ Message requestInternal(String subject, Headers headers, byte[] data, Duration t
12391242 */
12401243 @ Override
12411244 public CompletableFuture <Message > request (String subject , byte [] body ) {
1242- return requestFutureInternal (subject , null , body , null , cancelAction , true );
1245+ return requestFutureInternal (subject , null , body , null , cancelAction , true , forceFlushOnRequest );
12431246 }
12441247
12451248 /**
12461249 * {@inheritDoc}
12471250 */
12481251 @ Override
12491252 public CompletableFuture <Message > request (String subject , Headers headers , byte [] body ) {
1250- return requestFutureInternal (subject , headers , body , null , cancelAction , true );
1253+ return requestFutureInternal (subject , headers , body , null , cancelAction , true , forceFlushOnRequest );
12511254 }
12521255
12531256 /**
12541257 * {@inheritDoc}
12551258 */
12561259 @ Override
12571260 public CompletableFuture <Message > requestWithTimeout (String subject , byte [] body , Duration timeout ) {
1258- return requestFutureInternal (subject , null , body , timeout , cancelAction , true );
1261+ return requestFutureInternal (subject , null , body , timeout , cancelAction , true , forceFlushOnRequest );
12591262 }
12601263
12611264 /**
12621265 * {@inheritDoc}
12631266 */
12641267 @ Override
12651268 public CompletableFuture <Message > requestWithTimeout (String subject , Headers headers , byte [] body , Duration timeout ) {
1266- return requestFutureInternal (subject , headers , body , timeout , cancelAction , true );
1269+ return requestFutureInternal (subject , headers , body , timeout , cancelAction , true , forceFlushOnRequest );
12671270 }
12681271
12691272 /**
@@ -1272,7 +1275,7 @@ public CompletableFuture<Message> requestWithTimeout(String subject, Headers hea
12721275 @ Override
12731276 public CompletableFuture <Message > requestWithTimeout (Message message , Duration timeout ) {
12741277 validateNotNull (message , "Message" );
1275- return requestFutureInternal (message .getSubject (), message .getHeaders (), message .getData (), timeout , cancelAction , false );
1278+ return requestFutureInternal (message .getSubject (), message .getHeaders (), message .getData (), timeout , cancelAction , false , forceFlushOnRequest );
12761279 }
12771280
12781281 /**
@@ -1281,10 +1284,11 @@ public CompletableFuture<Message> requestWithTimeout(Message message, Duration t
12811284 @ Override
12821285 public CompletableFuture <Message > request (Message message ) {
12831286 validateNotNull (message , "Message" );
1284- return requestFutureInternal (message .getSubject (), message .getHeaders (), message .getData (), null , cancelAction , false );
1287+ return requestFutureInternal (message .getSubject (), message .getHeaders (), message .getData (), null , cancelAction , false , forceFlushOnRequest );
12851288 }
12861289
1287- CompletableFuture <Message > requestFutureInternal (String subject , Headers headers , byte [] data , Duration futureTimeout , CancelAction cancelAction , boolean validateSubjectAndReplyTo ) {
1290+ CompletableFuture <Message > requestFutureInternal (String subject , Headers headers , byte [] data , Duration futureTimeout ,
1291+ CancelAction cancelAction , boolean validateSubjectAndReplyTo , boolean flushImmediatelyAfterPublish ) {
12881292 checkPayloadSize (data );
12891293
12901294 if (isClosed ()) {
@@ -1336,7 +1340,7 @@ CompletableFuture<Message> requestFutureInternal(String subject, Headers headers
13361340 responsesAwaiting .put (sub .getSID (), future );
13371341 }
13381342
1339- publishInternal (subject , responseInbox , headers , data , validateSubjectAndReplyTo , true );
1343+ publishInternal (subject , responseInbox , headers , data , validateSubjectAndReplyTo , flushImmediatelyAfterPublish );
13401344 statistics .incrementRequestsSent ();
13411345
13421346 return future ;
0 commit comments