简体   繁体   English

关闭输出流后,servlet请求会怎样?

[英]What happens to a servlet request after you close the output stream?

We are running on tomcat for our application server. 我们正在我们的应用程序服务器的tomcat上运行。

We have a servlet that generates a PDF and returns it to the browser. 我们有一个servlet,可以生成PDF并将其返回给浏览器。 It then deletes the temporary PDF file. 然后删除临时PDF文件。

The issue is that we were having some timing difficulties with the delete. 问题是我们在删除时存在一些时间困难。 Someone on our team decided that we should wait 20 seconds before we delete the file, so they use a Thread.sleep(20000). 我们团队中的某个人决定,我们应该等待20秒才能删除文件,因此他们使用Thread.sleep(20000)。

The interesting thing is that they close the output stream, wait 20 seconds, and then delete the file and exit doGet(). 有趣的是,它们关闭输出流,等待20秒钟,然后删除文件并退出doGet()。

My question is this: Since the output stream is closed on the response, it returns immediately to the browser before the wait, are there any performance concerns to the additional 20 seconds wait? 我的问题是:由于响应中输出流已关闭,因此在等待之前它会立即返回浏览器,另外20秒的等待是否会对性能造成影响? For example, does it tie up a network connection or some such? 例如,它是否占用网络连接或其他连接?

Well no, I have put something like that in an application and its running for quite sometime now, only the output stream is closed, so you can't communicate with the user's current request anymore, but since the doGet did not exist so the servlet's object is not destroyed until the thread timeout ends. 好吧,不,我已经在一个应用程序中放置了类似的代码,并且它已经运行了一段时间,只有输出流关闭了,因此您无法再与用户的当前请求进行通信,但是由于doGet不存在,因此servlet的线程超时结束之前,对象不会被破坏。

There will probably be no issues regarding the network bandwidth, because the deletion and the waiting only happens in memory, like a background task, the only thing that affects that network's bandwidth is the time taken to send a response and since you respond fast, and then finish your work later then its ok. 关于网络带宽,可能不会有任何问题,因为删除和等待仅发生在内存中,就像后台任务一样,影响网络带宽的唯一因素是发送响应所花费的时间,并且响应速度很快,并且然后稍后再完成工作就可以了。

I would suggest however to use a destroy() method instead to delete the file. 但是,我建议使用destroy()方法来删除文件。

I would suggest using File.createTempFile() to create your temporary file (unless you have already tried this and is not feasible for you for whatever reason) and let Java decide when to delete it. 我建议使用File.createTempFile()创建临时文件(除非您已经尝试过该文件,并且出于某种原因对您而言是不可行的),然后让Java决定何时删除它。 By using Thread.sleep() in your servlet you are essentially holding the servlet instance. 通过在servlet中使用Thread.sleep(),您实际上是在保留servlet实例。 The ServletContainers create a pool of servlets for subsequent requests and any servlet living for 20 seconds will increase your pool size and the Servlet will not be available for the next request hence the container will have no option other than to create a new instance for subsequent requests. ServletContainers为后续请求创建一个Servlet池,任何Servlet存活20秒都会增加您的池大小,并且Servlet无法用于下一个请求,因此容器除了为后续请求创建新实例外别无选择。

There may not be any network or bandwidth related issues. 可能没有任何与网络或带宽有关的问题。 However, holding on to a Servlet in the way you are describing is a recipe for disaster. 但是,按照您所描述的方式坚持Servlet是造成灾难的秘诀。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 调用close方法后,输出流/输入流对象引用会发生什么情况? - What happens to an Output Stream/Input Stream object reference after calling close method? 当我从servlet向浏览器发送文件时,输出流会发生什么? - What happens to the output stream when I'm sending a file from a servlet to a browser? 当请求超时时,http servlet会发生什么? - What happens with http servlet when request times out? 如果不关闭URL连接会怎样? - What happens if you don't close a URL connection? 不关闭HBase表时会发生什么? - What happens when you do not close an HBase table? 双击jar时,java输出流会怎样? - What happens to java output stream when jar is double clicked? 在em.close()之后,延迟加载的实体会发生什么? - What happens with lazily loaded entities after em.close()? 如果我没有在Java中关闭BufferedReader会发生什么? (在多线程程序中读取流类型) - What happens if I do not close BufferedReader in java? (Stream type reading in multi-threaded program) 将.method放在对象之后会发生什么? - What happens when you put a .method after an object? 如果我在web.xml中有两个与请求匹配的servlet映射会发生什么? - What happens if I have two servlet mappings in web.xml that match a request?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM