如何从 j2ee 中长时间运行的进程中获得即时响应?

发布于 2024-12-07 00:32:54 字数 52 浏览 1 评论 0原文

我似乎无法在任何地方找到可靠的答案。我想我找到了一个与 JMS 相关的内容,但它令人困惑。

I can't seem to find a solid answer anywhere. I THINK i found one with respect to JMS but it was confusing.

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

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

发布评论

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

评论(2

ゝ杯具 2024-12-14 00:32:54

这实际上取决于您使用什么 j2EE 堆栈?是只是 web 层、ejb 层还是两者兼而有之?如果我们谈论的是 Web,那么您可以使用最新 Java EE 规范中引入的异步 servlet,如果您使用的是普通 EJB,那么自然的选择将是 Messege 驱动的 beans(提到了 JMS)。您当然可以设计一个自定义解决方案,例如发送一些数据进行处理,然后 j2ee 应用程序本身调用您的应用程序(例如使用 http 请求)以通知其已完成运行作业。可能性是无限的,其中一种是否比另一种更好总是取决于具体情况。

It really depends what stack of j2EE are you using? Is it just web, ejb layer or both?. If we are talking about the web then you can use asynchronous servlet introduced in the newest Java EE specification, if you are using plain EJB's then the natural choice would be Messege driven beans (mentioned JMS). You can of course design a custom solution where for example you send some data to process and then the j2ee application itself calls your application (with http request for example) to notify that its done running the job. Possibilities are endless and if one is better than other always depends on the specific scenario.

栀梦 2024-12-14 00:32:54

如果我理解正确的话,您所说的是启动任务(这将需要一些时间)然后在该任务仍在执行任务时响应用户的能力。根据您的要求,这实际上非常简单,您可以使用普通的旧 Java 线程来执行该操作。

public class DoSillyCounting extends Thread {
    private volatile int counter;
    public int getCounter() { return counter; }
    public run() {
        while (counter < 10) {
            counter ++;
            try { Thread.sleep(1000); }
            catch (InterruptedException ie) { }
        }
    }
}

在您的设置页面中,您可以这样做:(会话是一个 HttpSession)

DoSillyCounting doSillyCounting = new DoSillyCounting();
doSillyCounting.start();
session.putValue("tenSecondsCounter", doSillyCounting);
/* Here you can respond to the user while the Thread is executing */

在您的状态页面中,您可以这样做:

DoSillyCounting doSillyCounting =
    (DoSillyCounting)session.getValue("tenSecondsCounter");
out.print(Integer.toString(doSillyCounting.getCounter());
if (doSillyCounting.isAlive()) {
    out.print("Still Working on it");
} else {
    out.print("Yippee, I finished");
}

当然,这是一个相当无用的示例,当您可能有大量会话时,此模型不是一个好主意如果要满足请求,则值得考虑 ThreadPool 实现或使用 JMS 之类的东西。

If I understand correctly what you are talking about is the ability to start a task (that will take some time) then respond to the user while that task is still doing it's stuff. Depending on your requirements it is really quite simple and you can use a plain old Java Thread to perform the operation.

public class DoSillyCounting extends Thread {
    private volatile int counter;
    public int getCounter() { return counter; }
    public run() {
        while (counter < 10) {
            counter ++;
            try { Thread.sleep(1000); }
            catch (InterruptedException ie) { }
        }
    }
}

In your setup page you might do this: (session is an HttpSession)

DoSillyCounting doSillyCounting = new DoSillyCounting();
doSillyCounting.start();
session.putValue("tenSecondsCounter", doSillyCounting);
/* Here you can respond to the user while the Thread is executing */

And in your status page you might do this:

DoSillyCounting doSillyCounting =
    (DoSillyCounting)session.getValue("tenSecondsCounter");
out.print(Integer.toString(doSillyCounting.getCounter());
if (doSillyCounting.isAlive()) {
    out.print("Still Working on it");
} else {
    out.print("Yippee, I finished");
}

Of course, this is a rather useless example and this model is not a good idea when you may have a large number of requests to satisfy, it would then be worth looking at a ThreadPool implementation or using something like JMS.

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