Tomcat: Shutdown leads to Premature EOF error in client










0















I have a servlet running on tomcat8 (using Jersey) and a client application, written in Java. To transfer data that is exported from the client application directly to the webserver (instead of first exporting all data and uploading the data afterwards) I am using a custom InputStream implementation.



Everything works fine, except in this scenario:



I am starting the data transfer from the client, then shutdown tomcat while the data is still transferring. Log messages are showing that the request is completed (I set unloadDelay to a very high value to ensure requests will complete before shutdown), but the client throws an "Premature EOF" exception when trying to read the response.



I verified that the stream is transfered to the server completely. So after the server handles the InputStream it returns a String as a result, but when trying to read the result in the client the server is already gone (it seems).



How can I gracefully shutdown tomcat, so requests still in process will finish without errors?



Here is part of the code:



Snippet from Jersey Resource



@POST
@Produces(MediaType.TEXT_PLAIN)
public Response handleData(InputStream data)
String result = service.put(data);
log.info("handledData, result: " + result);
return Response.status(201).entity(result).build();



Snippet from WebRequests.java - A helper class using jersey to call the servlet



private static Builder builder(String url, String sessionId, InputStream data) 
WebResource resource = createClient().resource(url);
Builder builder = resource.accept(MediaType.APPLICATION_JSON_TYPE, MediaType.TEXT_PLAIN_TYPE, MediaType.APPLICATION_OCTET_STREAM_TYPE);
builder.cookie(new Cookie("JSESSIONID", sessionId));
builder.entity(data, MediaType.APPLICATION_OCTET_STREAM_TYPE);
return builder;


private static Client createClient()
ClientConfig config = new DefaultClientConfig();
config.getProperties().put(ClientConfig.PROPERTY_FOLLOW_REDIRECTS, false);
Client client = Client.create(config);
client.setChunkedEncodingSize(1024 * 100);
return client;



The calling code (CommitStream is the custom input stream):



InputStream stream = new CommitStream(db, message, data, callback);
String result = WebRequests.builder("http://....", sessionId, stream).post(String.class);


Stacktrace



 java.io.IOException: Premature EOF
at sun.net.www.http.ChunkedInputStream.readAheadBlocking(ChunkedInputStream.java:565)
at sun.net.www.http.ChunkedInputStream.readAhead(ChunkedInputStream.java:609)
at sun.net.www.http.ChunkedInputStream.read(ChunkedInputStream.java:696)
at java.io.FilterInputStream.read(FilterInputStream.java:133)
at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(HttpURLConnection.java:3444)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.Reader.read(Reader.java:140)
at com.sun.jersey.core.util.ReaderWriter.readFromAsString(ReaderWriter.java:171)
at com.sun.jersey.core.util.ReaderWriter.readFromAsString(ReaderWriter.java:157)
at com.sun.jersey.core.provider.AbstractMessageReaderWriterProvider.readFromAsString(AbstractMessageReaderWriterProvider.java:114)
at com.sun.jersey.core.impl.provider.entity.StringProvider.readFrom(StringProvider.java:73)
at com.sun.jersey.core.impl.provider.entity.StringProvider.readFrom(StringProvider.java:58)
at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:634)









share|improve this question
























  • Im not very sure about this, but I think it might have to do something with how Jersey Client is processing the response. It seems that the response is not directly retrieved, even when using builder.post(String.class) directly (instead of using builder.post(ClientResponse.class).getEntity(String.class))

    – Sebastian Greve
    Nov 15 '18 at 15:54
















0















I have a servlet running on tomcat8 (using Jersey) and a client application, written in Java. To transfer data that is exported from the client application directly to the webserver (instead of first exporting all data and uploading the data afterwards) I am using a custom InputStream implementation.



Everything works fine, except in this scenario:



I am starting the data transfer from the client, then shutdown tomcat while the data is still transferring. Log messages are showing that the request is completed (I set unloadDelay to a very high value to ensure requests will complete before shutdown), but the client throws an "Premature EOF" exception when trying to read the response.



I verified that the stream is transfered to the server completely. So after the server handles the InputStream it returns a String as a result, but when trying to read the result in the client the server is already gone (it seems).



How can I gracefully shutdown tomcat, so requests still in process will finish without errors?



Here is part of the code:



Snippet from Jersey Resource



@POST
@Produces(MediaType.TEXT_PLAIN)
public Response handleData(InputStream data)
String result = service.put(data);
log.info("handledData, result: " + result);
return Response.status(201).entity(result).build();



Snippet from WebRequests.java - A helper class using jersey to call the servlet



private static Builder builder(String url, String sessionId, InputStream data) 
WebResource resource = createClient().resource(url);
Builder builder = resource.accept(MediaType.APPLICATION_JSON_TYPE, MediaType.TEXT_PLAIN_TYPE, MediaType.APPLICATION_OCTET_STREAM_TYPE);
builder.cookie(new Cookie("JSESSIONID", sessionId));
builder.entity(data, MediaType.APPLICATION_OCTET_STREAM_TYPE);
return builder;


private static Client createClient()
ClientConfig config = new DefaultClientConfig();
config.getProperties().put(ClientConfig.PROPERTY_FOLLOW_REDIRECTS, false);
Client client = Client.create(config);
client.setChunkedEncodingSize(1024 * 100);
return client;



The calling code (CommitStream is the custom input stream):



InputStream stream = new CommitStream(db, message, data, callback);
String result = WebRequests.builder("http://....", sessionId, stream).post(String.class);


Stacktrace



 java.io.IOException: Premature EOF
at sun.net.www.http.ChunkedInputStream.readAheadBlocking(ChunkedInputStream.java:565)
at sun.net.www.http.ChunkedInputStream.readAhead(ChunkedInputStream.java:609)
at sun.net.www.http.ChunkedInputStream.read(ChunkedInputStream.java:696)
at java.io.FilterInputStream.read(FilterInputStream.java:133)
at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(HttpURLConnection.java:3444)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.Reader.read(Reader.java:140)
at com.sun.jersey.core.util.ReaderWriter.readFromAsString(ReaderWriter.java:171)
at com.sun.jersey.core.util.ReaderWriter.readFromAsString(ReaderWriter.java:157)
at com.sun.jersey.core.provider.AbstractMessageReaderWriterProvider.readFromAsString(AbstractMessageReaderWriterProvider.java:114)
at com.sun.jersey.core.impl.provider.entity.StringProvider.readFrom(StringProvider.java:73)
at com.sun.jersey.core.impl.provider.entity.StringProvider.readFrom(StringProvider.java:58)
at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:634)









share|improve this question
























  • Im not very sure about this, but I think it might have to do something with how Jersey Client is processing the response. It seems that the response is not directly retrieved, even when using builder.post(String.class) directly (instead of using builder.post(ClientResponse.class).getEntity(String.class))

    – Sebastian Greve
    Nov 15 '18 at 15:54














0












0








0








I have a servlet running on tomcat8 (using Jersey) and a client application, written in Java. To transfer data that is exported from the client application directly to the webserver (instead of first exporting all data and uploading the data afterwards) I am using a custom InputStream implementation.



Everything works fine, except in this scenario:



I am starting the data transfer from the client, then shutdown tomcat while the data is still transferring. Log messages are showing that the request is completed (I set unloadDelay to a very high value to ensure requests will complete before shutdown), but the client throws an "Premature EOF" exception when trying to read the response.



I verified that the stream is transfered to the server completely. So after the server handles the InputStream it returns a String as a result, but when trying to read the result in the client the server is already gone (it seems).



How can I gracefully shutdown tomcat, so requests still in process will finish without errors?



Here is part of the code:



Snippet from Jersey Resource



@POST
@Produces(MediaType.TEXT_PLAIN)
public Response handleData(InputStream data)
String result = service.put(data);
log.info("handledData, result: " + result);
return Response.status(201).entity(result).build();



Snippet from WebRequests.java - A helper class using jersey to call the servlet



private static Builder builder(String url, String sessionId, InputStream data) 
WebResource resource = createClient().resource(url);
Builder builder = resource.accept(MediaType.APPLICATION_JSON_TYPE, MediaType.TEXT_PLAIN_TYPE, MediaType.APPLICATION_OCTET_STREAM_TYPE);
builder.cookie(new Cookie("JSESSIONID", sessionId));
builder.entity(data, MediaType.APPLICATION_OCTET_STREAM_TYPE);
return builder;


private static Client createClient()
ClientConfig config = new DefaultClientConfig();
config.getProperties().put(ClientConfig.PROPERTY_FOLLOW_REDIRECTS, false);
Client client = Client.create(config);
client.setChunkedEncodingSize(1024 * 100);
return client;



The calling code (CommitStream is the custom input stream):



InputStream stream = new CommitStream(db, message, data, callback);
String result = WebRequests.builder("http://....", sessionId, stream).post(String.class);


Stacktrace



 java.io.IOException: Premature EOF
at sun.net.www.http.ChunkedInputStream.readAheadBlocking(ChunkedInputStream.java:565)
at sun.net.www.http.ChunkedInputStream.readAhead(ChunkedInputStream.java:609)
at sun.net.www.http.ChunkedInputStream.read(ChunkedInputStream.java:696)
at java.io.FilterInputStream.read(FilterInputStream.java:133)
at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(HttpURLConnection.java:3444)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.Reader.read(Reader.java:140)
at com.sun.jersey.core.util.ReaderWriter.readFromAsString(ReaderWriter.java:171)
at com.sun.jersey.core.util.ReaderWriter.readFromAsString(ReaderWriter.java:157)
at com.sun.jersey.core.provider.AbstractMessageReaderWriterProvider.readFromAsString(AbstractMessageReaderWriterProvider.java:114)
at com.sun.jersey.core.impl.provider.entity.StringProvider.readFrom(StringProvider.java:73)
at com.sun.jersey.core.impl.provider.entity.StringProvider.readFrom(StringProvider.java:58)
at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:634)









share|improve this question
















I have a servlet running on tomcat8 (using Jersey) and a client application, written in Java. To transfer data that is exported from the client application directly to the webserver (instead of first exporting all data and uploading the data afterwards) I am using a custom InputStream implementation.



Everything works fine, except in this scenario:



I am starting the data transfer from the client, then shutdown tomcat while the data is still transferring. Log messages are showing that the request is completed (I set unloadDelay to a very high value to ensure requests will complete before shutdown), but the client throws an "Premature EOF" exception when trying to read the response.



I verified that the stream is transfered to the server completely. So after the server handles the InputStream it returns a String as a result, but when trying to read the result in the client the server is already gone (it seems).



How can I gracefully shutdown tomcat, so requests still in process will finish without errors?



Here is part of the code:



Snippet from Jersey Resource



@POST
@Produces(MediaType.TEXT_PLAIN)
public Response handleData(InputStream data)
String result = service.put(data);
log.info("handledData, result: " + result);
return Response.status(201).entity(result).build();



Snippet from WebRequests.java - A helper class using jersey to call the servlet



private static Builder builder(String url, String sessionId, InputStream data) 
WebResource resource = createClient().resource(url);
Builder builder = resource.accept(MediaType.APPLICATION_JSON_TYPE, MediaType.TEXT_PLAIN_TYPE, MediaType.APPLICATION_OCTET_STREAM_TYPE);
builder.cookie(new Cookie("JSESSIONID", sessionId));
builder.entity(data, MediaType.APPLICATION_OCTET_STREAM_TYPE);
return builder;


private static Client createClient()
ClientConfig config = new DefaultClientConfig();
config.getProperties().put(ClientConfig.PROPERTY_FOLLOW_REDIRECTS, false);
Client client = Client.create(config);
client.setChunkedEncodingSize(1024 * 100);
return client;



The calling code (CommitStream is the custom input stream):



InputStream stream = new CommitStream(db, message, data, callback);
String result = WebRequests.builder("http://....", sessionId, stream).post(String.class);


Stacktrace



 java.io.IOException: Premature EOF
at sun.net.www.http.ChunkedInputStream.readAheadBlocking(ChunkedInputStream.java:565)
at sun.net.www.http.ChunkedInputStream.readAhead(ChunkedInputStream.java:609)
at sun.net.www.http.ChunkedInputStream.read(ChunkedInputStream.java:696)
at java.io.FilterInputStream.read(FilterInputStream.java:133)
at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(HttpURLConnection.java:3444)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.Reader.read(Reader.java:140)
at com.sun.jersey.core.util.ReaderWriter.readFromAsString(ReaderWriter.java:171)
at com.sun.jersey.core.util.ReaderWriter.readFromAsString(ReaderWriter.java:157)
at com.sun.jersey.core.provider.AbstractMessageReaderWriterProvider.readFromAsString(AbstractMessageReaderWriterProvider.java:114)
at com.sun.jersey.core.impl.provider.entity.StringProvider.readFrom(StringProvider.java:73)
at com.sun.jersey.core.impl.provider.entity.StringProvider.readFrom(StringProvider.java:58)
at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:634)






java tomcat jersey shutdown






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 14:52







Sebastian Greve

















asked Nov 15 '18 at 13:57









Sebastian GreveSebastian Greve

174




174












  • Im not very sure about this, but I think it might have to do something with how Jersey Client is processing the response. It seems that the response is not directly retrieved, even when using builder.post(String.class) directly (instead of using builder.post(ClientResponse.class).getEntity(String.class))

    – Sebastian Greve
    Nov 15 '18 at 15:54


















  • Im not very sure about this, but I think it might have to do something with how Jersey Client is processing the response. It seems that the response is not directly retrieved, even when using builder.post(String.class) directly (instead of using builder.post(ClientResponse.class).getEntity(String.class))

    – Sebastian Greve
    Nov 15 '18 at 15:54

















Im not very sure about this, but I think it might have to do something with how Jersey Client is processing the response. It seems that the response is not directly retrieved, even when using builder.post(String.class) directly (instead of using builder.post(ClientResponse.class).getEntity(String.class))

– Sebastian Greve
Nov 15 '18 at 15:54






Im not very sure about this, but I think it might have to do something with how Jersey Client is processing the response. It seems that the response is not directly retrieved, even when using builder.post(String.class) directly (instead of using builder.post(ClientResponse.class).getEntity(String.class))

– Sebastian Greve
Nov 15 '18 at 15:54













0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53321071%2ftomcat-shutdown-leads-to-premature-eof-error-in-client%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53321071%2ftomcat-shutdown-leads-to-premature-eof-error-in-client%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

What does pagestruct do in Eviews?

Dutch intervention in Lombok and Karangasem

Channel Islands