J2EE - 实现持续运行的组件/守护进程
我正在设计一个服务器应用程序,它应该连续处理大量数据并使用 Web 界面按需呈现结果。
操作方案大致如下:
- 电子传感器阵列通过 USB 不断将数据溢出到 ramdisk
- 一个“flusher”应用程序尽可能快地处理数据并将其加载到 db(暂存区)中
- 使用触发器,db 对数据执行计算并存储结果在另一个模式(数据区域)
- 客户端 web 应用程序可以根据需要在图形/报告等中显示已处理的数据
该解决方案理想情况下如下所示:
- 数据库服务器 - PostgreSQL
- 有一个管理 Web 界面,可以监视刷新器(即每个处理的记录)小时或类似的东西),如果作为单独的守护进程实现,则控制它。
- 用 Java 编写的 Flusher 和客户端应用程序,最好使用 J2EE
现在,这个问题一直困扰着我,而且我找不到答案:如何编写 Flusher 组件,即在 J2EE 中不断在后台运行的进程。
通过搜索网络,基本上出现了三种可能性:
a) 将冲刷器编写为消息驱动 bean 并使用 JMS 从主应用程序控制它。但是:我不喜欢让 MDB 持续运行的想法,我什至不确定这是否可能
b) 将刷新器编写为 EJB 并使用计时器/调度服务控制它。然而:这些事件并不是真正定时的,它只需要无限循环运行,直到被告知不要这样做,这似乎是对该技术的错误使用。
c) 将冲刷器编写为单独的 java 应用程序,将其作为操作系统服务(Linux 或 Windows)运行,并通过从 EJB 调用的 ProcessBuilder 使用启动脚本进行控制。要监视其状态,请使用 JMS。然而:在我看来,这似乎是过于复杂的解决方案,依赖于平台,甚至可能不可靠,并且 EJB 不应该生成/管理它自己的线程,而 ProcessBuilder 基本上就是这样做的,这似乎是错误的。
基本上,这些对我来说都不合适,我无法弄清楚,在 Java/J2EE 世界中什么是正确的解决方案。
谢谢 托马斯
I am designing a server application, that is supposed to crunch a lot of data continuously and present results on demand using web interface.
The operating scheme goes roughly like this:
- An electronic sensor array constantly spills data into ramdisk through USB
- A "flusher" application processes data as fast as it can and loads it into db (staging area)
- Using triggers, db performs calculations on data and stores results in another schema (data area)
- Client webapp can display processed data in graphs/reports etc. on demand
The solution would ideally look like this:
- Database server - PostgreSQL
- Have an administration web interface, that can monitor the flusher (i.e. records processed per hour or something like that) and if implemented as separate daemon, control it.
- Flusher and Client applications written in Java, ideally using J2EE
Now the problem that keeps bugging me and I can't find the answer: How to go about writing the flusher component, i.e. a process that constantly runs in background in J2EE.
By scouring the web, basically three possibilities emerged:
a) Write the flusher as message driven bean and control it from master application using JMS. However: I don't like the idea of having a MDB running constantly, I'm not even sure that that's possible
b) Write the flusher as EJB and control it using Timer/Scheduling service. However: the events are not really timed, it just needs to run in infinite loop until told not to do so, just seems wrong usage of the technology.
c) Write the flusher as separate java application, run it as OS service (Linux or Windows) and control using startup scripts through ProcessBuilder invoked from EJB. To monitor it's status, use JMS. However: this just seems to me as overly complicated solution, platform dependent and maybe even unreliable and as EJB should not spawn/manage it's own threads, which ProcessBuilder basically does, it just seem wrong.
Basically, none of these look right to me and I cannot figure out, what would we the right solution in the Java/J2EE world.
Thank you
Thomas
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会将“Flusher”应用程序编写为独立的 Java 进程。也许可以使用诸如 Java Service Wrapper 之类的东西将其转变为适用于您的操作系统的服务。我不太熟悉通过 Java 与 RAM 磁盘交互的选项,但是您要么最终得到一个 InputStream,您可以在进程的整个生命周期中保持打开状态并不断读取,要么您将在 while 循环内不断轮询。执行如下操作是完全可以的:
然后,当您想要终止进程时,另一个线程中将有一些其他机制可以将 stopFlag 设置为 true。
至于监控冲洗器 JMX 似乎是一个很好的解决方案。这正是它的目的。您将创建一个 MBean,它将公开您想要的任何类型的状态或统计信息,然后其他进程可以连接到该 MBean 并查询该数据。
“客户端”应用程序将是一个简单的 servlet 应用程序,它对您的数据库进行报告,并为您的刷新器中的 MBean 提供一个漂亮的前端。或者,您可以只使用 JMX 控制台监控刷新器,甚至不需要让客户端参与系统的该部分。
我认为 EJB 对于这个系统来说并没有真正的意义。我对 EJB 有一定的偏见,因此请对我的建议持保留态度,但对我来说,我并不认为此应用程序需要它们。
I would write the "Flusher" app as a stand alone Java process. Perhaps use something like Java Service Wrapper to turn it into a service for your OS. I'm not very familiar with the options for interfacing with a RAM disk via Java, but you're either going to end up with an InputStream which you can keep open for the life of the process and continually read from, or you're going to continually poll from inside a while loop. It's perfectly ok to do something like the following:
Then you would have some other mechanism in another thread that could set stopFlag to true when you wanted to terminate the process.
As for monitoring the flusher JMX seems like a good solution. That's exactly what it was intended for. You would create an MBean that would expose any kind of status or statistics you wanted and then other processes could connect to that MBean and query for that data.
The "Client" app would then be a simple servlet application which does reporting on your database and provides a pretty front end for the MBean from your flusher. Alternatively you could just monitor the flusher using a JMX console and not even involve the client with that piece of the system.
I don't think EJBs really make sense for this system. I'm somewhat biased against EJBs, so take my advice with a grain of salt, but to me I don't really see a need for them in this application.