停止服务器请求

发布于 2024-12-27 16:27:22 字数 142 浏览 0 评论 0原文

我有一个网络应用程序(java,tomcat),允许用户通过水晶报告生成报告。问题是,如果用户请求需要几个小时才能完成的庞大报告,我的整个应用程序就会停止运行。有没有办法停止这样的请求,比如说 5 分钟后它还没有完成?除了优化速度和限制报告大小之外,还有其他解决方法吗?

I have a web app (java, tomcat) that allows users to generate reports through crystal reports. Problem is if a user requests a huge report that takes hours to complete, my whole app grinds to a halt. Is there a way to stop a request like this, say if after 5 minutes it has not been complete? Other than optimizing for speed and limiting report sizes, are there any other work-arounds?

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

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

发布评论

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

评论(2

把时间冻结 2025-01-03 16:27:22

我认为您没有在另一个线程中运行报告生成。最好在另一个线程中运行报告生成,并在准备就绪时将链接发送回用户。

您可以使用java的ExecutorService并为线程设置一些超时,如果它花费的时间超过您为其分配的时间,则该线程将过期。

搜索Java ExecutorService、Future、Callable教程

示例代码:

 Callable<Integer> callable = new CallableImpl(2);
  ExecutorService executor = new ScheduledThreadPoolExecutor(5);
  Future<Integer> future = executor.submit(callable);
  Integer result = future.get(600,TimeUnit.seconds));

I think you are not running your report generation in another Thread. It is better to run the report generation in another Thread and send a link back to user when it is ready.

You can use ExecutorService of java and set some timeout for the thread and it will expire if it takes more than the time you have allocated for it.

Search for Java ExecutorService, Future, Callable tutorial

Sample code:

 Callable<Integer> callable = new CallableImpl(2);
  ExecutorService executor = new ScheduledThreadPoolExecutor(5);
  Future<Integer> future = executor.submit(callable);
  Integer result = future.get(600,TimeUnit.seconds));
梦晓ヶ微光ヅ倾城 2025-01-03 16:27:22

另一种方法是在单独的线程中运行报告生成(或者可能使用ExecutorService),并在生成时间超过某个阈值时调用Thread.interrupt()。如果线程当前在 I/O(网络、数据库)上阻塞,则该线程很可能会被中断。

Another approach is to run report generation in a separate thread (or maybe using ExecutorService) and call Thread.interrupt() when generation time exceeds some threshold. Chances are the thread will be interrupted if it currently blocks on I/O (network, database).

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