我有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();
}
但是在到达队列上执行请求。
如何使服务器多线程?这样,下一个请求就不会等待上一个请求完成。
下一行解决了多线程问题:
httpsServer.setExecutor (Executors.newCachedThreadPool ());
这必须在之前指定
httpsServer.start ();