Skip to content

Instantly share code, notes, and snippets.

@ebarlas
Created February 12, 2022 21:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ebarlas/42aa8ce6ced6573b46a3e4e36d312535 to your computer and use it in GitHub Desktop.
Save ebarlas/42aa8ce6ced6573b46a3e4e36d312535 to your computer and use it in GitHub Desktop.
package test;
import microhttp.EventLoop;
import microhttp.Header;
import microhttp.LogEntry;
import microhttp.Logger;
import microhttp.Options;
import microhttp.Response;
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(args[0])
.withAcceptLength(Integer.parseInt(args[1]));
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