Does StreamingOutput push a new stream to write to the output stream?

Say we have a web service:

// This code is taken from the stack overflow question

@Autowired
private Service service;

@GET
@Produces(MediaType.TEXT_PLAIN)
public Response streamExample() {
  StreamingOutput stream = service.getStream();
  return Response.ok(stream).build();
}

Class of service:

public class Service{

      public StreamingOutput getStream(){
            log.info("Going to start Streaming");
            StreamingOutput stream = new StreamingOutput() {
            @Override
            public void write(OutputStream os) throws IOException,
            WebApplicationException {
                Writer writer = new BufferedWriter(new OutputStreamWriter(os));
                writer.write("test");
                log.info("Inside streaming.");
                writer.flush();
           }
           };
           log.info("Finished streaming.");
           return stream;
      }
}

The output in the log file: Start streaming. Ready to stream. Inside streaming.

There are two questions that I would like to ask about this: 1. Is a new stream created for streaming output for each request? 2. If there is a request for sleep mode that I would like to run inside the stream output recording method, how do I associate a session with this stream?

+4
source share

All Articles