Java Code Examples for javax.servlet.AsyncContext#start()
The following examples show how to use
javax.servlet.AsyncContext#start() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestCoyoteOutputStream.java From Tomcat8-Source-Read with MIT License | 6 votes |
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/plain"); resp.setCharacterEncoding("UTF-8"); ServletOutputStream sos = resp.getOutputStream(); AsyncContext asyncCtxt = req.startAsync(); asyncCtxt.setTimeout(5); Runnable task = new AsyncTask(asyncCtxt, sos); if (useContainerThreadToSetListener) { asyncCtxt.start(task); } else { Thread t = new Thread(task); t.start(); } }
Example 2
Source File: AsyncServletIT.java From glowroot with Apache License 2.0 | 6 votes |
@Override protected void doGet(HttpServletRequest request, HttpServletResponse response) { request.getSession().setAttribute("sync", "a"); new CreateTraceEntry().traceEntryMarker(); final AsyncContext asyncContext = request.startAsync(request, response); asyncContext.start(new Runnable() { @Override public void run() { try { MILLISECONDS.sleep(200); ((HttpServletRequest) asyncContext.getRequest()).getSession() .setAttribute("async", "b"); new CreateTraceEntry().traceEntryMarker(); asyncContext.getResponse().getWriter().println("async response"); asyncContext.complete(); } catch (Exception e) { throw new RuntimeException(e); } } }); }
Example 3
Source File: TestAbstractHttp11Processor.java From tomcatsrc with Apache License 2.0 | 6 votes |
@Override protected void doPut(HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException { final AsyncContext ac = req.startAsync(); ac.start(new Runnable() { @Override public void run() { resp.setContentType("text/plain"); resp.setCharacterEncoding("UTF-8"); try { resp.getWriter().print("OK"); } catch (IOException e) { // Should never happen. Test will fail if it does. } ac.complete(); } }); }
Example 4
Source File: TestAbstractHttp11Processor.java From Tomcat7.0.67 with Apache License 2.0 | 6 votes |
@Override protected void doPut(HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException { final AsyncContext ac = req.startAsync(); ac.start(new Runnable() { @Override public void run() { resp.setContentType("text/plain"); resp.setCharacterEncoding("UTF-8"); try { resp.getWriter().print("OK"); } catch (IOException e) { // Should never happen. Test will fail if it does. } ac.complete(); } }); }
Example 5
Source File: TestAsyncContextImpl.java From tomcatsrc with Apache License 2.0 | 6 votes |
@Override protected void doGet(HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException { final AsyncContext ctx = req.startAsync(); ctx.start(new Runnable() { @Override public void run() { try { Thread.sleep(THREAD_SLEEP_TIME); resp.setHeader("A", "xyz"); resp.setContentType("text/plain"); resp.setContentLength("OK".getBytes().length); resp.getWriter().print("OK"); ctx.complete(); } catch (Exception e) { e.printStackTrace(); } } }); }
Example 6
Source File: TestAsyncContextImpl.java From tomcatsrc with Apache License 2.0 | 6 votes |
@Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { final AsyncContext asyncContext = request.startAsync(request, response); asyncContext.addListener(new TrackingListener(false, false, null)); asyncContext.start(new Runnable() { @Override public void run() { try { Thread.sleep(THREAD_SLEEP_TIME); TestAsyncContextImpl.track("Runnable-"); asyncContext.complete(); } catch (Exception e) { e.printStackTrace(); } } }); }
Example 7
Source File: TestAsyncContextImpl.java From tomcatsrc with Apache License 2.0 | 6 votes |
@Override protected void doGet(HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException { final AsyncContext actxt = req.startAsync(); actxt.setTimeout(TIMEOUT); if (threaded) { actxt.start(new Runnable() { @Override public void run() { try { resp.sendError(status, ERROR_MESSAGE); actxt.complete(); } catch (IOException e) { // Ignore } } }); } else { resp.sendError(status, ERROR_MESSAGE); actxt.complete(); } }
Example 8
Source File: AsyncServlet.java From servlet-core-learning with Apache License 2.0 | 6 votes |
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException,IOException { resp.setContentType("text/html;charset=UTF-8"); Date startDate = new Date(); System.out.println("Servlet当前时间 --- " + startDate ); // 从请求中获取AsyncContext AsyncContext asyncContext = req.startAsync(); // 设置异步调用的超时时长 asyncContext.setTimeout(30*1000);// 30秒 // 开始异步调用 asyncContext.start(new AsyncThread(asyncContext)); Date endDate = new Date(); System.out.println("Servlet结束时间 --- " + endDate); // startDate和endDate时间差不多相等。 // 说明Servlet重新发起了一条线程去掉用耗时业务方法,避免了阻塞式调用 }
Example 9
Source File: AbstractJettyTest.java From java-web-servlet-filter with Apache License 2.0 | 6 votes |
@Override public void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException { // avoid retries on timeout if (request.getAttribute("timedOut") != null) { return; } request.setAttribute("timedOut", true); final AsyncContext asyncContext = request.startAsync(request, response); asyncContext.setTimeout(10); asyncContext.start(new Runnable() { @Override public void run() { try { TimeUnit.MILLISECONDS.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } finally { asyncContext.complete(); } } }); }
Example 10
Source File: TestAsyncContextImpl.java From tomcatsrc with Apache License 2.0 | 5 votes |
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // Should not be async at this point isAsyncWhenExpected = isAsyncWhenExpected && !req.isAsyncStarted(); final AsyncContext async = req.startAsync(); // Should be async at this point isAsyncWhenExpected = isAsyncWhenExpected && req.isAsyncStarted(); async.start(new Runnable() { @Override public void run() { // This should be delayed until the original container // thread exists async.dispatch("/ServletB"); } }); try { Thread.sleep(3000); } catch (InterruptedException e) { throw new ServletException(e); } // Should be async at this point isAsyncWhenExpected = isAsyncWhenExpected && req.isAsyncStarted(); }
Example 11
Source File: ITServlet3Container.java From brave with Apache License 2.0 | 5 votes |
@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); }
Example 12
Source File: TestAsyncContextImpl.java From Tomcat7.0.67 with Apache License 2.0 | 5 votes |
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // Should not be async at this point isAsyncWhenExpected = isAsyncWhenExpected && !req.isAsyncStarted(); final AsyncContext async = req.startAsync(); // Should be async at this point isAsyncWhenExpected = isAsyncWhenExpected && req.isAsyncStarted(); async.start(new Runnable() { @Override public void run() { // This should be delayed until the original container // thread exists async.dispatch("/ServletB"); } }); try { Thread.sleep(3000); } catch (InterruptedException e) { throw new ServletException(e); } // Should be async at this point isAsyncWhenExpected = isAsyncWhenExpected && req.isAsyncStarted(); }
Example 13
Source File: UserServlet.java From javaee8-cookbook with Apache License 2.0 | 5 votes |
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { AsyncContext ctx = req.startAsync(); ctx.start(() -> { try (PrintWriter writer = ctx.getResponse().getWriter()) { writer.write(jsonb.toJson(userBean.getUser())); } catch (IOException ex) { System.err.println(ex.getMessage()); } ctx.complete(); }); }
Example 14
Source File: ClientJettyStreamAsyncITest.java From hawkular-apm with Apache License 2.0 | 5 votes |
@Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { final AsyncContext ctxt = request.startAsync(); ctxt.start(new Runnable() { @Override public void run() { try { InputStream is = request.getInputStream(); byte[] b = new byte[is.available()]; is.read(b); is.close(); response.setContentType("text/html; charset=utf-8"); response.setStatus(HttpServletResponse.SC_OK); if (request.getHeader("test-no-data") == null) { OutputStream os = response.getOutputStream(); byte[] resp = HELLO_WORLD_RESPONSE.getBytes(); os.write(resp, 0, resp.length); os.flush(); os.close(); } } catch (Exception e) { fail("Failed: " + e); } ctxt.complete(); } }); }
Example 15
Source File: TestHttpServletResponseSendError.java From Tomcat8-Source-Read with MIT License | 5 votes |
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { if (errorPoint == AsyncErrorPoint.THREAD_A_BEFORE_START_ASYNC) { doError(resp); } AsyncContext ac = req.startAsync(); ac.setTimeout(2000); if (errorPoint == AsyncErrorPoint.THREAD_A_AFTER_START_ASYNC) { doError(resp); } AsyncRunnable r = new AsyncRunnable(ac, throwException, useDispatch, errorPoint); if (useStart) { ac.start(r); } else { Thread t = new Thread(r); t.start(); } if (errorPoint == AsyncErrorPoint.THREAD_A_AFTER_START_RUNNABLE) { doError(resp); } }
Example 16
Source File: ClientJettyReaderWriterAsyncITest.java From hawkular-apm with Apache License 2.0 | 5 votes |
@Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { final AsyncContext ctxt = request.startAsync(); ctxt.start(new Runnable() { @Override public void run() { try { BufferedReader reader = request.getReader(); reader.readLine(); response.setContentType("text/html; charset=utf-8"); response.setStatus(HttpServletResponse.SC_OK); if (request.getHeader("test-no-data") == null) { PrintWriter out = response.getWriter(); out.println(HELLO_WORLD_RESPONSE); } } catch (Exception e) { fail("Failed: " + e); } ctxt.complete(); } }); }
Example 17
Source File: TestStreamProcessor.java From Tomcat8-Source-Read with MIT License | 5 votes |
@Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/plain"); response.setCharacterEncoding("UTF-8"); PrintWriter pw = response.getWriter(); pw.print("Enter-"); final AsyncContext asyncContext = request.startAsync(request, response); pw.print("StartAsync-"); pw.flush(); asyncContext.start(new Runnable() { @Override public void run() { try { asyncContext.getResponse().getWriter().print("Complete"); asyncContext.complete(); } catch (Exception e) { e.printStackTrace(); } } }); }
Example 18
Source File: TestAsyncServlet.java From logging-log4j2 with Apache License 2.0 | 5 votes |
@Override protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException { final AsyncContext asyncContext = req.startAsync(); asyncContext.start(WebLoggerContextUtils.wrapExecutionContext(this.getServletContext(), new Runnable() { @Override public void run() { final Logger logger = LogManager.getLogger(TestAsyncServlet.class); logger.info("Hello, servlet!"); } })); }
Example 19
Source File: AsyncServletTest.java From apm-agent-java with Apache License 2.0 | 4 votes |
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setAttribute(ACTIVE_TRANSACTION_ATTRIBUTE, tracer.currentTransaction()); final AsyncContext ctx = req.startAsync(); ctx.start(() -> ctx.dispatch("/plain")); }
Example 20
Source File: JavaxServletAsyncServerITest.java From hawkular-apm with Apache License 2.0 | 4 votes |
@Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { final AsyncContext ctxt = request.startAsync(); ctxt.start(new Runnable() { @Override public void run() { try { InputStream is = request.getInputStream(); byte[] b = new byte[is.available()]; is.read(b); is.close(); System.out.println("REQUEST(ASYNC INPUTSTREAM) RECEIVED: " + new String(b)); response.setContentType("text/html; charset=utf-8"); if (request.getHeader("test-fault") != null) { response.sendError(401, "Unauthorized"); } else { response.setStatus(HttpServletResponse.SC_OK); if (request.getHeader("test-no-data") == null) { OutputStream os = response.getOutputStream(); byte[] resp = HELLO_WORLD_RESPONSE.getBytes(); os.write(resp, 0, resp.length); os.flush(); os.close(); } } } catch (Exception e) { System.err.println("Failed to handle servlet request"); e.printStackTrace(); } ctxt.complete(); } }); }