Java Rest API: how to check connection between client and endpoint?

江枫思渺然 提交于 2020-01-16 05:47:05

问题


How I can check if user who sent rest request aborted it using Ctrl-C? I need to check it on server side because it takes several minutes. If user aborted it then I should stop it.

I tried to write to ServletOutputStream but it doesn't work (I never catch exception):

@Path("test")
public void test(@Context MessageContext mc) {
  try {
     ServletOutputStream out = mc.getHttpServletResponse().getOutputStream();
     out.println("test");
     out.flush();
  } catch (IOException e) {
     System.out.println("Connection is broken");
     return;
  }
}

Found solution:

    private boolean isConnected() {
    try {
        servletResponse.getOutputStream().println("data");
        servletResponse.flushBuffer();
        return true;
    } catch (IOException e) {
        return false;
    }
}

回答1:


private boolean isConnected() {
    try {
        servletResponse.getOutputStream().println("data");
        servletResponse.flushBuffer();
        return true;
    } catch (IOException e) {
        return false;
    }
}


来源:https://stackoverflow.com/questions/17523118/java-rest-api-how-to-check-connection-between-client-and-endpoint

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!