In my web-app the client is both the server and client at the same time. Just like in peer to peer applications.After the client starts the web-app, first thing that is done is : Check if the internet connection is available (As the jsp page is loaded ). If the connection is available,node information is sent over to the remote database. This is just to know the addresses of the nodes connected to the main server.
Now,if the node gets disconnected to the server (due to failure of internet connection or whatever) I want the node’s information to be removed from the database.
One way,I thought to implement this is to start a thread on the main server that keeps attempting a connection to all the nodes there in the database. But I am not happy with this option. Might be there is another way around. Or may be I can implement this in a better way.
Please suggest, how do I do this ?
And I have to start a thread on the main server, shall I call the thread function from the jsp page ? Like :
<%-- This is a jsp page --%>
.....
........
<%-- start a new thread that does that task --%>
<% new ThreadClass().startContinuousThread(); %>
......
.........
Note: Each node connects to the central server as :
final URL url = new URL("http://sevrer/servlet");
final HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
int responseCode = urlc.getResponseCode();
if(responseCode == 200){
connected = true;
System.out.println("Initial connection to the server successful");
}
// send the node information to the servlet via a 'parameterized query'.
Instead of doing this check on the server side, I would suggest you to do this check on the on the client send. Something like sending a ping request at a configured time interval.
Just like your code that connects to the server, write code which calls another servlet periodically.
The server would just keep a timer task which simply checks the last ping request that arrived for each node entry in the server. If the ping entry for any of the nodes did not arrive in the configured timeout interval then the server would clean up the node from it’s database.
The advantage with this solution is your server would not be overloaded with initiating the requests everytime.However, I don’t see any improvements on I/O side, but the server side logic would be very clean and simple with this solution.