Currently i am trying below code.The problem is after timeout(5 seconds) catch (TimeoutException e) block is getting execute but not able to cancel execution of query. I want to cancel execution after timeout and continue with next part of code.
ExecutorService executor = Executors.newSingleThreadExecutor();
Future<String> future = executor.submit(new Callable() {
public String call() throws Exception {
System.out.println("task running!");
ResultSet rs = psLoadingStr.executeQuery();
return "OK";
}
});
try
{
future.get(5, TimeUnit.SECONDS); //timeout is in 5 seconds
}
catch (TimeoutException e)
{
System.err.println("Timeout");
future.cancel(true);
}
executor.shutdownNow();