我已经在Java中建立了一个HttpsServer。我所有的沟通都很完美。我设置了多个上下文,加载自签名证书,甚至基于外部配置文件启动。Java HttpsServer多线程
我现在的问题是让多个客户端能够打我的安全服务器。为此,我想以某种方式对来自HttpsServer的请求进行多线程处理,但无法弄清楚如何执行此操作。以下是我的基本HttpsConfiguration。
HttpsServer server = HttpsServer.create(new InetSocketAddress(secureConnection.getPort()), 0);
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(secureConnection.getKeyManager().getKeyManagers(), secureConnection.getTrustManager().getTrustManagers(), null);
server.setHttpsConfigurator(new SecureServerConfiguration(sslContext));
server.createContext("/", new RootHandler());
server.createContext("/test", new TestHandler());
server.setExecutor(Executors.newCachedThreadPool());
server.start();
其中secureConnection是一个包含服务器设置和证书信息的自定义类。
我试图将执行器设置为Executors.newCachedThreadPool()
和其他几个。但是,他们都产生了相同的结果。每个人都以不同的方式管理线程,但第一个请求必须在第二个请求处理之前完成。
我也试着写我自己的遗嘱执行人
public class AsyncExecutor extends ThreadPoolExecutor implements Executor
{
public static Executor create()
{
return new AsyncExecutor();
}
public AsyncExecutor()
{
super(5, 10, 10000, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(12));
}
@Override
public void execute(Runnable process)
{
System.out.println("New Process");
Thread newProcess = new Thread(process);
newProcess.setDaemon(false);
newProcess.start();
System.out.println("Thread created");
}
}
不幸的是,相同的结果,其他遗嘱执行人。
测试我正在使用Postman通过执行Thread.sleep(10000)
命中模拟长时间运行任务的/ Test端点。在运行时,我正在使用我的Chrome浏览器访问根端点。直到10秒睡眠结束,根页面才会加载。
有关如何处理多个并发请求到HTTPS服务器的任何想法?
为了便于测试,我使用标准HttpServer复制了我的场景,并将所有内容压缩为一个Java程序。
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.util.concurrent.Executors;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
public class Example
{
private final static int PORT = 80;
private final static int BACKLOG = 10;
/**
* To test hit:
* <p><b>http://localhost/test</b></p>
* <p>This will hit the endoint with the thread sleep<br>
* Then hit:</p>
* <p><b>http://localhost</b></p>
* <p>I would expect this to come back right away. However, it does not come back until the
* first request finishes. This can be tested with only a basic browser.</p>
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception
{
new Example().start();
}
private void start() throws Exception
{
HttpServer server = HttpServer.create(new InetSocketAddress(PORT), BACKLOG);
server.createContext("/", new RootHandler());
server.createContext("/test", new TestHandler());
server.setExecutor(Executors.newCachedThreadPool());
server.start();
System.out.println("Server Started on " + PORT);
}
class RootHandler implements HttpHandler
{
@Override
public void handle(HttpExchange httpExchange) throws IOException
{
String body = "<html>Hello World</html>";
httpExchange.sendResponseHeaders(200, body.length());
OutputStream outputStream = httpExchange.getResponseBody();
outputStream.write(body.getBytes("UTF-8"));
outputStream.close();
}
}
class TestHandler implements HttpHandler
{
@Override
public void handle(HttpExchange httpExchange) throws IOException
{
try
{
Thread.sleep(10000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
String body = "<html>Test Handled</html>";
httpExchange.sendResponseHeaders(200, body.length());
OutputStream outputStream = httpExchange.getResponseBody();
outputStream.write(body.getBytes("UTF-8"));
outputStream.close();
}
}
}
你可能需要添加一些源代码展示了如何在安装了港口装卸和实际处理个别请求在你的http服务器中:对于我们所知道的,你一次只能接受一个套接字。 – Femi
我怎么能改变HttpsServer接受多个套接字? – cain4355
它已经做到了。你如何处理接受的套接字? – EJP