English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Java Asynchronous Callback Mechanism
First, What is Callback
Callback, callback. There must be an invocation first, and then there is a callback between the invoker and the invoked. Therefore, in the Baike Encyclopedia, it is as follows:
There are always certain interfaces between software modules. From the perspective of invocation methods, they can be divided into three categories: synchronous invocation, callback, and asynchronous invocation.
Callback is a special type of invocation, and the three methods are also somewhat different.
1Synchronous callback, that is, blocking, unidirectional.
2Callback, that is, bidirectional (similar to the two gears of a bicycle).
3Asynchronous invocation, that is, notification through asynchronous messages.
Second, Asynchronous Callback in CS (Java Example)
For example, here we simulate a scenario: the client sends msg to the server, and after the server processes (5callback to the client, informing of the successful processing. The code is as follows:
Callback interface class:
/** * @author Jeff Lee * @since 2015-10-21 21:34:21 * Callback pattern-Callback interface class */ public interface CSCallBack { public void process(String status); }
Simulated client:
/** * @author Jeff Lee * @since 2015-10-21 21:25:14 * Callback pattern-Simulated client class */ public class Client implements CSCallBack { private Server server; public Client(Server server) { this.server = server; } public void sendMsg(final String msg){ System.out.println("Client: The message sent is: " + msg); new Thread(new Runnable() { @Override public void run() { server.getClientMsg(Client.this,msg); } }).start(); System.out.println("Client: Asynchronous sending successful"); } @Override public void process(String status) { System.out.println("Client: The callback status from the server is: " + status); } }
Simulated server:
/** * @author Jeff Lee * @since 2015-10-21 21:24:15 * Callback pattern-Simulated server class */ public class Server { public void getClientMsg(CSCallBack csCallBack , String msg) { System.out.println("Server: The server received the message sent by the client: " + msg); // Simulating that the server needs to process data try { Thread.sleep(5 * 1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Server: Data processing successful, return status successful" 200"); String status = "200"; csCallBack.process(status); } }
Test class:
/** * @author Jeff Lee * @since 2015-10-21 21:24:15 * Callback pattern-Test class */ public class CallBackTest { public static void main(String[] args) { Server server = new Server(); Client client = new Client(server); client.sendMsg("Server,Hello~"); } }
Run the test class — The result is as follows:
Client: The message sent is: Server,Hello~
Client: Asynchronous sending successful
Server: The server received a message from the client: Server,Hello~
(Simulating the processing time on the server side, waiting5Second)
Server: Data processing successful, return successful status 200
Client: The callback status of the server is:200
Analyze the code step by step, and the core summary is as follows
1The interface as a method parameter actually points to the implementation class through the reference passed in
2In the sendMsg method of Client, the parameter is final because it can be used by a new thread inside the inner class. This reflects the asynchronous nature.
3Call server's getClientMsg(), the parameter passed in is the Client itself (corresponding to the first point).
III. Application Scenarios of Callback
What scenarios are callback mechanisms currently used in more? From the operating system to the developer's call:
1Message mechanism on the Windows platform
2Asynchronous call to the WeChat interface, respond to the business logic according to the WeChat return status.
3In Servlet, the Filter (filter) is based on the callback function and requires container support.
Supplementary: The difference between Filter (filter) and Interceptor (interceptor) is that the interceptor is based on Java's reflection mechanism and is unrelated to the container. But it has the same effect as the callback mechanism.
In short, this design allows the underlying code to call the subprograms defined by the high-level (implementation layer), which enhances the flexibility of the program.
IV. Pattern Comparison
The above mentioned Filter and Intercepter have the same effect. In fact, the interface callback mechanism and a design pattern — Observer Pattern also have similarities:
Observer Pattern:
GOF says — 'A one-to-many dependency relationship of defining an object, when the state of an object changes, all the objects that depend on it are notified and updated.' It is a pattern that is implemented through interface callback methods, that is, it is an embodiment of callback.
Interface Callback:
The difference from the observer pattern is that it is a principle, not a specific implementation.
V. Insights
Summarize the four steps to go:
Mechanism is the principle of it.
Patterns are the embodiment of it.
Remember specific scenarios, common patterns.
Then delve deeply into the principles.
Thank you for reading, I hope it can help everyone. Thank you for your support of this site!