Awesome, the asynchronous processing mechanism of Servlet3

Servlet3 has been released for several years. How many people know its new features? Here is a brief introduction.

The following features are mainly added:

1. Asynchronous processing support

2. Pluggability support

3. Annotation support, zero configuration, no need to configure web.xml

...

What the hell is asynchronous processing?

Operate the keyboard directly.

@WebServlet(name = "index", urlPatterns = { "/" }, asyncSupported = true)

public class IndexServlet extends HttpServlet {

    @Override

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

        resp.setContentType("text/html");

        try {

            PrintWriter out = resp.getWriter();

            out.println("servlet started.<br/>");

            out.flush();

            AsyncContext asyncContext = req.startAsync();

            asyncContext.addListener(getListener());

            asyncContext.start(new IndexThread(asyncContext));

            out.println("servlet end.<br/>");

            out.flush();

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

    /**

     * Asynchronous thread result listening

     * @author javastack

     * @return

     */

    private AsyncListener getListener() {

        return new AsyncListener() {

            public void onComplete(AsyncEvent asyncEvent) throws IOException {

                asyncEvent.getSuppliedResponse().getWriter().close();

                System.out.println("thread completed.");

            }

            public void onError(AsyncEvent asyncEvent) throws IOException {

                System.out.println("thread error.");

            }

            public void onStartAsync(AsyncEvent asyncEvent) throws IOException {

                System.out.println("thread started.");

            }

            public void onTimeout(AsyncEvent asyncEvent) throws IOException {

                System.out.println("thread timeout.");

            }

        };

    }

}

public class IndexThread implements Runnable {

    private AsyncContext asyncContext;

    public IndexThread(AsyncContext asyncContext) {

        this.asyncContext = asyncContext;

    }

    public void run() {

        try {

            Thread.sleep(5000);

            PrintWriter out = asyncContext.getResponse().getWriter();

            out.println("hello servlet3.<br/>");

            out.flush();

            asyncContext.complete();

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

}

Visit localhost:8080/test

Page output first

servlet started.
servlet end.

Output after 5 seconds

hello servlet3.

It can be seen that the servlet immediately returned, but did not close the response flow, just passed the response response to the thread, and then the thread continued to output. We can put the programs that cost more resources and time to do asynchronously, which saves servlet resources on a large program.

Spring mvc3.2 has also added the feature of servlet3 asynchronous processing, which can be studied by interested students.

It can also be seen from the servlet annotation above that servlet3 completely liberates the configuration of web.xml, which can completely replace the configuration of web.xml.

Recommend to my blog to read more:

1.Java JVM, collection, multithreading, new features series

2.Spring MVC, Spring Boot, Spring Cloud series tutorials

3.Maven, Git, Eclipse, Intellij IDEA series tools tutorial

4.Latest interview questions of Java, backend, architecture, Alibaba and other large factories

Feel good, don't forget to like + forward!

Keywords: Java Spring xml jvm

Added by JamieWAstin on Wed, 29 Apr 2020 19:20:32 +0300