Skip to content

Commit dabca74

Browse files
authored
Extract from main 2.11 branch things not specific to 2.11 Part 3 (#1243)
* Extract 2.11 changes part 3 * remove tests too
1 parent 1fbee9b commit dabca74

File tree

4 files changed

+4
-419
lines changed

4 files changed

+4
-419
lines changed

src/main/java/io/nats/client/JetStreamManagement.java

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import java.io.IOException;
1818
import java.time.ZonedDateTime;
1919
import java.util.List;
20-
import java.util.concurrent.LinkedBlockingQueue;
2120

2221
/**
2322
* JetStream Management context for creation and access to streams and consumers in NATS.
@@ -325,48 +324,6 @@ public interface JetStreamManagement {
325324
*/
326325
MessageInfo getNextMessage(String streamName, long seq, String subject) throws IOException, JetStreamApiException;
327326

328-
/**
329-
* Request a batch of messages using a {@link MessageBatchGetRequest}.
330-
* <p>
331-
* This API is currently EXPERIMENTAL and is subject to change.
332-
*
333-
* @param streamName the name of the stream
334-
* @param messageBatchGetRequest the request details
335-
* @return a list containing {@link MessageInfo}
336-
* @throws IOException covers various communication issues with the NATS
337-
* server such as timeout or interruption
338-
* @throws JetStreamApiException the request had an error related to the data
339-
*/
340-
List<MessageInfo> fetchMessageBatch(String streamName, MessageBatchGetRequest messageBatchGetRequest) throws IOException, JetStreamApiException;
341-
342-
/**
343-
* Request a batch of messages using a {@link MessageBatchGetRequest}.
344-
* <p>
345-
* This API is currently EXPERIMENTAL and is subject to change.
346-
*
347-
* @param streamName the name of the stream
348-
* @param messageBatchGetRequest the request details
349-
* @return a queue used to asynchronously receive {@link MessageInfo}
350-
* @throws IOException covers various communication issues with the NATS
351-
* server such as timeout or interruption
352-
* @throws JetStreamApiException the request had an error related to the data
353-
*/
354-
LinkedBlockingQueue<MessageInfo> queueMessageBatch(String streamName, MessageBatchGetRequest messageBatchGetRequest) throws IOException, JetStreamApiException;
355-
356-
/**
357-
* Request a batch of messages using a {@link MessageBatchGetRequest}.
358-
* <p>
359-
* This API is currently EXPERIMENTAL and is subject to change.
360-
*
361-
* @param streamName the name of the stream
362-
* @param messageBatchGetRequest the request details
363-
* @param handler the handler used for receiving {@link MessageInfo}
364-
* @throws IOException covers various communication issues with the NATS
365-
* server such as timeout or interruption
366-
* @throws JetStreamApiException the request had an error related to the data
367-
*/
368-
void requestMessageBatch(String streamName, MessageBatchGetRequest messageBatchGetRequest, MessageInfoHandler handler) throws IOException, JetStreamApiException;
369-
370327
/**
371328
* Deletes a message, overwriting the message data with garbage
372329
* This can be considered an expensive (time-consuming) operation, but is more secure.

src/main/java/io/nats/client/api/MessageInfo.java

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,6 @@
3232
*/
3333
public class MessageInfo extends ApiResponse<MessageInfo> {
3434

35-
/**
36-
* Message returned as a response in {@link MessageBatchGetRequest} to signal end of data.
37-
*/
38-
public static final MessageInfo EOD = new MessageInfo(null, false);
39-
4035
private final boolean direct;
4136
private final String subject;
4237
private final long seq;
@@ -45,7 +40,6 @@ public class MessageInfo extends ApiResponse<MessageInfo> {
4540
private final Headers headers;
4641
private final String stream;
4742
private final long lastSeq;
48-
private final long numPending;
4943

5044
/**
5145
* Create a Message Info
@@ -57,25 +51,6 @@ public MessageInfo(Message msg) {
5751
this(msg, null, false);
5852
}
5953

60-
/**
61-
* Create a Message Info
62-
* This signature is public for testing purposes and is not intended to be used externally.
63-
* @param error the error
64-
* @param direct true if the object is being created from a get direct api call instead of the standard get message
65-
*/
66-
public MessageInfo(Error error, boolean direct) {
67-
super(error);
68-
this.direct = direct;
69-
subject = null;
70-
data = null;
71-
seq = -1;
72-
time = null;
73-
headers = null;
74-
stream = null;
75-
lastSeq = -1;
76-
numPending = -1;
77-
}
78-
7954
/**
8055
* Create a Message Info
8156
* This signature is public for testing purposes and is not intended to be used externally.
@@ -102,14 +77,6 @@ public MessageInfo(Message msg, String streamName, boolean direct) {
10277
else {
10378
lastSeq = JsonUtils.safeParseLong(tempLastSeq, -1);
10479
}
105-
String tempNumPending = msgHeaders.getLast(NATS_NUM_PENDING);
106-
if (tempNumPending == null) {
107-
numPending = -1;
108-
}
109-
else {
110-
// Num pending is +1 since it includes EOB message, correct that here.
111-
numPending = Long.parseLong(tempNumPending) - 1;
112-
}
11380
// these are control headers, not real headers so don't give them to the user.
11481
headers = new Headers(msgHeaders, true, MESSAGE_INFO_HEADERS);
11582
}
@@ -121,7 +88,6 @@ else if (hasError()) {
12188
headers = null;
12289
stream = null;
12390
lastSeq = -1;
124-
numPending = -1;
12591
}
12692
else {
12793
JsonValue mjv = readValue(jv, MESSAGE);
@@ -133,7 +99,6 @@ else if (hasError()) {
13399
headers = hdrBytes == null ? null : new IncomingHeadersProcessor(hdrBytes).getHeaders();
134100
stream = streamName;
135101
lastSeq = -1;
136-
numPending = -1;
137102
}
138103
}
139104

@@ -193,14 +158,6 @@ public long getLastSeq() {
193158
return lastSeq;
194159
}
195160

196-
/**
197-
* Amount of pending messages that can be requested with a subsequent batch request.
198-
* @return number of pending messages
199-
*/
200-
public long getNumPending() {
201-
return numPending;
202-
}
203-
204161
@Override
205162
public String toString() {
206163
StringBuilder sb = JsonUtils.beginJsonPrefixed("\"MessageInfo\":");
@@ -217,7 +174,6 @@ public String toString() {
217174
JsonUtils.addField(sb, TIME, time);
218175
JsonUtils.addField(sb, STREAM, stream);
219176
JsonUtils.addField(sb, LAST_SEQ, lastSeq);
220-
JsonUtils.addField(sb, NUM_PENDING, numPending);
221177
JsonUtils.addField(sb, SUBJECT, subject);
222178
JsonUtils.addField(sb, HDRS, headers);
223179
return JsonUtils.endJson(sb).toString();

src/main/java/io/nats/client/impl/NatsJetStreamManagement.java

Lines changed: 0 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,12 @@
1616
import io.nats.client.*;
1717
import io.nats.client.api.Error;
1818
import io.nats.client.api.*;
19-
import io.nats.client.support.Status;
2019

2120
import java.io.IOException;
2221
import java.nio.charset.StandardCharsets;
2322
import java.time.ZonedDateTime;
24-
import java.util.ArrayList;
2523
import java.util.List;
26-
import java.util.concurrent.LinkedBlockingQueue;
2724

28-
import static io.nats.client.support.NatsJetStreamClientError.JsAllowDirectRequired;
29-
import static io.nats.client.support.NatsJetStreamClientError.JsDirectBatchGet211NotAvailable;
3025
import static io.nats.client.support.Validator.*;
3126

3227
public class NatsJetStreamManagement extends NatsJetStreamImpl implements JetStreamManagement {
@@ -345,109 +340,6 @@ private MessageInfo _getMessage(String streamName, MessageGetRequest messageGetR
345340
}
346341
}
347342

348-
/**
349-
* {@inheritDoc}
350-
*/
351-
@Override
352-
public List<MessageInfo> fetchMessageBatch(String streamName, MessageBatchGetRequest messageBatchGetRequest) throws IOException, JetStreamApiException {
353-
validateMessageBatchGetRequest(streamName, messageBatchGetRequest);
354-
List<MessageInfo> results = new ArrayList<>();
355-
_requestMessageBatch(streamName, messageBatchGetRequest, msg -> {
356-
if (msg != MessageInfo.EOD) {
357-
results.add(msg);
358-
}
359-
});
360-
return results;
361-
}
362-
363-
/**
364-
* {@inheritDoc}
365-
*/
366-
@Override
367-
public LinkedBlockingQueue<MessageInfo> queueMessageBatch(String streamName, MessageBatchGetRequest messageBatchGetRequest) throws IOException, JetStreamApiException {
368-
validateMessageBatchGetRequest(streamName, messageBatchGetRequest);
369-
final LinkedBlockingQueue<MessageInfo> q = new LinkedBlockingQueue<>();
370-
conn.getOptions().getExecutor().submit(() -> _requestMessageBatch(streamName, messageBatchGetRequest, q::add));
371-
return q;
372-
}
373-
374-
/**
375-
* {@inheritDoc}
376-
*/
377-
@Override
378-
public void requestMessageBatch(String streamName, MessageBatchGetRequest messageBatchGetRequest, MessageInfoHandler handler) throws IOException, JetStreamApiException {
379-
validateMessageBatchGetRequest(streamName, messageBatchGetRequest);
380-
_requestMessageBatch(streamName, messageBatchGetRequest, handler);
381-
}
382-
383-
public void _requestMessageBatch(String streamName, MessageBatchGetRequest messageBatchGetRequest, MessageInfoHandler handler) {
384-
Subscription sub = null;
385-
try {
386-
String replyTo = conn.createInbox();
387-
sub = conn.subscribe(replyTo);
388-
389-
String requestSubject = prependPrefix(String.format(JSAPI_DIRECT_GET, streamName));
390-
conn.publish(requestSubject, replyTo, messageBatchGetRequest.serialize());
391-
392-
long maxTimeMillis = getTimeout().toMillis();
393-
long timeLeft = maxTimeMillis;
394-
long start = System.currentTimeMillis();
395-
while (true) {
396-
Message msg = sub.nextMessage(timeLeft);
397-
if (msg == null) {
398-
break;
399-
}
400-
if (msg.isStatusMessage()) {
401-
Status status = msg.getStatus();
402-
// Report error, otherwise successful status.
403-
if (status.getCode() < 200 || status.getCode() > 299) {
404-
MessageInfo messageInfo = new MessageInfo(Error.convert(status), true);
405-
handler.onMessageInfo(messageInfo);
406-
}
407-
break;
408-
}
409-
410-
Headers headers = msg.getHeaders();
411-
if (headers == null || headers.getLast(NATS_NUM_PENDING) == null) {
412-
throw JsDirectBatchGet211NotAvailable.instance();
413-
}
414-
415-
MessageInfo messageInfo = new MessageInfo(msg, streamName, true);
416-
handler.onMessageInfo(messageInfo);
417-
timeLeft = maxTimeMillis - (System.currentTimeMillis() - start);
418-
}
419-
} catch (InterruptedException e) {
420-
// sub.nextMessage was fetching one message
421-
// and data is not completely read
422-
// so it seems like this is an error condition
423-
Thread.currentThread().interrupt();
424-
throw new RuntimeException(e);
425-
} finally {
426-
try {
427-
handler.onMessageInfo(MessageInfo.EOD);
428-
} catch (Exception ignore) {
429-
}
430-
try {
431-
//noinspection DataFlowIssue
432-
sub.unsubscribe();
433-
} catch (Exception ignore) {
434-
}
435-
}
436-
}
437-
438-
private void validateMessageBatchGetRequest(String streamName, MessageBatchGetRequest messageBatchGetRequest) throws IOException, JetStreamApiException {
439-
validateNotNull(messageBatchGetRequest, "Message Batch Get Request");
440-
441-
if (!directBatchGet211Available) {
442-
throw JsDirectBatchGet211NotAvailable.instance();
443-
}
444-
445-
CachedStreamInfo csi = getCachedStreamInfo(streamName);
446-
if (!csi.allowDirect) {
447-
throw JsAllowDirectRequired.instance();
448-
}
449-
}
450-
451343
/**
452344
* {@inheritDoc}
453345
*/

0 commit comments

Comments
 (0)