0

To handle long the running process we are using SpringMVC DeferredResult to send the data to the browser, but after certain period of time the browser returns no data received, while the process is still running in the back end. it works fine in localmachine but when it is deployed in amazon EC2 instance we face the problem. how to solve this issue and how to send data periodically to Browser.

Here is my code where the process is delayed by 90 sec

 @RequestMapping("/myDefferedResult1") 
        @ResponseBody 
        public DeferredResult<List<String>> myDefferedResult2(final HttpServletResponse httprep) {


          final DeferredResult<List<String>> deferredResult = new DeferredResult<List<String>>(null, Collections.emptyList());
            this.chatRequests.put(deferredResult, 0);

            try {
                Thread.sleep(90000L);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            deferredResult.onCompletion(new Runnable() {
                @Override
                public void run() {

                    System.out.println("completeion");
                    chatRequests.remove(deferredResult);
                }
            });

            List<String> messages = new ArrayList<String>();
            messages.add("hi");
            messages.add("hello");

            if (!messages.isEmpty()) {
                deferredResult.setResult(messages);
                System.out.println("setresult");
            }

            return deferredResult;


        } 

the browser returns No data received after sometime

Arun Kishore
  • 119
  • 3
  • 12

1 Answers1

0

The main problem might be either on the browser side or there might be a proxy on the way with timeout set. In either case it's caused by configuration set outside your application. Try rather toimplement some sort of polling/SockJs/Websockets mechanism. In general it's much safer.

JiriS
  • 6,870
  • 5
  • 31
  • 40
  • I need to respond browser as soon as process done with the result. Why deferred result not maintaining the connection with browser in my case? – Arun Kishore Jun 05 '15 at 14:44
  • Because you're not sending back any data for extended period of time either proxy or browser considers the connection to be dead. Your better option is to poll server for the data every now and then (e.g. every second) or use some sort of push mechanism. – JiriS Jun 05 '15 at 14:50
  • Can you post some example? – Arun Kishore Jun 05 '15 at 14:56
  • can you edit our code to send data for every 2 secs till the back end process is getting completed? – Arun Kishore Jun 05 '15 at 15:18