views:

127

answers:

1

I am brand new to the JEE world. As an exercise to try and familiarize myself with JEE, 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 JEE 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!)

+2  A: 

There are three main containers in the JEE 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: JEE 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 JEE, 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.

ewernli
@Ewernli This looks promising! Would you say this is still a good article to learn from (written in Dec 2001): http://java.sun.com/developer/technicalArticles/J2EE/connectorclient/resourceadapter.html
Kate
@Kate The link that you mention is ok, but IMHO, the best doc about JCA to start with is "Creating Resource Adapter with J2EE Connector Architecture 1.5". The corresponding code can be found in the J2EE samples which come with the SDK. http://developers.sun.com/appserver/reference/techart/resource_adapters.pdf Otherwise google for Frank Kievet, I think he had great articles. Other of my resources include two blog posts I wrote about it http://www.ewernli.com/web/guest/52 http://www.ewernli.com/web/guest/51 and a few other SO answers: http://stackoverflow.com/search?q=user%3A217862+jca.
ewernli
@Ewernli I don't know how Stack Overflow's rating system works, but I will definitely check out these two articles and have a go at shelling out a solution. If I don't run into any intractable problems, I'll mark your answer as a solution! Thanks so much!
Kate