我的 Google App Engine 应用程序使用 Web 服务,该 Web 服务响应速度非常慢,有时我的应用程序会崩溃:
java.net.SocketTimeoutException: Timeout while fetching URL: http://...
为了调用此 Web 服务,我使用 wsimport(用于解析现有 WSDL 文件并生成所需文件的 Java 工具)生成的类。
我需要更改此调用或全局所有应用 URL 提取的默认截止时间(5 秒)。
应用引擎文档:
You can set a deadline for a request, the most amount of time the service will wait for a response. By default, the deadline for a fetch is 5 seconds. The maximum deadline is 60 seconds for HTTP requests and 10 minutes for task queue and cron job requests. When using the URLConnection interface, the service uses the connection timeout (setConnectTimeout()) plus the read timeout (setReadTimeout()) as the deadline.
Source : https://developers.google.com/appengine/docs/java/urlfetch/#Java_Making_requests
我尝试在我的代码中添加此行(下面的强字)来更改截止日期,但它不起作用:
URL urlConnection = new URL(url);
URLConnection connection = urlConnection.openConnection();
connection.setConnectTimeout(180000); // 3 minutes
connection.setReadTimeout(180000); // 3 minutes
SWS webService = new SWS(urlConnection, new QName("http://...", "SWS"));
注意:SWS 是 wsimport 从我的 WSDL 生成的主类
最佳答案
一分钟前在此发布,因为也没有接受的答案:Can I globally set the timeout of HTTP connections?
对于具有 JAX-WS 的 App Engine,您必须设置请求上下文(今天使用 SDK 1.9.15 进行了测试)。对于普通机器,您不能超过 60 秒,并且必须切换到更大的机器 (Bx) 才能更好地使用任务队列。
对于本地测试,您通常会使用 BindingProviderProperties.CONNECT_TIMEOUT 和 BindingProviderProperties.REQUEST_TIMEOUT,但它们不在 App Engine JRE 白名单中,您的代码检查可能会不断向您发出警告。 不过可以使用等效的字符串:
com.sun.xml.internal.ws.connect.timeout
com.sun.xml.internal.ws.connect.timeout
对于部署到 App Engine:
com.sun.xml.ws.connect.timeout
com.sun.xml.ws.request.timeout
如何将其应用于 JAX-WS 2.x 自动生成的代码的完整示例,必须以毫秒为单位提供值:
@WebEndpoint(name = "Your.RandomServicePort")
public YourServiceInterface getYourRandomServicePort() {
YourRandomServiceInterface port = super.getPort(YOURRANDOMSERVICE_QNAME_PORT, YourRandomServiceInterface.class);
Map<String, Object> requestContext = ((BindingProvider)port).getRequestContext();
requestContext.put("com.sun.xml.ws.connect.timeout", 10000);
requestContext.put("com.sun.xml.ws.request.timeout", 10000);
return port;
}
关于java - 如何更改 Google 应用引擎上 Web 服务调用的默认超时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21484221/