use of org.apache.ratis.client.api.MessageOutputStream in project incubator-ratis by apache.
the class MessageStreamImpl method streamAsync.
@Override
public CompletableFuture<RaftClientReply> streamAsync(Message message, SizeInBytes subSize) {
final int n = subSize.getSizeInt();
final MessageOutputStream out = stream();
final ByteString bytes = message.getContent();
for (int i = 0; i < bytes.size(); ) {
final int j = Math.min(i + n, bytes.size());
final ByteString sub = bytes.substring(i, j);
out.sendAsync(Message.valueOf(sub));
i = j;
}
return out.closeAsync();
}
use of org.apache.ratis.client.api.MessageOutputStream in project incubator-ratis by apache.
the class MessageStreamApiTests method runTestStream.
void runTestStream(CLUSTER cluster) throws Exception {
RaftTestUtil.waitForLeader(cluster);
// stream multiple parts
final int numParts = 9;
final int endOfRequest = 6;
final StringBuilder key = new StringBuilder();
try (RaftClient client = cluster.createClient();
MessageOutputStream out = client.getMessageStreamApi().stream()) {
for (int i = 1; i <= numParts; i++) {
key.append(i);
out.sendAsync(new SimpleMessage(i + ""), i == endOfRequest);
}
}
// check if all the parts are streamed as a single message.
final String k = key.toString();
try (RaftClient client = cluster.createClient()) {
final String k1 = k.substring(0, endOfRequest);
final RaftClientReply r1 = client.io().sendReadOnly(new SimpleMessage(k1));
Assert.assertTrue(r1.isSuccess());
final String k2 = k.substring(endOfRequest);
final RaftClientReply r2 = client.io().sendReadOnly(new SimpleMessage(k2));
Assert.assertTrue(r2.isSuccess());
}
}
Aggregations