0

I am trying to make a simple HTTP server that streams data to users. I would like the server listener to block main threads execution (take over the main thread):

public static void main(String [] args) throws Throwable {
    HttpServer server = HttpServer.create(new InetSocketAddress(80), 0);
    // The handler for connections
    server.createContext("/test", new MyHandler());
    // This is probably what needs tweaking
    server.setExecutor(null);
    // The start should block
    System.out.println("Server started at 0.0.0.0:80.");
    server.start();
    System.out.println("Server terminated without errors.");
}

The each connection will receive endless stream of data, so they need to be started in separate threads once they connect to the listening socket.

Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778
  • Is there a reason you need to do this other than curiosity? There are many well-tested Java HTTP servers out there, that do all that already. – Jochen Bedersdorfer Feb 17 '16 at 15:51
  • @JochenBedersdorfer I am writing a hackish plug-in therefore I'm trying to avoid additional worry of including other people's code while I have enough trouble including my *own code* in the running program. So no, there's no other reason than curiosity. But you have never ended up at unanswered/locked question that you really needed solution for but that was deemed useless at the time of asking? – Tomáš Zato Feb 17 '16 at 16:07
  • I assume `HttpServer.create(...)` is from the Oracle JDK? Here's a longer treatment about this and alternatives. http://stackoverflow.com/questions/3732109/simple-http-server-in-java-using-only-java-se-api – Jochen Bedersdorfer Feb 17 '16 at 16:17

1 Answers1

0

If you use the internal HttpServer package from com.sun.net.httpserver, you can set an java.util.concurrent.Executor to determine how threads are being used for MyHandler

Jochen Bedersdorfer
  • 4,093
  • 24
  • 26