jdk6-webservice超时设立

jdk6-webservice超时设置

在开发webservice客户端的代码中,必须需要设置timeout与connection-timeout两个参数,因为很多时候客户端的应用不可能一直阻塞的等待服务端的响应。下面就针对jdk6开发的webservice客户端,对于上面两个参数的设置做一个说明:

一、直接用jdk中的java.net.URLConnection(也可以是java.net.HttpURLConnection)做客户端的开发。

			URL url = new URL(urlString);
			URLConnection conn = url.openConnection();
			conn.setUseCaches(false);
			conn.setDoInput(true);
			conn.setDoOutput(true);
			conn.setConnectTimeout(connectTimeout * Millisecond);
			conn.setReadTimeout(reaquestTimeout * Millisecond);

 connectTimeout * Millisecond和reaquestTimeout * Millisecond:Millisecond=1000(表示1000毫秒的意思)

connectTimeout和reaquestTimeout是设置的时间数。

 

二、基于jdk6的wsimport命令通过wsdl文件构建的客户端(见本博客http://xm-koma.iteye.com/blog/1605800):

			HuaXiaServiceImplPortType hxip = (HuaXiaServiceImplPortType) hxs
					.getHuaXiaServiceImplPort();
			Map<String, Object> ctxt = ((BindingProvider) hxip)
					.getRequestContext();
			ctxt.put("com.sun.xml.internal.ws.connect.timeout", connectTimeout
					* Millisecond);
			ctxt.put("com.sun.xml.internal.ws.request.timeout", reaquestTimeout
					* Millisecond);

 

com.sun.xml.internal.ws.connect.timeout和com.sun.xml.internal.ws.request.timeout两个参数来自com.sun.xml.internal.ws.developer.JAXWSProperties这个类中的字符串常量。但由于很多小版本号较低的jdk6中没有这个类,所以写成字符串也是可行的。

 

另外可参考下面的文章:

http://*.com/questions/3130913/setting-jax-ws-client-timeout
http://androidyo.iteye.com/blog/624015
http://www.blogjava.net/supercrsky/articles/247449.html