This can easily be obtained by putting your coded in a Thread, start it off like this:
private static class TimeoutJob implements Runnable {
private Response resp;
public void run() {
resp = ResponseSender.getResponse();
}
public Response getResponse() {
return resp;
}
in your code put this:
TimeoutJob tj = new TimneoutJob();
Thread t = new Thread(tj);
t.start();
t.join(1000); // try to join the thread so waiting for the response to comeback, having a timeout of 1000 milliseconds
if (tj.getResponse() != null) // -> you have a response...
There is an interrupted exception that needs to be catched so this code is not 100% complete but you catch my drift.
Note: that this will give you the timeout capability but the ResponseSender is still running the getResponse() code and you did not interuupted that. You probably are better of in redesinging the ResponseSender class to support timeouts and properly close resources when a timeout occurs...