Java asynchronous callback instance Parsing

Source: Internet
Author: User
Tags sendmsg

Java asynchronous callback instance Parsing

What is callback? Today, I took a silly screenshot and asked, and then Chen daniu replied, "just a callback ...". At this time, tens of millions of Grass-mud horses fly

Haha, looking at the source code, enjoying the role of this callback in the Code is really beautiful. Summary.

1. What is callback?

Callback. The callback between the caller and the called user must be called first. So in Baidu encyclopedia, the following is true:

There are always some interfaces between software modules. In terms of calling methods, they can be divided into three types: Synchronous call, callback and asynchronous call.

Callback is a special call, and the three methods are also a little different.

1. synchronous callback, that is, blocking, one-way.

2. Callback, that is, two-way gears similar to the bicycle ).

3. asynchronous calls: notifications are sent through asynchronous messages.

Ii. asynchronous callback java case in CS)

For example, the following scenario is simulated: the client sends the msg to the server, and the server calls back the message to the client five seconds after processing. The Code is as follows:

Callback interface class:

 
 
  1. /**
  2. * @ Author Jeff Lee
  3. * @ Since 2015-10-21 21:34:21
  4. * Callback mode-callback Interface Class
  5. */
  6. Public interface CSCallBack {
  7. Public void process (String status );
  8. }

Simulate the client:

 
 
  1. /**
  2. * @ Author Jeff Lee
  3. * @ Since 2015-10-21 21:25:14
  4. * Callback mode-simulate the client class
  5. */
  6. Public class Client implements CSCallBack {
  7.  
  8. Private Server server;
  9.  
  10. Public Client (Server server ){
  11. This. server = server;
  12. }
  13.  
  14. Public void sendMsg (final String msg ){
  15. System. out. println ("client: sent message:" + msg );
  16. New Thread (new Runnable (){
  17. @ Override
  18. Public void run (){
  19. Server. getClientMsg (Client. this, msg );
  20. }
  21. }). Start ();
  22. System. out. println ("client: asynchronous transmission successful ");
  23. }
  24.  
  25. @ Override
  26. Public void process (String status ){
  27. System. out. println ("client: Server callback status:" + status );
  28. }
  29. }

Simulated Server:

 
 
  1. /**
  2. * @ Author Jeff Lee
  3. * @ Since 2015-10-21 21:24:15
  4. * Callback mode-simulate the server class
  5. */
  6. Public class Server {
  7.  
  8. Public void getClientMsg (CSCallBack csCallBack, String msg ){
  9. System. out. println ("server: the server receives the message sent by the Client:" + msg );
  10.  
  11. // Simulate data processing on the server
  12. Try {
  13. Thread. sleep (5*1000 );
  14. } Catch (InterruptedException e ){
  15. E. printStackTrace ();
  16. }
  17. System. out. println ("server: Data Processing successful, return success 200 ");
  18. String status = "200 ";
  19. CsCallBack. process (status );
  20. }
  21. }

Test class:

 
 
  1. /**
  2. * @ Author Jeff Lee
  3. * @ Since 2015-10-21 21:24:15
  4. * Callback mode-test class
  5. */
  6. Public class CallBackTest {
  7. Public static void main (String [] args ){
  8. Server server = new Server ();
  9. Client client = new Client (server );
  10.  
  11. Client. sendMsg ("Server, Hello ~ ");
  12. }
  13. }

Run the following test class:

Client: The sent message is Server, Hello ~
Client: the message is sent asynchronously.
Server: the Server receives the following message from the client: Server, Hello ~

Here we simulate the server's data processing time and wait for 5 seconds)
Server: data is processed successfully, and 200 is returned.
Client: Server callback status: 200

Analyze the code step by step. The core is summarized as follows:

1. The interface is used as a method parameter, and its actual incoming reference points to the implementation class.

2. In the sendMsg method of the Client, the parameter is final, because it can be used by a new internal thread. Asynchronous is embodied here.

3. Call getClientMsg () of the server. The parameter is passed to the first vertex of the Client ).

It is worth mentioning that

-The open source code is on my gitHub ~

Iii. Callback application scenarios

What scenarios are callback currently used in? From operating system to developer call:

1. Message Mechanism on Windows Platform

2. Call the interface asynchronously to respond to the business logic based on the returned status.

3. The Filter in Servlet is based on the callback function and must be supported by containers.

Supplement: The difference between Filter and Interceptor is that the Interceptor is based on the Java reflection mechanism and has nothing to do with the container. But it is similar to the callback mechanism.

In short, this design allows the underlying code to call the subroutine of the high-level definition Implementation Layer, enhancing the flexibility of the program.

Iv. Mode Comparison

The Filter and Intercepter are similar. In fact, the interface callback mechanism is similar to the observer mode in a design mode:

Observer mode:

GOF said-"defines an object's one-to-many dependency. When the status of an object changes, all objects dependent on it are notified and updated ." It is implemented through the interface callback method, that is, it is the embodiment of callback.

Interface callback:

The difference with the observer mode is that it is a principle rather than a specific implementation.

5. Experiences

The four steps are summarized as follows:

The mechanism is the principle.

Mode.

Remember specific scenarios and common patterns.

Then, we will have a deep understanding of the principles.

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.