我们的Jersey客户端从服务器获取以下例外。
Got exception from API:POST http://my-host.com/api/x/y returned a response status of 504 Gateway Timeout, Stack trace: com.sun.jersey.api.client.UniformInterfaceException: POST http://my-host.com/api/x/y returned a response status of 504 Gateway Timeout
at com.sun.jersey.api.client.WebResource.handle(WebResource.java:698)
at com.sun.jersey.api.client.WebResource.access$300(WebResource.java:74)
at com.sun.jersey.api.client.WebResource$Builder.post(WebResource.java:574)
at com.g.e.s.i.t.XYZ.callResource(ABC.java:84)
at ...
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
这是客户端创建代码:
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
JacksonJaxbJsonProvider provider = new JacksonJaxbJsonProvider();
provider.setMapper(mapper);
ClientConfig clientConfig = new DefaultClientConfig();
clientConfig.getSingletons().add(provider);
Client client = Client.create(clientConfig);
client.setConnectTimeout(Integer.MAX_VALUE);
client.setReadTimeout(Integer.MAX_VALUE);
return client.resource(url);
因此,我们尝试为客户端设置最大超时但没有帮助。
任何人都知道如何配置客户端超时?
可能是更好地处理Tomcat超时? (怎么样?)虽然我认为它落后于一些负载均衡器。
答案 0 :(得分:2)
尝试使用以下代码设置客户端读取时间。
clientConfig.getProperties().put(ClientConfig.PROPERTY_READ_TIMEOUT,30000);//30 seconds read timeout
尝试使用以下代码设置客户端连接超时。
clientConfig.getProperties().put(ClientConfig.PROPERTY_CONNECT_TIMEOUT,30000);//30 seconds for connection timeout