使用javax.xml.soap.SOAPConnection设置套接字读取超时
我正在使用javax.xml.soap
API( javax.xml.soap.SOAPConnectionFactory
, javax.xml.soap.SOAPConnection
和朋友)对远程服务器进行Web服务调用,但大部分都取得了巨大成功。
但是,有时会出现问题,程序会永远陷入困境。
为了解决这个问题,我想添加一个读取超时。
我找到了几种可能实现这一目标的方法,但它们看起来都非常糟糕。
所以我对社区的问题是:在使用javax.xml.soap API进行调用时,实现读取超时行为的最佳方法是什么?
您必须创建自己的URLStreamHandler,以便可以设置URLConnection参数,如连接超时和读取超时。
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);
为清楚起见,我删除了一些try / catch。
import com.sun.xml.internal.ws.client.BindingProviderProperties public someResponse callWebService() { MyPort port = new Service().getPort(); Map 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(); }
- 无法使用java将byte 插入MySQL
- MalformedByteSequenceException:2字节UTF-8序列的无效字节2
- 这两个java变量声明有什么区别?
- @WebServlet annotation web.xml welcome-file
- 如果执行顺序(几乎)不受影响,如何在严重的性能下降中分配变量结果?
- Ant目标失败:Antlib还是Ivy问题?
- Java / Eclipse:如何为JUnit测试配置Run Configuration的Classpath?
- 如何在使用Java Web Services时访问HttpServletRequest对象
- 如何在默认情况下指示成员字段是@Nonnull?