jsp多线程?

发布于 2024-12-05 23:20:19 字数 253 浏览 1 评论 0原文

我对 jersey、jsp 和 web 应用程序开发总体来说是新手,所以希望这不是一个愚蠢的问题。我有一个 jsp,当前当用户点击其上的按钮时,它会启动一个 HTTP 请求,该请求大约需要 5-10 分钟才能返回。一旦完成,他们就会被重定向到另一个页面。

我想知道,是否有可能甚至建议我对应用程序进行多线程处理,以便开始繁重的处理,但用户会立即重定向到下一个 .jsp。如果多线程无法实现,您是否会推荐另一种方法来处理 Web 应用程序中的繁重处理?

I'm new to jersey, jsp's and web application development in general so hopefully this isn't a silly question. I've got a jsp and currently when the user hits a button on it, it starts a HTTP request which takes about 5-10 minutes to return. Once it finishes they're redirected to another page.

I'm wondering, is it possible or even advisable that I multithread the application so that the heavy processing will start but the user get's redirected to the next .jsp right away. If multithreading is not possible is there another method that you would recommend for dealing with heavy processing in a web application?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

箜明 2024-12-12 23:20:19

JSP 基本上是一个 Servlet(它在 Java Servlet 类中进行翻译并编译)。理论上,您可以在 servlet 中(因此在 JSP 中,通过 scriptlet)启动一个新线程,但出于多种原因,实际上不建议这样做。

最好通过 ajax 进行异步 HTTP 调用,然后,一旦调用完成,立即向用户显示其他内容,并在回调返回时显示结果。

A JSP is basically a Servlet (it's translated in a Java Servlet Class and compiled). Teoretically you can start a new thread in a servlet (and hence in a JSP, via scriptlet), but that's really not advised for multiple reasons.

It'd be better recommended to make an asynchronous HTTP call via ajax, then, once the call is done immediately show something else to the user, and when the call back returns display the results.

海夕 2024-12-12 23:20:19

使用持续轮询共享队列的工作线程可能比每次创建一个新线程更有效。例如,使用 ArrayBlockingQueue,您的 Web 请求可以简单地将对象添加到队列中并返回给用户,并且您的工作线程(或重复计划作业)可以处理繁重的处理。

Rather than create a new thread each time it might be more efficient to have a worker thread which continually polls a shared queue. Using, for example, ArrayBlockingQueue you web request can simple add an object to the queue and return to the user, and your worker thread (or repeating scheduled job) can take care of the heavy weight processing.

千里故人稀 2024-12-12 23:20:19

您可以创建一个 TimerTask(或 Quartz Job)并将其设置为立即执行并将用户重定向到其他一些页面。让该 Job 将结果存储在可以由另一个 JSP 访问的某个中心位置(如果您想稍后提取 Job 的结果,可能是通过 ajax)这样做,您可以避免手动管理线程(这很容易出错) ,您可以获得异步功能,用户不需要在大约 5-10 分钟内看到空白的浏览器屏幕。

Instead of waiting for process to complete in a JSP, you can create a TimerTask (or Quartz Job) and set it for immediate execution and redirect user to some other page. Have that Job store the result in some central place that can be accessed by another JSP (in case you want to pull result of Job later, may be through ajax) Doing so, you save yourself from managing threads manually (which is error prone), you get async functionality, user does not need to see the blank browser screen for around 5-10 minutes.

国粹 2024-12-12 23:20:19

这是可能的。

创建一个线程,将其引用存储在随处可用的地方(静态映射)并存储其密钥(在会话中,在 JSP 答案的代码中)。

以下调用可以检索线程并检查其状态/结果。

无论如何,请小心使用:

a) 您需要控制旧结果是否被删除。有时浏览器关闭是不可避免的,所以你需要一个Watchdog来清除显然不再需要的数据。

b) 用户不习惯这种行为。存在严重的风险,他们会“返回”并尝试一次又一次地启动该线程。尝试控制它(理想情况下,线程的 id 将链接到用户,因此只要较旧的线程处于活动状态,用户就无法启动另一个线程)。

It is possible.

Create a thread, store its reference somewhere that is available everywhere (a static Map) and store its key (in the session, in the code of the JSP's answer).

Following calls can retrieve the thread and check its state/results.

Anyway, use with care:

a) You will need to control that old results are deleted. It is inevitable that sometimes the browser will close, so you need a Watchdog to clear data obviously no longer needed.

b) The user are not used to this kind of behavior. There is a serious risk that they will just "go back" and try to launch the thread again, and again, and again. Try to control it (ideally the id of the thread will be linked to the user, so as long as an older thread is active an user cannot launch another one).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文