Streamfork makes it possible to connect the input streaming data to a multi-source Outputstream directly!
Starting three server on localhost
from port 8050 to 8052:
int TEST_SERVER_COUNT = 3;
int PORT_RANGE = 8050;
ExecutorService executorService = Executors.newFixedThreadPool(TEST_SERVER_COUNT);
for (int i = 0; i < TEST_SERVER_COUNT; i++){
int finalI = i;
executorService.execute(() -> new SFServer().start("127.0.0.1", (PORT_RANGE + finalI), 100));
}
Let's create a temp file for sending to the servers:
File inputFile = File.createTempFile("temp", "txt");
FileWriter fileWriter = new FileWriter(inputFile);
PrintWriter printWriter = new PrintWriter(fileWriter);
printWriter.print("Lorem Ipsum is simply dummy text of the printing" +
" and typesetting industry.");
printWriter.close();
InputStream fileStream = new FileInputStream(inputFile);
//for read directly from stream
InputStream fileStream2 = new FileInputStream(inputFile);
int len;
byte[] data = null;
while ((len = fileStream.available()) > 0){
data = StreamReader.read(fileStream, len);
}
Let's connecting a client to the started servers:
SFClient client = SFClient.get(StreamMode.Parallel)
.addServer("127.0.0.1", 8050)
.addServer("127.0.0.1", 8051)
.addServer("127.0.0.1", 8052)
.setAutoClosable(true);
//read file content from byte array
String name = UUID.randomUUID().toString().substring(0, 16);
StreamBlock block = new StreamBlock(name, data);
client.write(block);
//or read from InputStream
String name2 = UUID.randomUUID().toString().substring(0, 16);
StreamBlock block2 = new StreamBlock(name2, fileStream2);
client.write(block2);
In the provided example, 3 servers are started and a client sends a file with a random fixed name length (16 char) to the servers and each server saves the file in the files
folder under the classpath directory.