Example usage for javax.servlet AsyncEvent getAsyncContext

List of usage examples for javax.servlet AsyncEvent getAsyncContext

Introduction

In this page you can find the example usage for javax.servlet AsyncEvent getAsyncContext.

Prototype

public AsyncContext getAsyncContext() 

Source Link

Document

Gets the AsyncContext from this AsyncEvent.

Usage

From source file:com.sishuok.chapter3.web.controller.StreamingController.java

@RequestMapping("/async3")
public void doGet(final HttpServletRequest req, final HttpServletResponse resp)
        throws ServletException, IOException {
    resp.setHeader("Connection", "Keep-Alive");
    resp.addHeader("Cache-Control", "private");
    resp.addHeader("Pragma", "no-cache");
    resp.setContentType("text/html;charset=utf-8");
    resp.flushBuffer();//ww  w.  ja  va 2  s . c  om

    //1??
    final AsyncContext asyncContext = req.startAsync();

    asyncContext.addListener(new AsyncListener() {
        @Override
        public void onComplete(final AsyncEvent event) throws IOException {
            queue.remove(event.getAsyncContext());
        }

        @Override
        public void onTimeout(final AsyncEvent event) throws IOException {
            event.getAsyncContext().complete();
            queue.remove(event.getAsyncContext());
        }

        @Override
        public void onError(final AsyncEvent event) throws IOException {
            queue.remove(event.getAsyncContext());
        }

        @Override
        public void onStartAsync(final AsyncEvent event) throws IOException {
        }
    });

    //????
    queue.add(asyncContext);

}

From source file:io.atrato.pubsubserver.PubsubRestProvider.java

@GET
@Path("topics/{topic}")
@Produces(MediaType.APPLICATION_JSON)/*  w  w  w .  j ava 2  s . co m*/
public PubsubBroker.TimedTopicData getTopicData(@PathParam("topic") String topic,
        @QueryParam("laterThan") Long timestamp, @QueryParam("poll") Boolean poll,
        @Context HttpServletRequest request) {
    if (timestamp == null) {
        timestamp = -1L;
    }
    //LOG.debug("topic: {} poll: {}", topic, poll);
    PubsubBroker.TimedTopicData data = PubsubServer.getBroker().getLatestTopicData(topic, timestamp);
    if (data == null) {
        if (BooleanUtils.isTrue(poll)) {
            AsyncContext asyncContext = request.startAsync();
            PubsubServer.getBroker().oneTimeSubscribe(asyncContext, topic);
            asyncContext.addListener(new AsyncListener() {
                @Override
                public void onComplete(AsyncEvent asyncEvent) throws IOException {
                    PubsubServer.getBroker().invalidateOneTimeSubscriber(asyncEvent.getAsyncContext());
                }

                @Override
                public void onTimeout(AsyncEvent asyncEvent) throws IOException {
                    PubsubServer.getBroker().invalidateOneTimeSubscriber(asyncEvent.getAsyncContext());
                }

                @Override
                public void onError(AsyncEvent asyncEvent) throws IOException {
                    PubsubServer.getBroker().invalidateOneTimeSubscriber(asyncEvent.getAsyncContext());
                }

                @Override
                public void onStartAsync(AsyncEvent asyncEvent) throws IOException {
                }
            });
            return null;
        } else {
            throw new NotFoundException();
        }
    }
    LOG.debug("topic: {} poll: {} data: {}", topic, poll, (data != null ? data.getData() : data));
    return data;
}

From source file:com.boylesoftware.web.AsynchronousExecutor.java

@Override
public void onTimeout(final AsyncEvent event) {

    final boolean debug = this.log.isDebugEnabled();
    if (debug)/*  w w  w. j a  v  a  2 s  .  c om*/
        this.log.debug("async event: timeout");

    this.timedOut = true;

    this.cleanup();

    if (debug)
        this.log.debug("recycling router request " + this.routerReq);
    RouterRequestLifecycle.recycle(this.routerReq);

    this.routerReq = null;
    this.asyncContext = null;
    this.webapp = null;

    if (this.executorThread != null)
        this.executorThread.interrupt();

    final AsyncContext asyncCtx = event.getAsyncContext();
    Router.setAsyncException(asyncCtx.getRequest(), new ServiceUnavailableException());
    if (debug)
        this.log.debug("dispatching service unavailable exception back to" + " the router");
    asyncCtx.dispatch();
}