Package javax.servlet

Examples of javax.servlet.AsyncContext


    @Override
    protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {

        System.out.println("====filter servlet 1  before");

        final AsyncContext asyncContext = req.startAsync();
        asyncContext.setTimeout(10L * 1000);
        asyncContext.addListener(new AsyncListener() {
            @Override
            public void onComplete(final AsyncEvent event) throws IOException {
                System.out.println("=====async complete");
            }

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

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

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

        System.out.println("====filter servlet 1  before dispatch");
        //异步下分派过去的请求,异步filter也可以拦截
        //如果filter不是DispatcherType.ASYNC 类型,那么分派到/filter2时,拦截器不会调用
        asyncContext.dispatch("/filter2");
        System.out.println("====filter servlet 1  after dispatch");

        System.out.println("====filter servlet 1  after");


View Full Code Here


    @Override
    protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {

        System.out.println("====filter servlet 2  before");

        final AsyncContext asyncContext = req.startAsync();
        asyncContext.setTimeout(10L * 1000);
        asyncContext.addListener(new AsyncListener() {
            @Override
            public void onComplete(final AsyncEvent event) throws IOException {
                System.out.println("=====async complete");
            }

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

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

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

        System.out.println("====filter servlet 2  before dispatch");
        req.setAttribute("msg", "success");
        asyncContext.dispatch("/WEB-INF/jsp/dispatch.jsp");
        System.out.println("====filter servlet 2  after dispatch");

        System.out.println("====filter servlet 2  after");


View Full Code Here

public class ListenerServlet2 extends HttpServlet {


    @Override
    protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
        final AsyncContext asyncContext = req.startAsync();
        asyncContext.setTimeout(10 * 1000);
        asyncContext.addListener(new AsyncListener() {
            @Override
            public void onComplete(final AsyncEvent event) throws IOException {
                System.out.println("=====async complete");
            }

            @Override
            public void onTimeout(final AsyncEvent event) throws IOException {
                System.out.println("=====async timeout");
                asyncContext.complete(); //需要调用下complete 否则按超时时间(比如当前设置的是10秒)重新调度一次当前方法
            }

            @Override
            public void onError(final AsyncEvent event) throws IOException {
                System.out.println("=====async error");
            }

            @Override
            public void onStartAsync(final AsyncEvent event) throws IOException {
                System.out.println("=====start new async");
            }
        });
        asyncContext.dispatch("/error"); //分派到一个不存在的地址 会报404,但是最终服务器会调用onComplete来完成异步
    }
View Full Code Here

    }


    public void startAsync(HttpServletRequest req, final String username) {
        //1、开启异步
        final AsyncContext asyncContext = req.startAsync();
        asyncContext.setTimeout(30L * 1000);

        //将异步上下文加入到队列中,这样在未来可以推送消息
        Queue<AsyncContext> queue = usernameToAsyncContextMap.get(username);
        if(queue == null) {
            queue = new ConcurrentLinkedDeque();
            usernameToAsyncContextMap.put(username, queue);
        }

        queue.add(asyncContext);

        asyncContext.addListener(new AsyncListener() {
            @Override
            public void onComplete(final AsyncEvent event) throws IOException {
                Queue<AsyncContext> queue = usernameToAsyncContextMap.get(username);
                if(queue != null) {
                    queue.remove(event.getAsyncContext());
View Full Code Here

                }
                Queue<AsyncContext> queue = usernameToAsyncContextMap.get(loginUsername);
                if(queue != null) {
                    Iterator<AsyncContext> iter = queue.iterator();
                    while(iter.hasNext()) {
                        AsyncContext asyncContext = iter.next();
                        try {
                            ServletResponse response = asyncContext.getResponse();
                            PrintWriter out = response.getWriter();
                            out.write(data);
                            out.flush();
                            asyncContext.complete();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        } else { //私人消息
            Queue<AsyncContext> queue = usernameToAsyncContextMap.get(receiver);
            if(queue != null) {
                Iterator<AsyncContext> iter = queue.iterator();
                while(iter.hasNext()) {
                    AsyncContext asyncContext = iter.next();
                    try {
                        ServletResponse response = asyncContext.getResponse();
                        PrintWriter out = response.getWriter();
                        out.write(data);
                        out.flush();
                        asyncContext.complete();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
View Full Code Here

public class ListenerServlet1 extends HttpServlet {


    @Override
    protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
        final AsyncContext asyncContext = req.startAsync();
        asyncContext.setTimeout(2 * 1000);
        asyncContext.addListener(new AsyncListener() {
            @Override
            public void onComplete(final AsyncEvent event) throws IOException {
                System.out.println("=====async complete");
            }

            @Override
            public void onTimeout(final AsyncEvent event) throws IOException {
                System.out.println("=====async timeout");
                asyncContext.complete(); //需要调用下complete 否则如jetty默认每2秒重新调度一次当前方法
            }

            @Override
            public void onError(final AsyncEvent event) throws IOException {
                System.out.println("=====async error");
View Full Code Here

public class ListenerServlet3 extends HttpServlet {


    @Override
    protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
        final AsyncContext asyncContext = req.startAsync();
        asyncContext.setTimeout(10 * 1000);

        asyncContext.addListener(new AsyncListener() {
            @Override
            public void onComplete(final AsyncEvent event) throws IOException {
                System.out.println("=====async complete");
            }

            @Override
            public void onTimeout(final AsyncEvent event) throws IOException {
                System.out.println("=====async timeout");
                asyncContext.complete(); //需要调用下complete 否则如jetty默认每2秒重新调度一次当前方法
            }

            @Override
            public void onError(final AsyncEvent event) throws IOException {
                System.out.println("=====async error");
View Full Code Here

    @Override
    protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {

        System.out.println("===before start async:" + req.isAsyncStarted());
        final AsyncContext asyncContext = req.startAsync();
        System.out.println("===after start async:" + req.isAsyncStarted());

        asyncContext.setTimeout(10L * 1000);
        asyncContext.addListener(new AsyncListener() {
            @Override
            public void onComplete(final AsyncEvent event) throws IOException {
                System.out.println("=====async complete");
            }

            @Override
            public void onTimeout(final AsyncEvent event) throws IOException {
                System.out.println("=====async timeout");
            }

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

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

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(3L * 1000);
                } catch (InterruptedException e) {
                }
                req.setAttribute("ok", "true");
                req.setAttribute("msg", "success");
                asyncContext.dispatch("/WEB-INF/jsp/dispatch.jsp");
            }
        }).start();

    }
View Full Code Here

    @Override
    protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {

        if(req.getAttribute("ok") == null) {
            System.out.println("===before start async:" + req.isAsyncStarted());
            final AsyncContext asyncContext = req.startAsync();
            System.out.println("===after start async:" + req.isAsyncStarted());

            asyncContext.setTimeout(10L * 1000);
            asyncContext.addListener(new AsyncListener() {
                @Override
                public void onComplete(final AsyncEvent event) throws IOException {
                    System.out.println("=====async complete");
                }

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

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

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

            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(3L * 1000);
                    } catch (InterruptedException e) {
                    }
                    req.setAttribute("ok", "true");
                    req.setAttribute("msg", "success");
                    asyncContext.dispatch();
                    System.out.println("===after dispatch before handle:" + req.isAsyncStarted());
                }
            }).start();

            return;
View Full Code Here

        System.out.println(req.getAttribute(AsyncContext.ASYNC_QUERY_STRING));
        System.out.println(req.getAttribute(AsyncContext.ASYNC_SERVLET_PATH));

        if(req.getAttribute("ok1") == null) {
            System.out.println("===before start async:" + req.isAsyncStarted());
            final AsyncContext asyncContext = req.startAsync();
            System.out.println("===after start async:" + req.isAsyncStarted());

            asyncContext.setTimeout(10L * 1000);
            asyncContext.addListener(new AsyncListener() {
                @Override
                public void onComplete(final AsyncEvent event) throws IOException {
                    System.out.println("=====async complete");
                }

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

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

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

            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(3L * 1000);
                    } catch (InterruptedException e) {
                    }
                    req.setAttribute("ok1", "true");
                    req.setAttribute("msg", "success");
                    asyncContext.dispatch();
                    System.out.println("===after dispatch before handle:" + req.isAsyncStarted());
                }
            }).start();

            return;
View Full Code Here

TOP

Related Classes of javax.servlet.AsyncContext

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.