使用 Web 服务处理大文件
我有一个 Web 服务方法,应该处理一个非常大的文件并将多个文件输出到服务器。但是,此 Web 服务将会超时,并且调用者将无法获得 CREATED 状态。我只是想知道是否有一种方法可以运行处理作业(启动一个新线程或其他东西)并返回状态而不等待进程完成。
public Response processFile(InputStream inputStream){
//I want to process the file here
//but I dont want the invoker to wait for that process to finish
//I just want the response to be returned right away
return Response.status(Response.Status.CREATED).build();
}
I have web service method that is supposed to process a very large file and output several files to the server. However, this web service will just timeout and there will be no way for the invoker to get the CREATED status. I am just wondering whether there is a way to run the processing job (starting a new thread or something) and return the status without waiting for the process to be done.
public Response processFile(InputStream inputStream){
//I want to process the file here
//but I dont want the invoker to wait for that process to finish
//I just want the response to be returned right away
return Response.status(Response.Status.CREATED).build();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
文件来自输入流,对吗?因此,如果您要发回 CREATED 状态(实际上关闭连接),您可能会在收到整个输入文件之前断开连接?
无论如何,这就是我的想法......在这种情况下,您只想将超时设置为更长的值。
如果情况并非如此,那么我想启动一个新线程,及时处理其中的所有内容并发回 CREATED 状态就可以了。
The file comes from the input stream, right? So if you'll send back a CREATED status (effectually closing the connection) you might sever the connection before you receive the entirety of the input file?
That's what i thought anyways... In which case you'll just want to set the timeout to a lengthier value.
If that's not the case, then i guess it would be fine to start a new thread, process everything there in good time and send back the CREATED status.