假设我们有一个这样的网络服务:
//这段代码取自一个堆栈溢出问题
@Autowired
private Service service;
@GET
@Produces(MediaType.TEXT_PLAIN)
public Response streamExample() {
StreamingOutput stream = service.getStream();
return Response.ok(stream).build();
}
Run Code Online (Sandbox Code Playgroud)
服务等级:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
日志文件中的输出是:Going to start Streaming。完成流媒体。内流。
关于此我想问两个问题: 1. 是否创建了一个新线程来为每个请求流式传输输出?2. 如果我想在流输出的写入方法中运行一个休眠查询,我如何将会话关联到该线程?