-1
Situation
I needed to print a PDF report through a void method that returned Replay and after screen printing it became necessary to invalidate the permission key directly in the bank.
Problem
Soon after the print call, the key was invalidated at the bank without giving time to print the report. I performed attempts with thread Sleep, but in success.
Solution Create a Future and wait for Thread to finish to update 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;
Yeah, but what’s the doubt?
– Denis Rudnei de Souza
The method get() Future should already wait to finish the execution, I think you do not need the while on top of that get.
– Douglas
One thing I don’t understand. If you are running the printing on a separate thread, that means the method
createPDFReport()
is blocking. If it is blocking, you might as well call it directly (without needing to executor) and then invalidate the key. Why can’t you do that?– Piovezan
If it is not blocking, it makes no sense to call it in a separate thread.
– Piovezan