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);
}
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);
}
}
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));
}
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"));
}
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);
}
Aggregations