@WebServlet(urlPatterns = "/somepath", asyncSupported = true) public class AsyncServlet extends HttpServlet { @EJB private AsyncBean asyncBean; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { AsyncContext asyncContext = request.startAsync(); // The following line will not block and return immediately asyncBean.doAsyncStuff(asyncContext); } // Shortly after this method has ended, thread will be returned to pool }
@Override public void handleAsync() { request.startAsync(); }
@Override public void startAsync(final Executor executor, final Runnable runnable) { req.startAsync(); executor.execute(runnable); }
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) { AsyncContext asyncContext = req.startAsync(req, resp); executor.execute(() -> asyncContext.dispatch("/async")); } }
private void sendErrorResponse(HttpServletRequest req, int code, String error) throws IOException { AsyncContext asyncContext = req.startAsync(); HttpServletResponse response = (HttpServletResponse) asyncContext.getResponse(); response.sendError(code, error); asyncContext.complete(); }
private void sendErrorResponse(HttpServletRequest req, int code, String error) throws IOException { AsyncContext asyncContext = req.startAsync(); HttpServletResponse response = (HttpServletResponse) asyncContext.getResponse(); response.sendError(code, error); asyncContext.complete(); }
protected void service(final HttpServletRequest req,final HttpServletResponse resp) throws ServletException,IOException { final AsyncContext async = req.startAsync(); new kilim.Task() { public void execute() throws Pausable, Exception { try { String result = handler.handle(req,resp); if (result != null) resp.getOutputStream().print(result); } catch (Exception ex) { resp.sendError(500,"the server encountered an error"); } async.complete(); } }.start(); }
@Override protected void handleSuccess(HttpServletRequest request, HttpServletResponse response, UpgradeInfo upgradeInfo, TyrusUpgradeResponse upgradeResponse) throws IOException, ServletException { response.setStatus(upgradeResponse.getStatus()); upgradeResponse.getHeaders().forEach((key, value) -> response.addHeader(key, Utils.getHeaderFromList(value))); AsyncContext asyncContext = request.startAsync(); asyncContext.setTimeout(-1L); Object nativeRequest = getNativeRequest(request); BeanWrapper beanWrapper = new BeanWrapperImpl(nativeRequest); Object httpSocket = beanWrapper.getPropertyValue("connection.connectionHandler.rawConnection"); Object webSocket = webSocketHelper.newInstance(request, httpSocket); webSocketHelper.upgrade(webSocket, httpSocket, request.getServletContext()); response.flushBuffer(); boolean isProtected = request.getUserPrincipal() != null; Writer servletWriter = servletWriterHelper.newInstance(webSocket, isProtected); Connection connection = upgradeInfo.createConnection(servletWriter, noOpCloseListener); new BeanWrapperImpl(webSocket).setPropertyValue("connection", connection); new BeanWrapperImpl(servletWriter).setPropertyValue("connection", connection); webSocketHelper.registerForReadEvent(webSocket); }
@Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { AsyncContext asyncContext = request.startAsync(); asyncContext.setTimeout(5000); asyncBean.doAsync(asyncContext); }
@Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { final AsyncContext context = request.startAsync(); // Asynchronously reads the POSTed input, then triggers #processContent. try { request.getInputStream().setReadListener(new ReadListenerImpl(context)); } catch (IllegalStateException e) { Log.warn("Error when setting read listener", e); context.complete(); } }
private void startAsyncIfNeedIt() { HttpServletRequest req = nreq.servletRequest(); if (!req.isAsyncStarted()) { req.startAsync(); } } }
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) { if (Tracing.currentTracer().currentSpan() == null) { throw new IllegalStateException("couldn't read current span!"); } AsyncContext ctx = req.startAsync(); ctx.start(ctx::complete); } }
@Override public void start(long timeout) { Assert.state(!isCompleted(), "Async processing has already completed"); if (isStarted()) { return; } HttpServletRequest servletRequest = this.request.getServletRequest(); HttpServletResponse servletResponse = this.response.getServletResponse(); this.asyncContext = servletRequest.startAsync(servletRequest, servletResponse); this.asyncContext.addListener(this); if (timeout != NO_TIMEOUT_VALUE) { this.asyncContext.setTimeout(timeout); } }
@Override public void startAsync() { Assert.state(getRequest().isAsyncSupported(), "Async support must be enabled on a servlet and for all filters involved " + "in async request processing. This is done in Java code using the Servlet API " + "or by adding \"<async-supported>true</async-supported>\" to servlet and " + "filter declarations in web.xml."); Assert.state(!isAsyncComplete(), "Async processing has already completed"); if (isAsyncStarted()) { return; } this.asyncContext = getRequest().startAsync(getRequest(), getResponse()); this.asyncContext.addListener(this); if (this.timeout != null) { this.asyncContext.setTimeout(this.timeout); } }
public void handle(String target, Request baseRequest, HttpServletRequest request, final HttpServletResponse response) throws IOException, ServletException { response.setStatus(HttpServletResponse.SC_OK); final AsyncContext asyncContext = request.startAsync(); new Thread(() -> { try { Thread.sleep(SLEEPTIME_MS); response.getOutputStream().print(MSG); response.getOutputStream().flush(); asyncContext.complete(); } catch (InterruptedException | IOException e) { logger.error(e.getMessage(), e); } }).start(); baseRequest.setHandled(true); } }
private AsyncContext getAsyncContext() { final AsyncContext asyncContext; if (request.isAsyncStarted()) { asyncContext = request.getAsyncContext(); try { asyncContext.setTimeout(NEVER_TIMEOUT_VALUE); } catch (IllegalStateException ex) { // Let's hope the time out is set properly, otherwise JAX-RS AsyncResponse time-out support // may not work as expected... At least we can log this at fine level... LOGGER.log(Level.FINE, LocalizationMessages.SERVLET_ASYNC_CONTEXT_ALREADY_STARTED(), ex); } } else { asyncContext = request.startAsync(request, response); // Tell underlying asyncContext to never time out. asyncContext.setTimeout(NEVER_TIMEOUT_VALUE); } return asyncContext; }
@Override public void start(long timeout) { Assert.state(!isCompleted(), "Async processing has already completed"); if (isStarted()) { return; } HttpServletRequest servletRequest = this.request.getServletRequest(); HttpServletResponse servletResponse = this.response.getServletResponse(); this.asyncContext = servletRequest.startAsync(servletRequest, servletResponse); this.asyncContext.addListener(this); if (timeout != NO_TIMEOUT_VALUE) { this.asyncContext.setTimeout(timeout); } }
@Override public void startAsync() { Assert.state(getRequest().isAsyncSupported(), "Async support must be enabled on a servlet and for all filters involved " + "in async request processing. This is done in Java code using the Servlet API " + "or by adding \"<async-supported>true</async-supported>\" to servlet and " + "filter declarations in web.xml."); Assert.state(!isAsyncComplete(), "Async processing has already completed"); if (isAsyncStarted()) { return; } this.asyncContext = getRequest().startAsync(getRequest(), getResponse()); this.asyncContext.addListener(this); if (this.timeout != null) { this.asyncContext.setTimeout(this.timeout); } }
@Override public AsyncContext startAsync(ServletRequest request, ServletResponse response) { AsyncContext ac; if (AtmosphereResource.TRANSPORT.WEBSOCKET == resource().transport()) { noopsAsyncContextStarted = true; ac = new NoOpsAsyncContext(request, response); } else { ac = b.request.startAsync(request, response); } return isCompletionAware() ? new CompletionAwareAsyncContext(ac, (CompletionAware)resource().getResponse()) : ac; }
@Override public AsyncContext startAsync() { AsyncContext ac; if (AtmosphereResource.TRANSPORT.WEBSOCKET == resource().transport()) { noopsAsyncContextStarted = true; ac = new NoOpsAsyncContext(getRequest(), resource().getResponse().getResponse()); } else { ac = b.request.startAsync(); } return isCompletionAware() ? new CompletionAwareAsyncContext(ac, (CompletionAware)resource().getResponse()) : ac; }