【问题标题】:Execute a method without blocking the client在不阻塞客户端的情况下执行方法
【发布时间】:2009-07-08 15:38:34
【问题描述】:

我正在使用 Java 来创建 Web 应用程序。 应用程序必须加载一个表单,该表单将其数据发送到调用长业务逻辑的 jsp。 我需要jsp返回一条消息,例如“感谢您使用我的服务,完成后会向您发送一封电子邮件。”

问题是,如果我要写这样的东西:

<html>
<head></head>
<body>
...
<span>thank you for using my service, an email will sent to you when the proccess is done</span>
...
<% businessClass.doLongOperation(); %>
...
...
</Body>
</html>

jsp 会运行很长时间,用户必须等待操作结束。此外,如果用户关闭浏览器,我不确定 jsp 是否会继续工作。

如何将长操作与 jsp 隔离,这样用户就不必等待所有的执行时间,以及如何使长操作的执行不依赖于浏览器?

诺尔

【问题讨论】:

    标签: jsp business-logic


    【解决方案1】:

    您的 JSP 可能在服务器端由 servlet 或 Struts Action 或 Spring MVC 控制器支持。

    在该对象中,执行异步执行:

    executor.execute(new Runnable() {
    
        public void run() { businessClass.doLongOperation(); }
    });
    

    其中executorjava.util.concurrent.Executor 的一个实例。

    【讨论】:

      猜你喜欢
      • 2016-02-23
      • 2011-01-19
      • 1970-01-01
      • 2014-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多