Future call with Void type

Asked

Viewed 39 times

-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;
  • 2

    Yeah, but what’s the doubt?

  • 1

    The method get() Future should already wait to finish the execution, I think you do not need the while on top of that get.

  • 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?

  • If it is not blocking, it makes no sense to call it in a separate thread.

1 answer

1

In fact in this case the problem was another, I thought that due to createPDFReport triggering the Reset, the method that invalidated the key was running before the Reset arrived at the browser. However I found that the problem only occurred in Edge, this browser strangely makes 2 requests to print the pdf, and in the first already 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);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.