NanoHTTPD仅处理一个并发请求

时间:2019-06-25 18:58:26

标签: java http web concurrency nanohttpd

我希望NanoHTTPD一次处理一个请求。有办法实现吗?

所有其他同时发出的请求应等待或删除。

这是我的NanoHTTPD服务器:

    package com.example;
    import java.io.IOException;
    import org.nanohttpd.NanoHTTPD;
    import java.util.concurrent.TimeUnit;

    public class App extends NanoHTTPD {

        public App() throws IOException {
            super(8080);
            start(NanoHTTPD.SOCKET_READ_TIMEOUT, false);
            System.out.println("Running!");
        }

        public static void main(String[] args) {
            try {
                new App();
            } catch (IOException ioe) {
                System.err.println("Couldn't start server:\n" + ioe);
            }
        }

        @Override
        public Response serve(IHTTPSession session) {
            try {
               TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {}

            String msg = "<html><body>Hello server</body></html>";
            return newFixedLengthResponse(msg);
        }
    }

0 个答案:

没有答案