Java Code Examples for javax.servlet.AsyncContext#getResponse()

The following examples show how to use javax.servlet.AsyncContext#getResponse() . 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: QueryMessageServlet.java    From qmq with Apache License 2.0 6 votes vote down vote up
@Override
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) {
    resp.setStatus(HttpServletResponse.SC_OK);
    final String queryJson = req.getParameter("backupQuery");
    if (Strings.isNullOrEmpty(queryJson)) return;
    final AsyncContext context = req.startAsync();
    RemoteMessageQuery query = deserialize(queryJson);
    if (query == null) {
        context.complete();
        return;
    }

    final ServletResponse response = context.getResponse();
    final CompletableFuture<Boolean> future = query(query, response);
    future.exceptionally(throwable -> {
        LOG.error("Failed to query messages. {}", query, throwable);
        return true;
    }).thenAccept(aBoolean -> context.complete());
}
 
Example 2
Source File: AsyncStockServlet.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
public void writeStock(AsyncContext actx, Stock stock) {
    HttpServletResponse response = (HttpServletResponse)actx.getResponse();
    try {
        PrintWriter writer = response.getWriter();
        writer.write("STOCK#");//make client parsing easier
        writer.write(stock.getSymbol());
        writer.write("#");
        writer.write(stock.getValueAsString());
        writer.write("#");
        writer.write(stock.getLastChangeAsString());
        writer.write("#");
        writer.write(String.valueOf(stock.getCnt()));
        writer.write("\n");
        writer.flush();
        response.flushBuffer();
    }catch (IOException x) {
        try {actx.complete();}catch (Exception ignore){/* Ignore */}
    }
}
 
Example 3
Source File: AsyncStockServlet.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
public void writeStock(AsyncContext actx, Stock stock) {
    HttpServletResponse response = (HttpServletResponse)actx.getResponse();
    try {
        PrintWriter writer = response.getWriter();
        writer.write("STOCK#");//make client parsing easier
        writer.write(stock.getSymbol());
        writer.write("#");
        writer.write(stock.getValueAsString());
        writer.write("#");
        writer.write(stock.getLastChangeAsString());
        writer.write("#");
        writer.write(String.valueOf(stock.getCnt()));
        writer.write("\n");
        writer.flush();
        response.flushBuffer();
    }catch (IOException x) {
        try {actx.complete();}catch (Exception ignore){/* Ignore */}
    }
}
 
Example 4
Source File: AsyncStockServlet.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
public void writeStock(AsyncContext actx, Stock stock) {
    HttpServletResponse response = (HttpServletResponse)actx.getResponse();
    try {
        PrintWriter writer = response.getWriter();
        writer.write("STOCK#");//make client parsing easier
        writer.write(stock.getSymbol());
        writer.write("#");
        writer.write(stock.getValueAsString());
        writer.write("#");
        writer.write(stock.getLastChangeAsString());
        writer.write("#");
        writer.write(String.valueOf(stock.getCnt()));
        writer.write("\n");
        writer.flush();
        response.flushBuffer();
    }catch (IOException x) {
        try {actx.complete();}catch (Exception ignore){/* Ignore */}
    }
}
 
Example 5
Source File: AsyncStockServlet.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
public void writeStock(AsyncContext actx, Stock stock) throws IOException {
    HttpServletResponse response = (HttpServletResponse)actx.getResponse();
    PrintWriter writer = response.getWriter();
    writer.write("STOCK#");//make client parsing easier
    writer.write(stock.getSymbol());
    writer.write("#");
    writer.write(stock.getValueAsString());
    writer.write("#");
    writer.write(stock.getLastChangeAsString());
    writer.write("#");
    writer.write(String.valueOf(stock.getCnt()));
    writer.write("\n");
    writer.flush();
    response.flushBuffer();
}
 
Example 6
Source File: AsyncStockServlet.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
public void writeStock(AsyncContext actx, Stock stock) throws IOException {
    HttpServletResponse response = (HttpServletResponse)actx.getResponse();
    PrintWriter writer = response.getWriter();
    writer.write("STOCK#");//make client parsing easier
    writer.write(stock.getSymbol());
    writer.write("#");
    writer.write(stock.getValueAsString());
    writer.write("#");
    writer.write(stock.getLastChangeAsString());
    writer.write("#");
    writer.write(String.valueOf(stock.getCnt()));
    writer.write("\n");
    writer.flush();
    response.flushBuffer();
}
 
Example 7
Source File: ChangeReturnAction.java    From qconfig with MIT License 5 votes vote down vote up
@Override
public void act(AsyncContext context) throws Exception {
    try {
        HttpServletResponse response = (HttpServletResponse) context.getResponse();
        response.setHeader(Constants.UPDATE_TYPE, type);
        response.getWriter().write(change);
    } catch (Exception e) {
        Monitor.returnChangeFailCounter.inc();
        throw e;
    }
}
 
Example 8
Source File: Web.java    From flower with Apache License 2.0 5 votes vote down vote up
public Web(AsyncContext context) {
  this.asyncContext = context;
  this.servletRequest = context.getRequest();
  this.servletResponse = context.getResponse();
  servletResponse.setCharacterEncoding(Constant.ENCODING_UTF_8);
  try {
    this.writer = servletResponse.getWriter();
  } catch (IOException e) {
    logger.error("", e);
  }
}
 
Example 9
Source File: SendServlet.java    From qmq with Apache License 2.0 5 votes vote down vote up
private void asyncSuccess(AsyncContext asyncContext, Message message) {
    try {
        Map<String, Object> result = new HashMap<>();
        result.put("status", 0);
        result.put("message", message.getMessageId());
        ServletResponse response = asyncContext.getResponse();
        response.setContentType("application/json");
        MAPPER.writeValue(response.getWriter(), result);
    } catch (Exception e) {
        logger.error("return message error {} - {}", message.getSubject(), message.getMessageId());
    } finally {
        asyncContext.complete();
    }
}
 
Example 10
Source File: SendServlet.java    From qmq with Apache License 2.0 5 votes vote down vote up
private void asyncError(AsyncContext asyncContext, Message message) {
    try {
        Map<String, Object> result = new HashMap<>();
        result.put("status", -1);
        result.put("error", message.getMessageId());
        ServletResponse response = asyncContext.getResponse();
        response.setContentType("application/json");
        MAPPER.writeValue(response.getWriter(), result);
    } catch (Exception e) {
        logger.error("return message error {} - {}", message.getSubject(), message.getMessageId());
    } finally {
        asyncContext.complete();
    }
}