Java EE 新手;服务/守护进程的架构建议?

发布于 2024-09-04 23:09:13 字数 490 浏览 7 评论 0原文

我是 Java EE 世界的新手。作为尝试熟悉 Java EE 的练习,我正在尝试创建一个分层的 Web 应用程序,但我有点困惑最好的方法是在后台启动一个确实有效的服务。

服务的参数:

  • 它必须打开并保持套接字连接并从连接的服务器接收信息。
  • 用户和新的套接字连接之间存在一对一的关联。

所以这个想法是用户按下网页上的按钮,然后服务器上的某个地方打开套接字连接。对于用户会话的剩余时间(或者直到用户按下某种断开连接按钮),套接字保持打开状态并将接收到的信息推送到某种集中式存储,servlet 可以通过 AJAX 查询并返回给用户。

有没有 Java EE 类型的方法来处理这种情况?当然,我想做的就是编写一个 Java 应用程序,该应用程序侦听 servlet 可以连接的端口,并生成打开这些套接字的新线程,但这对我来说似乎非常特别。

(PS:我也是 Stack Overflow 的新手,所以如果我需要一些时间来弄清楚这个网站,请原谅我!)

I am brand new to the Java EE world. As an exercise to try and familiarize myself with Java EE, I'm trying to create a tiered web-app, but I'm getting a little stuck on what the best way is to spin up a service in the background that does work.

Parameters of the service:

  • It must open and hold a socket connection and receive information from the connected server.
  • There is a 1-to-1 correlation between a user and a new socket connection.

So the idea is the user presses a button on the web-page, and somewhere on the server a socket connection is opened. For the remainder of the users session (or until the user presses some sort of disconnect button) the socket remains open and pushes received information to some sort of centralized store that servlets can query and return to the user via AJAX.

Is there a Java EE type way to handle this situation? Naturally what I would think to do is to just write a Java application that listens on a port that the servlets can connect to and spawns new threads that open these sockets, but that seems very ad-hoc to me.

(PS: I am also new to Stack Overflow, so forgive me if it takes me some time to figure the site out!)

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

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

发布评论

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

评论(1

高速公鹿 2024-09-11 23:09:13

Java EE 堆栈中存在三个主要容器:Web 容器、EJB 容器和 JCA 容器。 JCA 旨在提供与第三方系统(例如数据库、JMS 代理或其他系统)的入站和出站连接。

从 EJB 或 Web 应用程序创建到 Telnet 服务器的连接的“正确”方法是使用 JCA 连接器。

[client] <-|-> [web] <--> [ejb] <--> [jca] <-|-> [telnet server]

管道 |表示远程边界。假设 EJB 是本地的,但它们无论如何都是可选的;您也可以从 Web 层使用 JCA 连接器。

我建议您调查是否有现有的实施。快速谷歌给了我这个结果: 用于 Telnet 的 JCA 连接器客户端

另一种方法(但不符合规范)是启动从 ServletContextListener 侦听套接字的线程。该线程将在 Web 层运行,您可以根据需要管理与 Telnet 服务器的连接。

我建议您也看看另一个问题: 侦听套接字的 Java EE 应用程序

在这两种情况下,您可能需要弄清楚如何临时存储 Telnet 服务器(您提到的集中存储)接收到的信息,这些信息稍后将显示在 Web 界面中。这对于 Java EE 来说又是一个问题,因为规范禁止使用全局状态。例如,理论上您不应该使用static字段。但实际上,如果您的应用程序只运行一个实例,那么这种方法是有效的。

这只是一个粗略的草图,但我希望它能有所帮助。

There are three main containers in the Java EE stack: the Web container, the EJB container, and the JCA container. JCA is meant to provide inbound and outbound connectivity with third-party systems, such as database, JMS broker, or others.

The "right" way to create an connection to a Telnet server from an EJB or web app would be to use a JCA connector for that.

[client] <-|-> [web] <--> [ejb] <--> [jca] <-|-> [telnet server]

The pipe | denotes remote boundaries. A assume EJB are locals, but they are optional anyway; you can use JCA connector from the web layer also.

I suggest you investigate if there are existing implementation. A quick google gave me this result: JCA connector for Telnet client.

Another approach (but not compliant with the spec), is to start the thread that listens to the socket from a ServletContextListener. The thread will run in the web layer and you can manage connectivity with the Telnet server as you wish.

I suggest you have also a look at this other SO question: Java EE application that listens to a socket.

In both cases, you will probably need to figure out how to temporary store the information received by the Telnet server (the centralized store that you mention) that will later be displayed in the web interface. This is again problematic with Java EE, because the spec forbid the usage of global state. For instance, you should not use static field in theory. But in practice that works if you have only one instance of your app running.

That's only a rough sketch, but I hope it helps.

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