Setting socket read timeout with javax.xml.soap.SOAPConnection

↘锁芯ラ 提交于 2019-11-27 14:23:11

You have to create your own URLStreamHandler so that you can set URLConnection parameters like connection timeout and read timeout.

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

I have removed some try/catch for clarity.

import com.sun.xml.internal.ws.client.BindingProviderProperties

public someResponse callWebService() {

    MyPort port = new Service().getPort();

    Map<String, Object> requestContext = ((BindingProvider) port).getRequestContext();

    requestContext.put(BindingProviderProperties.CONNECT_TIMEOUT, 10 * 1000); //10 secs

    requestContext.put(BindingProviderProperties.REQUEST_TIMEOUT, 1 * 60 * 1000); //1 min

    return port.someWebMethod();

}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!