EJB 如何并行化一个长的、CPU 密集型的进程? [英] How can an EJB parallelize a long, CPU intensive process?

查看:25
本文介绍了EJB 如何并行化一个长的、CPU 密集型的进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

应用程序有一个 CPU 密集型的长进程,当客户端请求它时,它当前在一个服务器上串行运行(一种 EJB 方法).

The application has a CPU intensive long process that currently runs on one server (an EJB method) serially when the client requests it.

理论上可以(从概念的角度来看)将该进程拆分为 N 个块并并行执行它们,只要可以收集所有并行作业的输出并将其连接在一起,然后再将其发送回客户端启动了这个过程.我想使用这种并行化来优化性能.

It’s theoretically possible (from a conceptual point of view) to split that process in N chunks and execute them in parallel, as long as the output of all parallel jobs can be collected and joined together before sending it back to the client that initiated the process. I’d like to use this parallelization to optimize performance.

如何使用 EJB 实现这种并行化?我知道我们不应该在 EJB 方法中创建线程.相反,我们应该发布消息(每个作业一个)以供消息驱动 bean (MDB) 使用.但是这样就不再是同步调用了.在这种情况下,同步似乎是一项要求,因为我需要在将所有作业的输出发送回客户端之前收集它.

How can I implement this parallelization with EJBs? I know that we should not create threads in a EJB method. Instead, we should publish messages (one per job) to be consumed by message driven beans (MDBs). But then it would not be a synchronous call anymore. And being synchronous seems to be a requirement in this case since I need to collect the output of all jobs before sending it back to the client.

有没有办法解决这个问题?

Is there a solution for this?

推荐答案

这个特定的问题已经多次出现,我总结一下有几种可能的解决方案,我只推荐其中一种.

This particular question has come up on multiple occasions and I will summarize that there are several possible solutions, only 1 of which I would recommend.

使用来自 commonj API 的 WorkManager.它允许在 Java EE 容器中使用托管线程,并且专门设计用于适合您的用例.如果您使用的是 WebSphere 或 WebLogic,这些 API 已经在您的服务器中可用.对于其他人,您必须自己使用第三方解决方案.

Use a WorkManager from the commonj API. It allows for managed threads in a Java EE container and is specifically designed to fit your use case. If you are using WebSphere or WebLogic, these API's are already available in your server. For others your will have to put a third party solution in yourself.

WorkManager 信息

相关问题为什么不鼓励生成线程

这篇关于EJB 如何并行化一个长的、CPU 密集型的进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆