Reputation: 305
I currently have a client -> server model where the client periodically pushes data. I am using Jersey with servlets on the server to respond to the incoming requests.
Since this is a data synchronization step, once the data gets received by the server, an OK message needs to be returned to the client.
As a test, I made a path the server handles as "/testResponse" where all it does is this:
@GET
@Path("/testResponse")
@Produces(MediaType.TEXT_PLAIN)
public String GET_testResponse() {
System.out.println("Received test request...");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Returning response...");
return "message received!";
}
What I wanted to test is I initiate the request from my client, but then while the client is waiting 5 seconds, the client exits the request, simulating it disconnecting from wireless. After those 5 seconds are up, the server returns the message (with no error being thrown).
My question I guess is simple: Is there any way for the server to know if the client received the response since no exceptions were thrown etc? Should an error be thrown when you try to handle the response? I know outside of jersey, you can use the HttpServletResponse method to actually respond before the function exits, so I'm wondering if I'd need something like that.
Thanks!
Upvotes: 4
Views: 2517
Reputation: 24306
Even if the client completed the HTTP request, there is no way for the server to know if the client completely processed the response. For that reason, it's best to design systems where the server doesn't need to know if the client received the response
One possible way to do this is to make your requests idempotent. If the client can determine that the request failed, it can simply try the same request again, and regardless of whether the server had already processed the request the result would be the same. So if the client is on a smart phone, and the battery dies before the client could process the request, the next time the client was started it could check if there are any outstanding requests to send, and retry.
Upvotes: 6