Java EE新手;服务/守护进程的架构建议? [英] New to Java EE; architecture suggestions for a service/daemon?

查看:143
本文介绍了Java EE新手;服务/守护进程的架构建议?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

服务参数:


  • 必须打开并保持套接字连接

  • 用户与新套接字连接之间存在一对一的关联。

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

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.

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

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:我也是Stack Overflow的新手,所以请原谅我,如果我需要一些时间来计算网站!)

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

推荐答案

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

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.

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

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]

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

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.

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

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

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

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.

我建议你看看这个其他的SO问题:侦听套接字的Java EE应用程序

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

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

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.

这篇关于Java EE新手;服务/守护进程的架构建议?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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