AsyncContext and I/O error handling (when peer disconnects)

后端 未结 2 1613
情书的邮戳
情书的邮戳 2021-01-06 03:58

I\'m implementing Server-Sent Events using Servlet 3.0\'s javax.servlet.AsyncContext interface.

However I can\'t understand how I should handle I/O errors like peer

相关标签:
2条回答
  • 2021-01-06 04:36

    There's an alternative solution, in cases where it's more convenient to use getWriter().

    PrintWriter out = ac.getResponse().getWriter();
    out.print(stuff);
    out.flush(); // swallows IOException
    if (out.checkError()) {
       // handle error or throw out...
    }
    

    That is, the PrintWriter class does provide a method to retrieve write errors later.

    0 讨论(0)
  • 2021-01-06 04:45

    The problem is that: ac.getResponse.getWriter().flush() does not throw IOException

    So in order to get a error notification upon I/O operation you need to use ServletOutputStream instead:

    try {
       ServletOutputStream out = ac.getResponse().getOutputStream();
       out.print(stuff);
       out.flush(); // throws IOException
    }
    catch(IOException e) {
       // handle a error
    }
    
    0 讨论(0)
提交回复
热议问题