Java Code Examples for javax.servlet.http.HttpServletRequest.isAsyncStarted()

The following are Jave code examples for showing how to use isAsyncStarted() of the javax.servlet.http.HttpServletRequest class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: JInsight   File: ContextMetricsHelper.java   View Source Code Vote up 6 votes
public void measure(HttpServletRequest request, HttpServletResponse response,
    MeasurableJob runnable)
    throws IOException, ServletException {

  activeRequestsCounter.inc();

  long startTime = System.nanoTime();
  runnable.run();
  if (request.isAsyncStarted()) {
    request.getAsyncContext()
        .addListener(new AsyncCompletionListener(startTime, request, response));
  } else {
    activeRequestsCounter.dec();
    updateStatusMetric(startTime, request.getMethod(), response.getStatus());
  }
}
 
Example 2
Project: apache-tomcat-7.0.73-with-comment   File: AsyncStockServlet.java   View Source Code Vote up 6 votes
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
    if (req.isAsyncStarted()) {
        req.getAsyncContext().complete();
    } else if (req.isAsyncSupported()) {
        AsyncContext actx = req.startAsync();
        actx.addListener(this);
        resp.setContentType("text/plain");
        clients.add(actx);
        if (clientcount.incrementAndGet()==1) {
            ticker.addTickListener(this);
        }
    } else {
        new Exception("Async Not Supported").printStackTrace();
        resp.sendError(400,"Async is not supported.");
    }
}
 
Example 3
Project: apache-tomcat-7.0.73-with-comment   File: AsyncStockServlet.java   View Source Code Vote up 6 votes
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
    if (req.isAsyncStarted()) {
        req.getAsyncContext().complete();
    } else if (req.isAsyncSupported()) {
        AsyncContext actx = req.startAsync();
        actx.addListener(this);
        resp.setContentType("text/plain");
        clients.add(actx);
        if (clientcount.incrementAndGet()==1) {
            ticker.addTickListener(this);
        }
    } else {
        new Exception("Async Not Supported").printStackTrace();
        resp.sendError(400,"Async is not supported.");
    }
}
 
Example 4
Project: wildfly_exporter   File: ServletMetricsFilter.java   View Source Code Vote up 5 votes
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
    if (!(servletRequest instanceof HttpServletRequest)) {
        filterChain.doFilter(servletRequest, servletResponse);
        return;
    }

    HttpServletRequest request = (HttpServletRequest) servletRequest;

    if (!request.isAsyncStarted()) {
        String context = getContext(request);

        servletConcurrentRequest.labels(context).inc();

        Histogram.Timer timer = servletLatency
                .labels(context, request.getMethod())
                .startTimer();

        try {
            filterChain.doFilter(servletRequest, servletResponse);
        } finally {
            timer.observeDuration();
            servletConcurrentRequest.labels(context).dec();
            servletStatusCodes.labels(context, Integer.toString(getStatus((HttpServletResponse) servletResponse))).inc();
        }
    } else {
        filterChain.doFilter(servletRequest, servletResponse);
    }
}
 
Example 5
Project: tomcat7   File: TestAsyncContextImpl.java   View Source Code Vote up 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 6
Project: apache-tomcat-7.0.73-with-comment   File: TestAsyncContextImpl.java   View Source Code Vote up 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 7
Project: tomcat_exporter   File: TomcatServletMetricsFilter.java   View Source Code Vote up 5 votes
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
    if (!(servletRequest instanceof HttpServletRequest)) {
        filterChain.doFilter(servletRequest, servletResponse);
        return;
    }

    HttpServletRequest request = (HttpServletRequest) servletRequest;

    if (!request.isAsyncStarted()) {
        String context = getContext(request);

        servletConcurrentRequest.labels(context).inc();

        Histogram.Timer timer = servletLatency
                .labels(context, request.getMethod())
                .startTimer();

        try {
            filterChain.doFilter(servletRequest, servletResponse);
        } finally {
            timer.observeDuration();
            servletConcurrentRequest.labels(context).dec();
            servletStatusCodes.labels(context, Integer.toString(getStatus((HttpServletResponse) servletResponse))).inc();
        }
    } else {
        filterChain.doFilter(servletRequest, servletResponse);
    }
}