Tomcat - 响应后做事情
我有一个servlet,用于在android 中上传我的应用程序的数据库。
数据库在服务器端处理,但这可能需要一段时间。
目前,我只是返回一个响应,表示使用 PrintWriter.write() 上传已成功。
有没有办法在响应发送到客户端(我的Android应用程序)后开始处理文件?
如果我发送重定向到另一个 servlet 或使用调度程序转发请求,则在执行处理的 servlet 完成之前我不会收到响应。
最好的方法是什么,立即响应用户,让数据库处理开始并将结果发送回android(注意这可能需要5分钟或10分钟,具体取决于数据库 - 所以我假设我需要某种侦听器在服务器上轮询结果是否已处理并准备好在客户端上显示)
I have a servlet for the purposes of uploading my application's database in android.
The database is processed on the server side but that could take a while.
At the moment, I'm just returning back with a response that the upload has been successful using PrintWriter.write().
Is there a way after the response has been sent to the client(my android app) to start processing the file?
If I send a redirect to another servlet or forward the request using the dispatcher, I won't have a response until the servlet that carries out the processing is finished.
What's the best way, to respond to the user instantly,let the processing of the database begin and send the results back to android (note this could take 5 minutes or 10 minutes, depending on the database - so I'm assuming I would need some sort of listener to poll on the server whether the results have been processed and ready to be shown on the client)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
后端处理可以通过作业完成,例如通过 Quartz。如果服务器可以替换为 JBoss AS、Glassfish 或 Resin,那么一个简单的 @Asynchronous 注释方法也可以完成这里的工作。
您确实可以连续轮询服务器以询问它是否已经完成。这也是 Web 环境中大多数基于 AJAX 的进度条所做的事情。
或者,您可以使用反向 AJAX (Comet) 来防止轮询(请参见例如 http://atmosphere.java.net/ )。在这种情况下,服务器能够在完成后立即将响应推送回给您。这确实需要在您的服务器上进行某种事件。如果您可以用 JBoss AS 或 Glassfish 替换服务器,则 CDI 事件总线可能是一个选项。否则,在 HTTP 会话中调用 wait/notify 的共享对象是一个粗略的替代方案。
The backend processing can be done via a job, e.g. via Quartz. If the server could be replaced with JBoss AS, Glassfish or Resin, a simple @Asynchronous annotated method could also do the job here.
You can indeed poll the server continuously to ask if it's already done. This is what the majority of the AJAX based progress bars in a web environment also do.
Alternatively you could make use of reverse AJAX (Comet) to prevent the polling (see e.g. http://atmosphere.java.net/). In that case the server is able to push a response back to you as soon as it's done. This does necessitate some kind of eventing on your server. If you could replace the server with JBoss AS or Glassfish, the CDI event bus could be an option. Otherwise, a shared object in the HTTP session on which you call wait/notify is a crude alternative.