Dea*_*lze 5 java jdk1.6 com.sun.net.httpserver
嵌入在JDK 6中的Http Server是开发Web服务的一个很大的帮助,但是我遇到了我发布了一个Endpoint然后代码崩溃并让服务器运行的情况.
一旦丢失对它的引用(或已发布的端点),如何关闭嵌入式服务器?
我使用下面的代码来启动它
this.httpServer = HttpServer.create(addr, 0);
HttpContext context = this.httpServer.createContext("/", new DocumentProcessHandler());
this.httpThreadPool = Executors.newFixedThreadPool(this.noOfThreads);
this.httpServer.setExecutor(this.httpThreadPool);
this.httpServer.start();
Run Code Online (Sandbox Code Playgroud)
以下代码来阻止它
this.httpServer.stop(1);
this.httpThreadPool.shutdownNow();
Run Code Online (Sandbox Code Playgroud)
我以前从未使用过这个服务器,也找不到任何好的文档。也许您已经想到了这些不太优雅的解决方案,但我只是想提一下它们。
似乎 com.sun.net.httpserver.HttpServer 有一个名为 HttpServerImpl 的实现类。它有一个名为 stop() 的方法。
或者你可以找到监听服务器套接字的线程并调用interrupt()。
肖恩