有一些批處理的東東,後臺執行可能超時,若是等最後遇到錯誤或者有什麼結果才返回,每每會超時,並且整個執行過程的細節也都無法看到,是個黑盒,不大方面調試。這裏講一下若是實現相似websocket的效果,就是後端不斷往流裏頭寫文本。html
@Controller @RequestMapping("/echo") public class EchoController { @RequestMapping(value = "/reply",method = RequestMethod.GET) public void writeStream(HttpServletResponse response) throws IOException, InterruptedException { response.setContentType("text/html;charset=utf-8"); for(int i=0;i<1000;i++){ write(response,"hello"); Thread.sleep(1000*2); System.out.println("send"); } response.getWriter().close(); } private void write(HttpServletResponse response,String content) throws IOException { response.getWriter().write(content+"<br/>"); response.flushBuffer(); response.getWriter().flush(); } }
➜ ~ wget http://localhost:8080/echo/reply --2017-06-14 16:08:14-- http://localhost:8080/echo/reply Resolving localhost... ::1, 127.0.0.1 Connecting to localhost|::1|:8080... connected. HTTP request sent, awaiting response... 200 Length: unspecified [text/html] Saving to: 'reply' reply [ <=> ] 60 5.99 B/s
而後tail一下web
➜ ~ tail -f reply hello<br/>hello<br/>hello<br/>hello<br/>hello<br/>hello<br/>hello<br/>hello<br/>hello<br/>hello<br/>hello<br/>hello<br/>hello<br/>hello<br/>hello<br/>hello<br/>hello<br/>hello<br/>hello<br/>hello<br/>hello<br/>hello<br/>hello<br/>hello<br/>hello<br/>hello<br/>hello<br/>hello<br/>hello<br/>hello<br/>hello<br/>hello<br/>hello<br/>hello<br/>
或者用瀏覽器訪問就能夠看到效果了.後端
直接curl貌似看不到效果瀏覽器
想獲取最新內容,請關注微信公衆號微信