如何从 j2ee 中长时间运行的进程中获得即时响应?
我似乎无法在任何地方找到可靠的答案。我想我找到了一个与 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这实际上取决于您使用什么 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.
如果我理解正确的话,您所说的是启动任务(这将需要一些时间)然后在该任务仍在执行任务时响应用户的能力。根据您的要求,这实际上非常简单,您可以使用普通的旧 Java 线程来执行该操作。
在您的设置页面中,您可以这样做:(会话是一个 HttpSession)
在您的状态页面中,您可以这样做:
当然,这是一个相当无用的示例,当您可能有大量会话时,此模型不是一个好主意如果要满足请求,则值得考虑 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.
In your setup page you might do this: (session is an HttpSession)
And in your status page you might do this:
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.