cai*_*355 5 java https multithreading http threadpoolexecutor
我已经在 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();
Run Code Online (Sandbox Code Playgroud)
其中 secureConnection 是包含服务器设置和证书信息的自定义类。
我试图将 executor 设置为Executors.newCachedThreadPool()和其他几个。然而,它们都产生了相同的结果。每个人都以不同的方式管理线程,但第一个请求必须在第二个可以处理之前完成。
我也尝试编写自己的 Executor
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");
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,结果与其他 Executor 相同。
为了进行测试,我使用 Postman 来访问 /Test 端点,该端点通过执行Thread.sleep(10000). 在运行时,我使用 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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
尝试指定非零的最大积压( 的第二个参数create()):
HttpsServer server = HttpsServer.create(new InetSocketAddress(secureConnection.getPort()), 10);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1499 次 |
| 最近记录: |