Created
July 12, 2022 15:54
-
-
Save ebarlas/b432f589246eae00e8a7cf7e680c367a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.microhttp; | |
import java.io.IOException; | |
import java.util.List; | |
import java.util.concurrent.Executors; | |
import java.util.concurrent.ScheduledExecutorService; | |
import java.util.concurrent.TimeUnit; | |
public class ConcurrencyServer { | |
public static void main(String[] args) throws IOException { | |
ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1); | |
Response response = new Response( | |
200, | |
"OK", | |
List.of(new Header("Content-Type", "text/plain")), | |
"hello world\n".getBytes()); | |
Options options = new Options() | |
.withHost("0.0.0.0") | |
.withAcceptLength(args.length > 0 ? Integer.parseInt(args[0]) : 0) | |
.withConcurrency(args.length > 1 ? Integer.parseInt(args[1]) : Runtime.getRuntime().availableProcessors()); | |
Logger logger = new NoOpLogger(); | |
EventLoop eventLoop = new EventLoop(options, logger, (req, callback) -> executorService.schedule(() -> callback.accept(response), 1, TimeUnit.SECONDS)); | |
eventLoop.start(); | |
} | |
static class NoOpLogger implements Logger { | |
@Override | |
public boolean enabled() { | |
return false; | |
} | |
@Override | |
public void log(LogEntry... entries) { | |
} | |
@Override | |
public void log(Exception e, LogEntry... entries) { | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment