英文:
how create Java HttpsServer multi threaded?
问题
我有一个HttpsServer:
public void startHttpsServer() {
try {
httpsServer = com.sun.net.httpserver.HttpsServer.create();
httpsServer.bind(new InetSocketAddress(httpsPort), 0);
httpsServer.createContext("/getVersion", new VersionHandler());
httpsServer.createContext("/sysInfo", new SysInfoHandler());
char[] storepass = "pass".toCharArray();
char[] keypass = "pass".toCharArray();
KeyStore ks = KeyStore.getInstance("JKS");
ks.load(HttpServer.class.getResourceAsStream("/keystore.jks"), storepass);
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
kmf.init(ks, keypass);
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
tmf.init(ks);
SSLContext ssl = SSLContext.getInstance("TLS");
ssl.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
httpsServer.setHttpsConfigurator(new HttpsConfigurator(ssl) {
public void configure(HttpsParameters params) {
SSLContext context = getSSLContext();
SSLEngine engine = context.createSSLEngine();
params.setNeedClientAuth(false);
params.setCipherSuites(engine.getEnabledCipherSuites());
params.setProtocols(engine.getEnabledProtocols());
// Set the SSL parameters
SSLParameters sslParameters = context.getSupportedSSLParameters();
params.setSSLParameters(sslParameters);
}
});
} catch (IOException e) {
e.printStackTrace();
Logger.addLogLine("HttpServerError", e.toString(), e);
} catch (CertificateException | NoSuchAlgorithmException | KeyManagementException | KeyStoreException | UnrecoverableKeyException e) {
e.printStackTrace();
}
httpsServer.start();
}
但请求在到达队列上执行。
如何使服务器多线程?以便后续请求不必等待前一个请求完成。
英文:
I have HttpsServer:
public void startHttpsServer() {
try {
httpsServer = com.sun.net.httpserver.HttpsServer.create();
httpsServer.bind(new InetSocketAddress(httpsPort), 0);
httpsServer.createContext("/getVersion", new VersionHandler());
httpsServer.createContext("/sysInfo", new SysInfoHandler());
char[] storepass = "pass".toCharArray();
char[] keypass = "pass".toCharArray();
KeyStore ks = KeyStore.getInstance("JKS");
ks.load(HttpServer.class.getResourceAsStream("/keystore.jks"), storepass);
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
kmf.init(ks, keypass);
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
tmf.init(ks);
SSLContext ssl = SSLContext.getInstance("TLS");
ssl.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
httpsServer.setHttpsConfigurator(new HttpsConfigurator(ssl) {
public void configure(HttpsParameters params) {
SSLContext context = getSSLContext();
SSLEngine engine = context.createSSLEngine();
params.setNeedClientAuth(false);
params.setCipherSuites(engine.getEnabledCipherSuites());
params.setProtocols(engine.getEnabledProtocols());
// Set the SSL parameters
SSLParameters sslParameters = context.getSupportedSSLParameters();
params.setSSLParameters(sslParameters);
}
});
} catch (IOException e) {
e.printStackTrace();
Logger.addLogLine("HttpServerError", e.toString(), e);
} catch (CertificateException | NoSuchAlgorithmException | KeyManagementException | KeyStoreException | UnrecoverableKeyException e) {
e.printStackTrace();
}
httpsServer.start();
}
but requests are executed on the arrival queue.
How to make the server multithreaded? So that the subsequent request does not wait for the previous one to complete.
答案1
得分: 1
以下是已翻译的内容:
下一行解决了多线程问题:
httpsServer.setExecutor(Executors.newCachedThreadPool());
这必须在以下指定之前:
httpsServer.start();
英文:
The next line solves the multithreading problem:
httpsServer.setExecutor (Executors.newCachedThreadPool ());
This must be specified before:
httpsServer.start ();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论