Search in sources :

Example 41 with AsyncContext

use of javax.servlet.AsyncContext in project spring-framework by spring-projects.

the class ServerHttpRequestTests method createHttpRequest.

private ServerHttpRequest createHttpRequest(String path) throws Exception {
    HttpServletRequest request = new MockHttpServletRequest("GET", path) {

        @Override
        public ServletInputStream getInputStream() {
            return new TestServletInputStream();
        }
    };
    AsyncContext asyncContext = new MockAsyncContext(request, new MockHttpServletResponse());
    return new ServletServerHttpRequest(request, asyncContext, new DefaultDataBufferFactory(), 1024);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) MockAsyncContext(org.springframework.mock.web.test.MockAsyncContext) MockAsyncContext(org.springframework.mock.web.test.MockAsyncContext) AsyncContext(javax.servlet.AsyncContext) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse)

Example 42 with AsyncContext

use of javax.servlet.AsyncContext in project camel by apache.

the class CamelServlet method service.

@Override
protected final 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);
    }
}
Also used : AsyncContext(javax.servlet.AsyncContext)

Example 43 with AsyncContext

use of javax.servlet.AsyncContext in project BRFS by zhangnianli.

the class DiskJettyHttpRequestHandler method handle.

@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    LOG.debug("handle request[{}:{}]", request.getMethod(), target);
    MessageHandler handler = methodToOps.get(request.getMethod());
    if (handler == null) {
        baseRequest.setHandled(true);
        responseError(response, HttpStatus.Code.METHOD_NOT_ALLOWED);
        return;
    }
    DiskMessage message = new DiskMessage();
    message.setFilePath(target);
    int contentLength = request.getContentLength();
    System.out.println("content length############" + contentLength);
    byte[] data = new byte[Math.max(contentLength, 0)];
    if (request.getContentLength() > 0) {
        InputUtils.readBytes(request.getInputStream(), data, 0, data.length);
        System.out.println(new String(data));
    }
    message.setData(data);
    Map<String, String> params = new HashMap<String, String>();
    for (String paramName : request.getParameterMap().keySet()) {
        params.put(paramName, request.getParameter(paramName));
        System.out.println(paramName + "---" + request.getParameter(paramName));
    }
    message.setParams(params);
    baseRequest.setHandled(true);
    // 采用异步方式处理Http响应
    AsyncContext context = request.startAsync();
    System.out.println("########################START HANDLING##########################");
    handler.handle(message, new DefaultHandleResultCallback(context));
}
Also used : MessageHandler(com.bonree.brfs.disknode.server.netty.MessageHandler) DiskMessage(com.bonree.brfs.disknode.server.handler.DiskMessage) HashMap(java.util.HashMap) AsyncContext(javax.servlet.AsyncContext)

Example 44 with AsyncContext

use of javax.servlet.AsyncContext in project felix by apache.

the class AsyncTest method testAsyncServletWithDispatchOk.

/**
 * Tests that we can use an asynchronous servlet (introduced in Servlet 3.0 spec) using the dispatching functionality.
 */
@Test
public void testAsyncServletWithDispatchOk() throws Exception {
    CountDownLatch initLatch = new CountDownLatch(1);
    CountDownLatch destroyLatch = new CountDownLatch(1);
    TestServlet servlet = new TestServlet(initLatch, destroyLatch) {

        private static final long serialVersionUID = 1L;

        @Override
        protected void doGet(final HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            DispatcherType dispatcherType = req.getDispatcherType();
            if (DispatcherType.REQUEST == dispatcherType) {
                final AsyncContext asyncContext = req.startAsync(req, resp);
                asyncContext.start(new Runnable() {

                    @Override
                    public void run() {
                        try {
                            // Simulate a long running process...
                            Thread.sleep(1000);
                            asyncContext.getRequest().setAttribute("msg", "Hello Async world!");
                            asyncContext.dispatch();
                        } catch (InterruptedException e) {
                            Thread.currentThread().interrupt();
                        }
                    }
                });
            } else if (DispatcherType.ASYNC == dispatcherType) {
                String response = (String) req.getAttribute("msg");
                resp.setStatus(SC_OK);
                resp.getWriter().printf(response);
                resp.flushBuffer();
            }
        }
    };
    register("/test", servlet);
    assertTrue(initLatch.await(5, TimeUnit.SECONDS));
    assertContent("Hello Async world!", createURL("/test"));
    unregister("/test");
    assertTrue(destroyLatch.await(5, TimeUnit.SECONDS));
    assertResponseCode(SC_NOT_FOUND, createURL("/test"));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) AsyncContext(javax.servlet.AsyncContext) CountDownLatch(java.util.concurrent.CountDownLatch) DispatcherType(javax.servlet.DispatcherType) Test(org.junit.Test)

Example 45 with AsyncContext

use of javax.servlet.AsyncContext in project incubator-servicecomb-java-chassis by apache.

the class ServletRestDispatcher method service.

public void service(HttpServletRequest request, HttpServletResponse response) {
    if (transport == null) {
        transport = CseContext.getInstance().getTransportManager().findTransport(Const.RESTFUL);
    }
    // 异步场景
    final AsyncContext asyncCtx = request.startAsync();
    asyncCtx.addListener(restAsyncListener);
    asyncCtx.setTimeout(ServletConfig.getServerTimeout());
    HttpServletRequestEx requestEx = new StandardHttpServletRequestEx(request);
    HttpServletResponseEx responseEx = new StandardHttpServletResponseEx(response);
    RestServletProducerInvocation restProducerInvocation = new RestServletProducerInvocation();
    restProducerInvocation.invoke(transport, requestEx, responseEx, httpServerFilters);
}
Also used : HttpServletResponseEx(org.apache.servicecomb.foundation.vertx.http.HttpServletResponseEx) StandardHttpServletResponseEx(org.apache.servicecomb.foundation.vertx.http.StandardHttpServletResponseEx) StandardHttpServletResponseEx(org.apache.servicecomb.foundation.vertx.http.StandardHttpServletResponseEx) AsyncContext(javax.servlet.AsyncContext) StandardHttpServletRequestEx(org.apache.servicecomb.foundation.vertx.http.StandardHttpServletRequestEx) HttpServletRequestEx(org.apache.servicecomb.foundation.vertx.http.HttpServletRequestEx) StandardHttpServletRequestEx(org.apache.servicecomb.foundation.vertx.http.StandardHttpServletRequestEx)

Aggregations

AsyncContext (javax.servlet.AsyncContext)194 IOException (java.io.IOException)90 HttpServletResponse (javax.servlet.http.HttpServletResponse)78 HttpServletRequest (javax.servlet.http.HttpServletRequest)76 ServletException (javax.servlet.ServletException)61 Test (org.junit.Test)57 CountDownLatch (java.util.concurrent.CountDownLatch)38 AsyncEvent (javax.servlet.AsyncEvent)35 AsyncListener (javax.servlet.AsyncListener)35 HttpServlet (javax.servlet.http.HttpServlet)34 ServletOutputStream (javax.servlet.ServletOutputStream)27 InterruptedIOException (java.io.InterruptedIOException)24 ReadListener (javax.servlet.ReadListener)20 ServletInputStream (javax.servlet.ServletInputStream)20 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)18 Request (org.eclipse.jetty.server.Request)16 WriteListener (javax.servlet.WriteListener)15 PrintWriter (java.io.PrintWriter)14 UncheckedIOException (java.io.UncheckedIOException)14 ByteBuffer (java.nio.ByteBuffer)13