Prevent Jerry server support when flow resumes after client closes

I am working on a small application that requires streaming different lengths from a REST endpoint to a client using Jersey 2.0 (version 2.19). Although this is ideal due to the existing structure of the client application, the client can cancel the request at any time.

I can successfully return streaming output using StreamingOutput, as seen from my code below.

@GET
public Response test() {

        StreamingOutput stream = new StreamingOutput() {
            @Override
            public void write(OutputStream os) throws IOException, WebApplicationException {

                Writer writer = new BufferedWriter(new OutputStreamWriter(os));

                for (int i = 0; i < 500000; i++) {
                    LOGGER.info(Integer.toString(i));
                    writer.write(String.valueOf(i) + "\n");
                    writer.flush();
                }

                writer.close();
            }
        };

        return Response.ok(stream).build();
}

( 0 499999), , Tomcat ( Tomcat).
, , ( ), , Tomcat, , , , . , Tomcat, Tomcat tomcat, , :

SEVERE: The web application [/streaming-1.0-SNAPSHOT] created a ThreadLocal with key of type [java.lang.ThreadLocal] (value [java.lang.ThreadLocal@27d415d9]) and a value of type [org.glassfish.jersey.process.internal.RequestScope.Instance] (value [Instance{id=3ad9c61c-22cd-40d5-b810-59bff3feafa9, referenceCounter=2, store size=4}]) but failed to remove it when the web application was stopped. This is very likely to create a memory leak.

, , , , ?

, , Tomcat, , , , - tomcat.

+4
1

BufferedWriter a try-with-resources statement IOException?

, writer.write IOException, , BufferedWriter , Tomcat- OutputStream .

try (Writer writer = new BufferedWriter(new OutputStreamWriter(os))) {
  ...
} catch(IOException ioe) {
  // log client termination.
}
+1

Source: https://habr.com/ru/post/1598906/


All Articles