@@ -836,31 +836,31 @@ void cleanUpPongQueue() {
836
836
*/
837
837
@ Override
838
838
public void publish (String subject , byte [] body ) {
839
- publishInternal (subject , null , null , body );
839
+ publishInternal (subject , null , null , body , true );
840
840
}
841
841
842
842
/**
843
843
* {@inheritDoc}
844
844
*/
845
845
@ Override
846
846
public void publish (String subject , Headers headers , byte [] body ) {
847
- publishInternal (subject , null , headers , body );
847
+ publishInternal (subject , null , headers , body , true );
848
848
}
849
849
850
850
/**
851
851
* {@inheritDoc}
852
852
*/
853
853
@ Override
854
854
public void publish (String subject , String replyTo , byte [] body ) {
855
- publishInternal (subject , replyTo , null , body );
855
+ publishInternal (subject , replyTo , null , body , true );
856
856
}
857
857
858
858
/**
859
859
* {@inheritDoc}
860
860
*/
861
861
@ Override
862
862
public void publish (String subject , String replyTo , Headers headers , byte [] body ) {
863
- publishInternal (subject , replyTo , headers , body );
863
+ publishInternal (subject , replyTo , headers , body , true );
864
864
}
865
865
866
866
/**
@@ -869,35 +869,30 @@ public void publish(String subject, String replyTo, Headers headers, byte[] body
869
869
@ Override
870
870
public void publish (Message message ) {
871
871
validateNotNull (message , "Message" );
872
- publishInternal (message .getSubject (), message .getReplyTo (), message .getHeaders (), message .getData ());
872
+ publishInternal (message .getSubject (), message .getReplyTo (), message .getHeaders (), message .getData (), false );
873
873
}
874
874
875
- void publishInternal (String subject , String replyTo , Headers headers , byte [] data ) {
876
- checkIfNeedsHeaderSupport (headers );
875
+ void publishInternal (String subject , String replyTo , Headers headers , byte [] data , boolean validateSubRep ) {
877
876
checkPayloadSize (data );
877
+ NatsPublishableMessage npm = new NatsPublishableMessage (subject , replyTo , headers , data , validateSubRep );
878
+ if (npm .hasHeaders && !serverInfo .get ().isHeadersSupported ()) {
879
+ throw new IllegalArgumentException ("Headers are not supported by the server, version: " + serverInfo .get ().getVersion ());
880
+ }
878
881
879
882
if (isClosed ()) {
880
883
throw new IllegalStateException ("Connection is Closed" );
881
884
} else if (blockPublishForDrain .get ()) {
882
885
throw new IllegalStateException ("Connection is Draining" ); // Ok to publish while waiting on subs
883
886
}
884
887
885
- NatsMessage nm = new NatsMessage (subject , replyTo , new Headers (headers ), data );
886
-
887
888
Connection .Status stat = this .status ;
888
889
if ((stat == Status .RECONNECTING || stat == Status .DISCONNECTED )
889
- && !this .writer .canQueueDuringReconnect (nm )) {
890
+ && !this .writer .canQueueDuringReconnect (npm )) {
890
891
throw new IllegalStateException (
891
892
"Unable to queue any more messages during reconnect, max buffer is " + options .getReconnectBufferSize ());
892
893
}
893
- queueOutgoing (nm );
894
- }
895
894
896
- private void checkIfNeedsHeaderSupport (Headers headers ) {
897
- if (headers != null && !headers .isEmpty () && !serverInfo .get ().isHeadersSupported ()) {
898
- throw new IllegalArgumentException (
899
- "Headers are not supported by the server, version: " + serverInfo .get ().getVersion ());
900
- }
895
+ queueOutgoing (npm );
901
896
}
902
897
903
898
private void checkPayloadSize (byte [] body ) {
@@ -1099,15 +1094,15 @@ else if (future.isDone()) {
1099
1094
*/
1100
1095
@ Override
1101
1096
public Message request (String subject , byte [] body , Duration timeout ) throws InterruptedException {
1102
- return requestInternal (subject , null , body , timeout , cancelAction );
1097
+ return requestInternal (subject , null , body , timeout , cancelAction , true );
1103
1098
}
1104
1099
1105
1100
/**
1106
1101
* {@inheritDoc}
1107
1102
*/
1108
1103
@ Override
1109
1104
public Message request (String subject , Headers headers , byte [] body , Duration timeout ) throws InterruptedException {
1110
- return requestInternal (subject , headers , body , timeout , cancelAction );
1105
+ return requestInternal (subject , headers , body , timeout , cancelAction , true );
1111
1106
}
1112
1107
1113
1108
/**
@@ -1116,11 +1111,11 @@ public Message request(String subject, Headers headers, byte[] body, Duration ti
1116
1111
@ Override
1117
1112
public Message request (Message message , Duration timeout ) throws InterruptedException {
1118
1113
validateNotNull (message , "Message" );
1119
- return requestInternal (message .getSubject (), message .getHeaders (), message .getData (), timeout , cancelAction );
1114
+ return requestInternal (message .getSubject (), message .getHeaders (), message .getData (), timeout , cancelAction , false );
1120
1115
}
1121
1116
1122
- Message requestInternal (String subject , Headers headers , byte [] data , Duration timeout , CancelAction cancelAction ) throws InterruptedException {
1123
- CompletableFuture <Message > incoming = requestFutureInternal (subject , headers , data , timeout , cancelAction );
1117
+ Message requestInternal (String subject , Headers headers , byte [] data , Duration timeout , CancelAction cancelAction , boolean validateSubRep ) throws InterruptedException {
1118
+ CompletableFuture <Message > incoming = requestFutureInternal (subject , headers , data , timeout , cancelAction , validateSubRep );
1124
1119
try {
1125
1120
return incoming .get (timeout .toNanos (), TimeUnit .NANOSECONDS );
1126
1121
} catch (TimeoutException | ExecutionException | CancellationException e ) {
@@ -1133,31 +1128,31 @@ Message requestInternal(String subject, Headers headers, byte[] data, Duration t
1133
1128
*/
1134
1129
@ Override
1135
1130
public CompletableFuture <Message > request (String subject , byte [] body ) {
1136
- return requestFutureInternal (subject , null , body , null , cancelAction );
1131
+ return requestFutureInternal (subject , null , body , null , cancelAction , true );
1137
1132
}
1138
1133
1139
1134
/**
1140
1135
* {@inheritDoc}
1141
1136
*/
1142
1137
@ Override
1143
1138
public CompletableFuture <Message > request (String subject , Headers headers , byte [] body ) {
1144
- return requestFutureInternal (subject , headers , body , null , cancelAction );
1139
+ return requestFutureInternal (subject , headers , body , null , cancelAction , true );
1145
1140
}
1146
1141
1147
1142
/**
1148
1143
* {@inheritDoc}
1149
1144
*/
1150
1145
@ Override
1151
1146
public CompletableFuture <Message > requestWithTimeout (String subject , byte [] body , Duration timeout ) {
1152
- return requestFutureInternal (subject , null , body , timeout , cancelAction );
1147
+ return requestFutureInternal (subject , null , body , timeout , cancelAction , true );
1153
1148
}
1154
1149
1155
1150
/**
1156
1151
* {@inheritDoc}
1157
1152
*/
1158
1153
@ Override
1159
1154
public CompletableFuture <Message > requestWithTimeout (String subject , Headers headers , byte [] body , Duration timeout ) {
1160
- return requestFutureInternal (subject , headers , body , timeout , cancelAction );
1155
+ return requestFutureInternal (subject , headers , body , timeout , cancelAction , true );
1161
1156
}
1162
1157
1163
1158
/**
@@ -1166,7 +1161,7 @@ public CompletableFuture<Message> requestWithTimeout(String subject, Headers hea
1166
1161
@ Override
1167
1162
public CompletableFuture <Message > requestWithTimeout (Message message , Duration timeout ) {
1168
1163
validateNotNull (message , "Message" );
1169
- return requestFutureInternal (message .getSubject (), message .getHeaders (), message .getData (), timeout , cancelAction );
1164
+ return requestFutureInternal (message .getSubject (), message .getHeaders (), message .getData (), timeout , cancelAction , false );
1170
1165
}
1171
1166
1172
1167
/**
@@ -1175,10 +1170,10 @@ public CompletableFuture<Message> requestWithTimeout(Message message, Duration t
1175
1170
@ Override
1176
1171
public CompletableFuture <Message > request (Message message ) {
1177
1172
validateNotNull (message , "Message" );
1178
- return requestFutureInternal (message .getSubject (), message .getHeaders (), message .getData (), null , cancelAction );
1173
+ return requestFutureInternal (message .getSubject (), message .getHeaders (), message .getData (), null , cancelAction , false );
1179
1174
}
1180
1175
1181
- CompletableFuture <Message > requestFutureInternal (String subject , Headers headers , byte [] data , Duration futureTimeout , CancelAction cancelAction ) {
1176
+ CompletableFuture <Message > requestFutureInternal (String subject , Headers headers , byte [] data , Duration futureTimeout , CancelAction cancelAction , boolean validateSubRep ) {
1182
1177
checkPayloadSize (data );
1183
1178
1184
1179
if (isClosed ()) {
@@ -1230,7 +1225,7 @@ CompletableFuture<Message> requestFutureInternal(String subject, Headers headers
1230
1225
responsesAwaiting .put (sub .getSID (), future );
1231
1226
}
1232
1227
1233
- publishInternal (subject , responseInbox , headers , data );
1228
+ publishInternal (subject , responseInbox , headers , data , validateSubRep );
1234
1229
writer .flushBuffer ();
1235
1230
statistics .incrementRequestsSent ();
1236
1231
0 commit comments