SocketException:连接重置 - 客户端Jersey 2.0 Java 7

时间:2018-01-30 16:04:42

标签: java rest ssl jersey timeout

我收到类似的错误link1。 当我通过REST发布(客户端到服务器)一个小的xml时,一切正常。不幸的是,当我发布一些更大的xmls时,我正在收到错误,当连接持续时间超过10/15分钟时。 (我认为这是某种超时)

我更正了SSL证书,因为它在link1中提到 - configureClient()方法在我的情况下与link1中的解决方案相同。

我还添加了System.setProperty(“java.net.preferIPv4Stack”,“true”); - 有时它解决了连接重置

基本信息:

错误:javax.ws.rs.ProcessingException:java.net.SocketException:连接重置

方法:REST POST

Java版本:7

引擎:泽西2.x

方:客户

系统:Windows 7

我的客户:

   System.setProperty("java.net.preferIPv4Stack" , "true");

RestMethodes restMethodes = new RestMethodes();
ClientConfig config = new ClientConfig();
config = config.property(ClientProperties.CONNECT_TIMEOUT, 0);
config = config.property(ClientProperties.READ_TIMEOUT, 0);

config.property(ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION, true);

Client client = configureClient(config);
client.register(HttpAuthenticationFeature.basic(USER, PASS));

WebTarget target = client.target(SERVER_URL + "/bundles/assets");
Invocation.Builder responseInvocation = target.request(MediaType.APPLICATION_JSON);

// My exception is thrown there

Response response = responseInvocation.post(Entity.xml(assetsString)); 

String entity = response.readEntity(String.class);
//entity = jsonPrettyPrinter(entity);

if (entity.isEmpty() || entity.equals("")) {
    log.info("[POST ASSETS] Failed : Response is empty");
}
Response.StatusType codeName = response.getStatusInfo();
int code = response.getStatus();

if (code != 200) {
    log.error("[POST ASSETS] Failed : HTTP error code : " + response.getStatus() + "\n" + entity);
    response.close();
} else {
    log.info("[POST ASSETS] RESPONSE CODE ID : " + code + " CODE NAME : " + codeName);
    log.info("[POST ASSETS] RESPONSE : " + entity);
    response.close();
}
client.close();

我的错误

Exception in thread "main" javax.ws.rs.ProcessingException: java.net.SocketException: Connection reset
at org.glassfish.jersey.client.internal.HttpUrlConnector.apply(HttpUrlConnector.java:287)
at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:252)
at org.glassfish.jersey.client.JerseyInvocation$1.call(JerseyInvocation.java:684)
at org.glassfish.jersey.client.JerseyInvocation$1.call(JerseyInvocation.java:681)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:444)
at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:681)
at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:437)
at org.glassfish.jersey.client.JerseyInvocation$Builder.post(JerseyInvocation.java:343)
at com.sas.spl.saslineagebridges.test.PostTester.main(PostTester.java:61)
Caused by: java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:209)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at sun.security.ssl.InputRecord.readFully(InputRecord.java:465)
at sun.security.ssl.InputRecord.read(InputRecord.java:503)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:973)
at sun.security.ssl.SSLSocketImpl.readDataRecord(SSLSocketImpl.java:930)
at sun.security.ssl.AppInputStream.read(AppInputStream.java:105)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:246)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:286)
at java.io.BufferedInputStream.read(BufferedInputStream.java:345)
at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:704)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:647)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:675)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1569)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:338)
at org.glassfish.jersey.client.internal.HttpUrlConnector._apply(HttpUrlConnector.java:399)
at org.glassfish.jersey.client.internal.HttpUrlConnector.apply(HttpUrlConnector.java:285)
... 11 more

修改

我忘了提到我设法通过 CURL 从无效超时的bash发布我的请求并保持活着,所以它不应该是服务器问题。 CURL REST帖子花了24分钟。并且我的java客户端在15分钟后抛出连接重置。在我看来,这可能是我的错。

1 个答案:

答案 0 :(得分:0)

您正面临超时问题,尽管您的客户端似乎配置得很好。如果在应用程序服务器前运行Apache之类的Web服务器,则可能由任何防火墙,代理,负载均衡器甚至服务器本身引起此超时。请检查客户端和应用程序服务器之间的内容,并相应地设置超时。

这并不意味着您无法对客户端本身做任何事情,但在那里解决起来要困难得多。 在Windows上,您需要先启用TCP keep-alives。 之后,根据您使用的Client Transport,我们将尝试将TCP保持活动添加到底层工厂构建的套接字,类似于我们在Axis clients上可能知道的。这个解决方案对您来说更耗时。