The following programs exist: 1. I have a java application which accepts bio potential data every second or two and stores it in the database. This is a socket server which accepts this data from multiple clients and spawns a new thread for processing it to store in the db. 2. I have a jsp page on tomcat server which reads historic client data from database (stored by application 1) and displays it on the page.
The socket server program in 1.) above is not running inside of tomcat server.
The new requirement now is : Display all of the human data coming in live on the jsp page.
Now the problem: I will now need to pass the live data from socket server (which is stand alone) to the jsp which is running on a tomcat server.
APPROACH 1: Run the socket server in the tomcat instead of stand alone and store the frequently incoming data in a java object so the jsp can access this object every second and display it on a graph.
PROBLEM : The stand alone java application does not need to be included in a tomcat server except for the fact that the jsp needs access to the live data. Also, I have read that this is not the best way.
APPROACH 2: Expose the stand alone java application as a web service and communicate with the jsp using REST architecture.
PROBLEM : The complication of using this method is that it will not have the flexibility offered by websockets or server sent events (SSE) of auto updating the latest data. The jsp will have to keep polling for new data every one second which is also not a very good option.
I need suggestions on which is a better method for accomplishing my task. Or is there a third better way which I have completely missed.
For approach 1, when you say "run socket server in tomcat instead of stand-alone", do you mean inside the web app? If this was what you meant, then no, it is not practical.
About fetching data, doesn't the JSP always have the latest data from the database (eg up to the last record in the table at the time of the query)? Does the socket server and web app use the same database?
Lastly, is it realistic to update the output page every second? The JSP output I presume is user triggered, if such page is updated (refreshed) every second, the page would be flickering all the time!
I can suggest: Run the stand-alone socket server OUTSIDE tomcat container in the same (physical) server. Have the JSP page access the database used by socket server to get the data. (this would just be a configuration issue)
I concur with not integrating the socket server with the web app. In fact, they should be entirely decoupled (so no web service either) - data gathering and data display are unrelated issues. You want either activity to continue even if the other is shut down for some reason.
Display of live data calls for some AJAXy thing on the web page - I am certain there are jQuery plugins that not only display live data, but can also be configured to update it every so often. That also avoids what K. Tsang refers to as "flickering", because the page would not reload.
You need to specify what "live" means, though - obviously, real-time is not possible, there will always be a delay. So the question is how big a delay is acceptable - a maximum delay of 2 seconds might need other approaches than one of 60 seconds.
Thank you K. Tsang and Ulf Dittmer. As you both rightly pointed out, I can make the JSP read the database directly rather than using the more complicated approaches I was thinking of. Thank you so much for pointing me in the right direction. Your responses have been most helpful.