Situation
I needed to print a pdf report via a void method that would return the response and after printing on the screen it became necessary to invalidate the permission key directly in the database.
Problem
Shortly after the print call, the key was invalidated at the bank without giving time to print the report. I made attempts with thread sleep, but in success.
Solution Create a Future and wait for Thread to finish updating the database.
Implementation
String chave = req.getParameter("chave");
final HttpServletResponse respFuture = resp;
final Map paramFuture = param;
final String jasperPath = getServletContext().getRealPath("/jasper/rendimentosCopart/rendimentosCopart.jasper");
try {
param.put("P_CHAVE", chave);
param.put("REPORT_LOCALE", new Locale("pt","BR"));
// Gerando relatório
ExecutorService executor = Executors.newFixedThreadPool(2);
Future<Void> future = executor.submit(new Callable<Void>() {
@Override
public Void call() throws Exception {
try{
createPDFReport(respFuture, paramFuture, jasperPath, "rendimentosCopart.pdf");
} catch (ReportException e) {
e.printStackTrace();
}
return null;
}
});
while (!future.isDone()) {
Thread.sleep(1000);
}
future.get();
executor.shutdown();
RelatoriosRhevDelegate.invalidaChave(chave);
} catch (Exception e){
IRelatoriosRhev.LOG.error(e.getMessage());
}
break;