Future Call with type Void [closed]

-1

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;
    
asked by anonymous 28.02.2018 / 20:24

1 answer

1

Actually in this case the problem was another one, I thought that due to createPDFReport triggering the response, the method that invalidated the key was executing before the response reached the browser. But I discovered that the problem only occurred in Edge, this browser strangely makes 2 requests to print the pdf, and in the first it invalidated the key.

To solve:

String userAgent = req.getHeader("user-agent");
createPDFReport(resp, param, jasperPath, "rendimentosCopart.pdf");
if(userAgent.contains("Edge")){
        String dlnaHeader = req.getHeader("getcontentfeatures.dlna.org");
        if(dlnaHeader != null)
            RelatoriosRhevDelegate.invalidaChave(chave);
    
01.03.2018 / 13:13