0

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.

5
  • 2
    The simplest answer would probably be to not implement your own server in Java and instead use a normal, mature http server, with the most obvious candidate being Apache. Having said that: you're unlikely to be the first person in the world to want this, did you search the web for solutions here, first? What did you find? What happened when you tried those solutions? (remember that you're asking real people for help, please remember to help them by talking about what you already did to try to make things work before you decided to post to SO) Commented Aug 3, 2020 at 17:01
  • 2
    I believe this is relevant: docs.oracle.com/en/java/javase/14/docs/api/jdk.httpserver/com/… Commented Aug 3, 2020 at 17:07
  • I was looking for an answer to my own question before asking it. I have not found an answer in Oracle documentation or similar questions on stackoverflow platform. Thanks for Apache. Commented Aug 3, 2020 at 17:15
  • Mr. Slaw. You're right. This helped to fix the problem, thanks. I must mark your answer as correct. Commented Aug 3, 2020 at 17:27
  • I only provided a hint to the solution. Consider adding your own answer since you implemented the actual solution. Commented Aug 3, 2020 at 23:21

1 Answer 1

1

The next line solves the multithreading problem:

httpsServer.setExecutor (Executors.newCachedThreadPool ());

This must be specified before:

httpsServer.start ();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.