Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

New to Java EE; architecture suggestions for a service/daemon?

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!)

like image 581
Kate Avatar asked Oct 14 '22 04:10

Kate


1 Answers

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.

like image 186
ewernli Avatar answered Oct 19 '22 02:10

ewernli