Skip to content

Instantly share code, notes, and snippets.

@parttimenerd
Created August 23, 2024 13:21
Show Gist options
  • Save parttimenerd/d66364f2086089761eb2fec7eda026d7 to your computer and use it in GitHub Desktop.
Save parttimenerd/d66364f2086089761eb2fec7eda026d7 to your computer and use it in GitHub Desktop.
import java.io.IOException;
import java.net.*;
import com.sun.net.httpserver.HttpServer;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpExchange;
class HttpRequestsExample {
private static HttpServer server;
public static void main(String[] args) throws Exception {
if (args.length != 1) {
System.err.println("Usage: java HttpRequestsExample <server|client>");
System.exit(1);
}
if ("server".equalsIgnoreCase(args[0])) {
startServer();
} else if ("client".equalsIgnoreCase(args[0])) {
runClient();
} else {
System.err.println("Invalid argument. Use 'server' or 'client'.");
System.exit(1);
}
}
// Method to start the HTTP server
private static void startServer() throws IOException {
server = HttpServer.create(new InetSocketAddress(8080), 0);
server.createContext("/fast", new DelayHandler(100)); // 100ms delay
server.createContext("/slow", new DelayHandler(1000)); // 1000ms delay
server.setExecutor(null); // creates a default executor
server.start();
System.out.println("Server started on port 8080");
}
// Method to run the client requests
private static void runClient() throws InterruptedException {
// Thread 1: Runs tenFastRequests
Thread thread1 = new Thread(HttpRequestsExample::tenFastRequests);
// Thread 2: Runs oneSlowRequest
Thread thread2 = new Thread(HttpRequestsExample::oneSlowRequest);
// Start both threads
thread1.start();
thread1.join();
thread2.start();
thread2.join();
System.out.println("All requests completed.");
}
// Method to make ten fast requests
private static void tenFastRequests() {
for (int i = 0; i < 10; i++) {
makeRequest("http://localhost:8080/fast"); // Local server endpoint
}
}
// Method to make one slow request
private static void oneSlowRequest() {
makeRequest("http://localhost:8080/slow"); // Local server endpoint
}
// Helper method to make an HTTP request using HttpURLConnection
private static void makeRequest(String urlString) {
HttpURLConnection connection = null;
try {
URL url = new URI(urlString).toURL();
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.getResponseCode();
} catch (IOException e) {
System.err.println("Request to " + urlString + " failed: " + e.getMessage());
} catch (URISyntaxException e) {
throw new RuntimeException(e);
} finally {
if (connection != null) {
connection.disconnect();
}
}
}
// Unified handler for both /fast and /slow endpoints
static class DelayHandler implements HttpHandler {
private final int delay;
public DelayHandler(int delay) {
this.delay = delay;
}
@Override
public void handle(HttpExchange exchange) throws IOException {
try {
Thread.sleep(delay); // Simulate processing time based on delay
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
exchange.sendResponseHeaders(200, 0);
exchange.close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment