conryyy
conryyy

Reputation: 127

How to set the Duration of a JAVA SSL Connection

I set my Java System Properties for my secure SSL Connection like this:

System.setProperty("https.protocols", "TLSv1.2")
System.setProperty("javax.net.ssl.keyStoreType", "PKCS12")
System.setProperty("javax.net.ssl.keyStore",keyStore)
System.setProperty("javax.net.ssl.keyStorePassword", keyStorePW)
System.setProperty("javax.net.ssl.trustStore",trustStore)
System.setProperty("javax.net.ssl.trustStorePassword", trustStorePW) 

Now I do Something like this:

If "doing other stuff" takes longer then 5 sec, the whole SSL Handshake (Server Hello, Client hello etc.) will we done again. If "doing other stuff" takes less then 5 sec, the request will be send immediately

--> How Can I set this duration longer then 5 sec?

EDIT:

This is how I do my SOAP call:

static String callSoap() {

       SOAPMessage request = //..creating request
        
       SOAPMessage response=dispatch.invoke(request)

       SOAPBody responseBody=response.getSOAPBody()

   .......
   
   return....
  }

Upvotes: 0

Views: 684

Answers (1)

aran
aran

Reputation: 11860

When you call socket.connect(), you can specify there the desired timeout. F.e:

int timeout = 5000 * 3;
socket.setSoTimeout(timeout);
socket.connect(new InetSocketAddress(hostAddress, port), timeout);

SoTimeout may not be needed; This timeout is the time a read() call will block before throwing a exception. You can set it to 0 if you don't wish any timeout reading, and you accept just waiting until a byte is read.

This should only try to reconnect when it takes more than 15 seconds to finish the process.


Ok, int the SOAP world,something like this should do the trick:

SOAPConnection connection = SOAPConnectionFactory.newInstance().createConnection();
URL endpoint =
  new URL(new URL("http://yourserver.yourdomain.com/"),
          "/path/to/webservice",
          new URLStreamHandler() {
            @Override
            protected URLConnection openConnection(URL url) throws IOException {
              URL target = new URL(url.toString());
              URLConnection connection = target.openConnection();
              // Connection settings
              connection.setConnectTimeout(10000); // 10 sec
              connection.setReadTimeout(60000); // 1 min
              return(connection);
            }
          });

SOAPMessage result = connection.call(soapMessage, endpoint);

Take a look here for more info, may be helpful.

Upvotes: 1

Related Questions