@Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { if (isAsync()) { final AsyncContext context = req.startAsync(); //run async context.start(() -> doServiceAsync(context)); } else { doService(req, resp); } }
/** * This is used to handle request asynchronously * @param context the {@link AsyncContext} */ protected void doServiceAsync(AsyncContext context) { final HttpServletRequest request = (HttpServletRequest) context.getRequest(); final HttpServletResponse response = (HttpServletResponse) context.getResponse(); try { doService(request, response); } catch (Exception e) { //An error shouldn't occur as we should handle most of error in doService log.error("Error processing request", e); try { response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); } catch (Exception e1) { log.debug("Cannot send reply to client!", e1); } //Need to wrap it in RuntimeException as it occurs in a Runnable throw new RuntimeCamelException(e); } finally { context.complete(); } }