简体   繁体   English

在返回响应之前获取servlet请求的状态

[英]Get status of servlet request before the response is returned

Good evening, 晚上好,

I am in the process of writing a Java Servlet (Struts 2, Tomcat, JSP etc) which is capable of doing some fairly complex simulations. 我正在编写Java Servlet(Struts 2,Tomcat,JSP等),它能够进行一些相当复杂的模拟。 These can take up to 2 minutes to complete on the and will return a graph of the results. 这些最多可能需要2分钟才能完成,并将返回结果图表。 It is trivial to calculate the percentage of the simulation completed because the process works by repeating the same calculations 1000s of times. 计算完成的模拟百分比是微不足道的,因为该过程通过重复相同的计算1000次来工作。

I would be interested to know if anyone has ever tried to use client side technology to provide any estimate of the percentage complete. 我很想知道是否有人试图使用客户端技术来提供完成百分比的任何估计。 Ie query the servlet processing to get the number of cycles completed at various point throughout the simulation. 即查询servlet处理以获得在整个模拟中的各个点处完成的循环数。 This could then be displayed as a bar in the client browser. 然后,这可以在客户端浏览器中显示为条形。

Any thoughts, advice, resources would be much appreciated. 任何想法,建议,资源将不胜感激。

Thanks, 谢谢,

Alex 亚历克斯

In your database, have a table to maintain a list of simulations along with their server-calculated progress. 在您的数据库中,有一个表来维护模拟列表及其服务器计算的进度。

In your web-application, use AJAX to query the server every few seconds (1-20 depending on load is what I'd go with) and update a graphical progress bar. 在您的Web应用程序中,每隔几秒钟使用AJAX查询服务器(1-20取决于负载是我要使用的)并更新图形进度条。 Most javascript libraries have simple timer-based AJAX functions to do exactly this sort of thing. 大多数javascript库都有简单的基于计时器的AJAX函数来完成这类工作。

There's a few details to figure out, such as whether or not completed simulations remain in the DB (could be useful logging info), but overall, this should be fairly simple. 有一些细节需要弄清楚,比如完成的模拟是否保留在数据库中(可能是有用的日志信息),但总的来说,这应该相当简单。

You could encapsulate your response in a mime/multipart message and sends your updates until you have a full response done. 您可以将响应封装在mime / multipart消息中并发送更新,直到完成完整响应。

Bugzilla uses this in their search to show "Searching ..."-screen until the searchresult is done. Bugzilla在搜索中使用它来显示“正在搜索...” - 屏幕直到搜索结果完成。

If you want to use plain Struts2, you should take a look at the ExecuteAndWait Interceptor . 如果你想使用普通的Struts2,你应该看一下ExecuteAndWait Interceptor

It works by the old refresh-with-timeout method. 它通过旧的refresh-with-timeout方法工作。 Sure, it has lower coolness factor than some AJAX thing, but it's simple and works (I've used it). 当然,它比一些AJAX的东西具有更低的凉爽系数,但它很简单并且有效(我已经使用过它)。

Struts2 takes care (by using this interceptor) of executing the action method (which would typically take a long time) in a separate thread, and it returns a special result wait until the work is completed. Struts2小心(通过使用此拦截器)在单独的线程中执行action方法(通常需要很长时间),并返回特殊结果wait直到工作完成。 You just have to code a jsp that shows some "waiting..." message for this result, and make it refresh to the same action, repeatedly, with (say) two or three seconds of timeout. 你只需要为这个结果编写一个显示一些“等待...”消息的jsp代码,然后重复刷新同样的动作,比如(比方说)两到三秒的超时。 The jsp has access to the action properties, of course, hence you can code some getProgress() method to show a progress message or bar. 当然,jsp可以访问动作属性,因此您可以编写一些getProgress()方法来显示进度消息或条形图。

AJAX是去这里的方式。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM