WebSocket applications in Java

Source: Internet
Author: User

In the process of doing web projects, one of the modules that we often need to do is the message module. Typical scenario: A mall system backstage management, I want to achieve if the front desk has a customer order, the backstage will receive the message, so as soon as possible delivery processing.

To achieve the above functions, we have several alternative scenarios.

Scenario 1. Use AJAX short polling, such as every 1 minutes to request the server, let the server go to the database to query, see if there are no new processing orders, and then return to the client.

Scenario 2. Long polling, the principle of long polling is similar to the above, except to take a blocking response (response) method, that is, as long as the server is not responding, the request continues to open until the server has a response.

Ajax short polling is like the client every time a phone call to the server, the server whether there is no data to respond to the client, after the response to hang up the phone, and then the cycle;

Long polling is the client that calls, starts waiting until the server responds, and if the server is not responding, the request hangs over there.

Obviously, both of these options have their drawbacks.

Scenario 1: Every certain time to poll the server, the setting of this time is very critical, too long, the immediacy is not guaranteed, too short, there may be a waste of server performance (mainly CPU), assuming that there is no one order within one hours, but the client is not interrupted every minute to send a request, These requests are wasted.

Scenario 2: Scenario 2 was originally intended to solve the problem of wasting the blind and uncertain sending of requests, but it also had its own drawbacks, first, it took a blocking approach to forcing the connection to persist for a long time, and for the server, The number of connections that can be processed at the same time (the number of concurrent numbers we call programs) is limited, and long polling makes it easy for the server to reach the concurrency limit because it does not release the connection as quickly as a short poll.

Their common disadvantage is that every request and response, processing the real useful data, the server and the client also exchange a bunch of request headers, in response to the first thing, the efficiency of information exchange is not high. In fact, long polling is a pseudo-long connection, and it is still necessary to follow the rules of the HTTP connection: the client request-the server response-frees the connection and, by the way, exchanges some of the same useless information. (resulting in wasted bandwidth)

WebSocket the principle of the Internet, a lot of people also introduced, simply speaking, it is a HTML5 in a protocol, my understanding is that he is a long connection to HTML an upgrade. You can also interpret it as a long connection. But this long connection has the following advantages over the long polling of scenario 2.

1. First, the WebSocket connection needs to be established only once, and at the first connection, the client and server exchange the necessary information, as shown below.

WebSocket applications in Java

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.