Using an asynchronous servlet to extend an AJAX application

Source: Internet
Author: User
Tags empty execution extend final http request interface socket thread
Ajax|servlet| Program | Asynchronous <br/> The advent of Ajax as a Web application model has dramatically changed the face of the server side. <table border= "0" cellspacing= "0" cellpadding= "0" align= "left" style= "MARGIN-TOP:10PX;MARGIN-RIGHT:7PX; margin-bottom:3px;margin-left:0px "><tr><td><span id=" Ad_pcdog_big "></span></td ></tr></table> users fill out a form on a Web page and click the Submit button to go to the next link the typical Web usage patterns are now being transformed into more advanced Client JavaScript and a richer user interface, The user interface continues to interact with the server as long as the form is manipulated, such as clicking a check box, pressing a key, or moving the mouse over a tab. <br/><br/> Consider how much data is transferred from the client to the server. From a usability standpoint, users get a rich user interface on a thin client browser without having to install anything. However, when extending these applications on the server side, there is a price to pay. Typical capacity plans for Ajax applications can be 3 to 4 times times that of standard Web applications. <br/><br/> One might ask: How does this affect WebLogic server? Each HTTP request sent to WebLogic uses an execution thread. Depending on the nature of Ajax programming and the constant dispatch of many short-term requests in the form of polling, this behavior pattern can cause a large number of client requests to continually impact the server situation. Over the years, WebLogic has taken this issue into account and has built a pretty good feature, the Futureresponseservlet. This paradigm is built on the basis of the asynchronous servlet concept. Starting with version 6.1, this feature allows developers to provide truly asynchronous notifications from the server without requiring client rotation of events and using thread execution on the server side. Before 9.x, Bea was in no hurry to expose the class. <br/><br/> How to use the class in real life? Let's take a look at an example. Suppose the business requirement is to build a web-based application that sends data to the server in an almost real-time manner without refreshing the bangsThe navigator. Such an application can submit a request to the server that takes a long time to process, and still be able to receive asynchronous events about its state and listen for events. From a technical point of view, there are many ways to implement this. One approach is to use a Java applet that communicates with the Java servlet to obtain asynchronous information. This is a good approach, but it's a little inconvenient for users because they have to download a JVM and download an applet to the browser. In addition, you must maintain a persistent socket connection from the client to the server to receive asynchronous messages. Imagine that if 1000 users use the applet, then 1000 execution threads are almost empty waiting to send an event notification to the client. Of course, there are other methods, such as building polling mechanisms from applets or AJAX applications to periodically check for new data. If the data is not often received, polling becomes useless, and the server resource is wasted and the execution thread is consumed. Instead, the server can poll periodically, propagate the event back to the client, and maintain the socket thread without using a persistent thread of execution. This is very similar to how Java NiO is run. Ideally, we would like to build an application that receives event notifications asynchronously from the server without using persistent thread execution on the server side, whether it is an applet or a thin Web application based on Ajax. <br/><br/> A solution to this problem is to create a servlet that extends the Futureresponseservlet class. The browser establishes a single connection to the Futureresponseservlet class and registers itself as a listener in another thread. Whenever an event is received on the server side, the thread notifies the client of the event. The server and the client remain asynchronous without using a persistent thread of execution. This model can be extended for multiple concurrent users. <br/><br/> This article does not intend to describe how to build AJAX applications. There are already a lot of articles in this field. The focus of this article is on the importance of asynchronous processing of presentation layers, such as Ajax, applets, or any front-end application. Listing 1 shows an example. <br/><br/><table bordercolor= #cccccc width= "90%" Align=center bgcolor= #e3e3e3 border=1><tr> <td>import java.io.IOException; &LT;BR/>import JAva.io.PrintWriter; <br/>import java.util.Date; <br/>import Java.util.Stack; <br/>import javax.servlet.ServletException; <br/>import javax.servlet.http.HttpServletRequest; <br/>import Weblogic.servlet.FutureResponseServlet; <br/>import Weblogic.servlet.FutureServletResponse; &LT;BR/>//An Asynchronousservlet this handles HTTP requests from a ' separate ' thread and <br/>//not the Execu Te thread used to invoke the this servlet. &LT;BR/>public class Asynchronousserverresponseservlet extends Futureresponseservlet {<br/><br/> Private final Notifier Notifier; <br/><br/>public Asynchronousserverresponseservlet () {<br/> this.notifier = new notifier (); &LT;BR/ > This.notifier.start (); <br/&gt} <br/><br/>public void Service (HttpServletRequest request, futureservletresponse response) Throws Ioexception,servletexception {<br/>//Push this client's request to a buffer and return IMmediately. &LT;BR/>//asynchronous processing occurs in the Run method of the Notifier Thread <br/> Notifier.poll (Request , response); <br/&gt} <br/><br/>class Notifier extends Thread {<br/> private static Stack clients = new Stac K (); <br/> Void Poll (HttpServletRequest request, futureservletresponse response) {<br/> Clients.push (New Clien T (request, response)); <br/>} <br/><br/>public void Run () {<br/> while (!clients.empty ()) {<br/> Client cl Ient = null; <br/> try{<br/> client = (client) clients.pop (); <br/> PrintWriter pw = Client.response.getWrit ER (); <br/> for (int j = 0; J < J + +) {<br/> pw.println ("" "" "); <br/> Pw.flush (); <br/>} <br/> (); <br/>} <br/> catch (Throwable t) {<br/> t.printstacktrace (); <br/>} <br/> Final  ly {<br/> try {<br/> client.response.send (); <br/>} <br/> catch (IOException IoE) {<br/> io E.printstacktrace (); <br/>} <br/>} <br/>} <br/>} <br/>} <br/><br/>//inner class that Ho LDS o-n to the clients HTTP request and Response <br/>class Client {<br/> private httpservletrequest reques T <br/> Private Futureservletresponse response; <br/> Private Client (httpservletrequest request, futureservletresponse response) {<br/> This.request = req uest; <br/> This.response = response; <br/>} <br/&gt} </td></tr></table><br/> You can see that this example is very simple. The Asynchronousserverresponseservlet class extends the Futureresponseservlet and overrides the service method. Use only one thread (that is, the notifier Class) to handle all client connection responses. For each HTTP request, the servlet registers the socket connection with the notifier thread and returns. Asynchronous events are delivered to the client, while persistent socket connections are maintained. <br/><br/> A single thread can manage multiple client connections! The run () method can be used to callback an event to the client based on a message selection condition. This example only performs a server-side push operation, which is a bit too simple. Line Cheng are used for certain types of event handling. &LT;BR/><br/> In summary, Futureresponseservlet is a good feature when dealing with long-running tasks, allowing developers to improve performance, handle responses in separate threads, and minimize overhead. This method supports scalability when building an asynchronous application.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.