如何创建多线程的Java HttpsServer?

huangapple go评论102阅读模式
英文:

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 ();

huangapple
  • 本文由 发表于 2020年8月4日 00:47:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/63233602.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定