Extrait du course scalable IO in Java
Outline
-Scalable network services
-Event-driven processing
-Reactor pattern
Basic version
Multithreaded versions
Other variants
-Walkthrough of java.nio nonblocking IO APIs.
Network Services
-Web services, Distributed Objects, etc
-Most have same basic structure:
Read request
Decode request
Process service
Encode reply
Send reply
-But differ in nature and cost of each step
XML parsing, File transfer, Web page
generation, computational services, …
Classic ServerSocket Loop
class Server implements Runnable {
public void run() {
try {
ServerSocket ss = new ServerSocket(PORT);
while (!Thread.interrupted())
new Thread(new Handler(ss.accept())).start();
// or, single-threaded, or a thread pool
} catch (IOException ex) { /* … */ }
}
static class Handler implements Runnable {
final Socket socket;
Handler(Socket s) { socket = s; }
public void run() {
try {
byte[] input = new byte[MAX_INPUT];
socket.getInputStream().read(input);
byte[] output = process(input);
socket.getOutputStream().write(output);
} catch (IOException ex) { /* … */ }
}
private byte[] process(byte[] cmd) { /* … */ }
}
}
Note: most exception handling elided from code examples
Scalability Goals
-Graceful degradation under increasing load (more clients)
-Continuous improvement with increasing resources (CPU, memory, disk, bandwidth)
-Also meet availability and performance goals
Short latencies
Meeting peak demand
Tunable quality of service
-Divide-and-conquer is usually the best approach for achieving any scalability goal
…….
Course scalable IO in Java scalable network services (599 KO) (Cours PDF)