In a Web/WAP-based HTTP-based large user application, instant messaging is an important indicator.
The feeling of request response time can be said to be the first indicator to measure the Web applications of large-scale users.
Timely asynchronous processing not only improves the response speed to the client, but also makes the interaction process more reliable!
If we have to do something, we need to know when to make it more appropriate. In software design, we always put the most difficult
The implementation part is done by the API provider. In fact, the caller is allowed to do the work and the API provider is allowed to do the work in the whole process (and eventually changes ).
Can be reduced, but put in the most suitable place, it makes your entire architecture very elegant.
Similarly, if a logic in a request response must be executed, you should not
So asynchronous calling is a very good solution.
For some time-consuming "background operations", we can start another thread in the current response processing, and display
The content is instantly fed back to the user. For example, the user clicks a page and sends a letter to someone. We can execute the mail process in the new thread.
Line, and the current response thread tells the user that "the mail has been successfully sent to someone", so the real sending time does not affect the user to see this
Tips.
In fact, for the action "send mail", the user cannot know the result in real time, even if your SMTP is 101% successfully sent, but the mail
You cannot control the routing in the network and the reliability of the recipient's email server. Therefore, as long as you "Tell the user that the mail has been sent ",
Enough, you cannot tell him that "the letter has been successfully received by the recipient". In case that the new thread does not know why it is not possible, use
Users cannot check whether your prompt is reliable.
The core of the UOP idea is that no matter how you implement it, it is the fundamental purpose to give users the best feelings. Others are just means.
Asynchronous calling not only improves the interaction response speed, but also improves the reliability.
In any case, you cannot guarantee that the "mail" action can be 100% linked to the SMTP server.
The execution will continue later, and an exception often occurs at this time, that is, the user still sees the error message after waiting for the timeout.
The perception is fatal to your application. Therefore, if you process this process in a thread invisible to the user, the user will not be aware of it.
In many cases, when processing a request, we need to link another URL or database. if the content is not obtained through these resources
To output the necessary information to the user, we do not need to process it in the current thread, because once the URL or database you want to link cannot be linked
An exception is thrown after a long wait.
The solution I often use is to put the object to be processed in a static data structure, in the normal response logic such as Servlet
In the service method, it is enough to put this object into this data structure, and then the service method continues to execute with the user
And the background starts a special listener to process the data, so that even if the listener is blocked or abnormal, the user
Unknown. This solution is very suitable for triggering logs, notifications, and other background operations. It not only enhances the friendliness of the interactive interface, but also you
The logical structure of the architecture is also very clear and reasonable.
The programming habit I often use is not to implement specific methods in the main logic. For example, a service method needs to process several steps.
If there are more than 300 rowsCodeYour response does not seem clear enough. I have been doing this all the time:
Public void Service (Arglist .....) {
Step 1 ();
Step 2 ();
step 3 ();
Step 4 ();
Step 5 ();
........
}
Private void Step1 (){
// Specific operation
}
Private void step2 (){
// Specific operation
}
Private void Step3 (){
// Specific operation
}
Private void step4 (){
// Specific operation
}
Private void step5 (){
// Specific operation
}
If you want to look at your logical process, you must first look at the service method. There are only a few simple lines to clearly tell him the entire process.
What is the process? For how to implement it, see the specific implementation below.
In fact, asynchronous processing itself is just an extension of this programming habit. Since it can improve the speed of user interaction and ensure
The reliability of each other makes your structure so elegant. Use asynchronous processing in due time!