как создать Java HttpsServer многопоточным? - PullRequest
0 голосов
/ 03 августа 2020

У меня 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();
    }

, но запросы выполняются в очереди прибытия.

Как сделать сервер многопоточным? Чтобы последующий запрос не ждал завершения предыдущего.

1 Ответ

1 голос
/ 04 августа 2020

Следующая строка решает проблему многопоточности:

httpsServer.setExecutor (Executors.newCachedThreadPool ());

Это должно быть указано перед:

httpsServer.start ();
...