Java EE 提交表单并让后台进程继续运行?
我需要用java(从oracle导出数据)生成一个报告,根据涉及的数据量,这可能需要几分钟的时间,所以当我提交表单时,屏幕会冻结直到结束。
如何在后台进程中生成报告,以便用户可以继续浏览?
JMS?我如何使用 JMS 做到这一点? (欢迎任何例子或解释)
还有其他方法吗? 根据 Java EE 规范,最佳选择是什么?
I need to generate a report with java (exporting data from oracle) that can take several minutes, by the amount of data involved, so when I submit my form the screen freezes until it ends.
How can I generate the report in a background process, so that the user can continue browsing?
JMS? How can i do this with JMS? (Any example or explanation is welcome)
Is there another way?
What would be the best option according to the Java EE specification?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,JMS 可能是一个选择。您只需将消息发送到队列,消息驱动 Bean(一种特殊的 EJB)就会接收并处理该消息。在这种情况下,该消息的作用类似于命令。虽然您可以在没有消息驱动 Bean 的情况下使用 JMS,但它有点复杂。
如果您处于 EJB 3.1 环境中,您可以尝试 异步 EJB。
否则,虽然规范不建议这样做,但您可以在 Web 容器中启动线程。您可以为每个作业启动一个线程,也可以在 ServletContextListener 中启动/停止一个后台线程,该线程处理您存储在数据库表中的命令。
使用异步作业表面上非常简单,但对于生产,您需要考虑如何管理错误、监控进度、重试失败的作业、确保不会同时运行两个相同的作业等。每种方法都有自己的优势和弱点。还要注意事务所需的策略(JMS 是事务处理的,我不太清楚异步 EJB 3.1 的情况。如果使用 JDBC 事务或 UserTransaction 正确执行操作,则可以对自定义线程和数据库表进行事务处理) /代码>)。
希望有帮助。
Yes, JMS might be an option. You simply send a message to a queue, and a Message Driven Bean -- a special kind of EJB -- will then receive and process the message. The message acts in this case like a command. You can use JMS without Message Driven Bean though, but it's a bit more complicated.
If you are in a EJB 3.1 environment, you could give a try to asynchronous EJB.
Otherwise, while it's not recommended by the spec, you could start a thread in the web container. Either you start one thread per job, or you could start/stop one background thread in a
ServletContextListener
, which processes commands that you store, say, in a table in a database.Using asynchronous jobs is superficially very easy, but for production you will need to think about how to manage error, monitor progress, retry failed job, ensure that no two same job are run at the same time, etc. Each approach has its own strength and weakness. Pay also attention to the strategy you need for the transactions (JMS is transacted, I don't know exactly for async EJB 3.1., and custom thread and database table can be transacted if do things correctly with JDBC transaction or
UserTransaction
).Hope it helps.