Java callback mechanism:
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.
Synchronous call is a blocking call. The caller must wait for the execution of the other party to return the result. It is a one-way call;
Callback is a two-way call mode, that is, the called party also calls the interface of the other party when the interface is called;
Asynchronous calling is a mechanism similar to a message or event, but its calling direction is the opposite. When an interface service receives a message or an event, the customer is notified (that is, the customer's interface is called ). Callback and asynchronous call are closely related. Generally, callback is used to register asynchronous messages and message notifications are implemented through asynchronous calls.
This is a boring theoretical explanation of the search. The red part gives me a little understanding of what is going on. Then I can see another example, which makes me understand a lot.
Let's look at the example in Java:
Public class test {
Public static void main (string [] ARGs ){
Foobar Foo = new foobar ();
/** Pay attention to the following code snippet. It passes an anonymous class that implements the icallback interface to the foo object, so that the object of the foobar class is retrieved.
So foobar can call the method in the interface at any time */
Foo. setcallback (New icallback (){
Public void postexec () {system. Out. println ("My (postexec) is implemented in the test class, but I cannot be referenced by the test object," +
"Called by the foobar object ");}
});
}
}
Public interface icallback (){
Void postexec ();
}
Public class foobar ..{
Private icallback callback;
Public void setcallback (icallback callback ){
This. Callback = callback;
}
/* I have not implemented the interface, but I have obtained an object that implements the interface. This object is another class that calls my method (setcallback ())
So I can call the method in the class implementing the interface as needed */
Public void dosomething (){
....
Callback. postexec ();
}
..
}
Description of the above two classes:
1. Class A, Class B
2. Class A implements the icallback interface.
3. Class B has a function setcallback (icallback O) with the icallback interface parameter)
4. When Class A is running, call the setcallback function in Class B to pass in parameters as needed.
5. If Class B has obtained a, the method in the icallback interface implemented by Class B can be called back at any time.
The following describes how to construct your own hibernatetemplate template in hibernate.
Use the template mode to simplify DaO operations on Hibernate
When using spring + hibernate for development, I used the hibernatedaosupport class of spring when writing Dao, and then can easily use gethibernatetemplate () during implementation () after the method, you can call the SAVE (), delete (), update (), and other hibernate session operations, which is very simple. For example:
Gethibernatetemplate (). Save (User );
However, we may not necessarily use spring when using hibernate, so we can simulate the processing method of spring, make a hibernate template, and use the template mode to simplify our development, its main purpose is to simplify development and maximize code reuse. In addition, it helps you have a deeper understanding of the callback mechanism.
1. Now we implement a hibernate template:
Package kick. hibernate;
Import net. SF. hibernate. hibernateexception;
Import net. SF. hibernate. Session;
Import net. SF. hibernate. transaction;
Public class hibernatetemplate {
Public static object run (hibernatecallback callback) throws hibernateexception {
Session session = NULL;
Transaction Tx = NULL;
Try {
Session = hibernatesessionutil. currentsession ();
Tx = session. begintransaction ();
Object result = callback.exe cute (session );
TX. Commit ();
Session. Flush ();
Return result;
} Catch (hibernateexception e ){
TX. rollback ();
Return NULL;
} Finally {
Hibernatesessionutil. closesession ();
}
}
This class is very simple. It is a callback class that implements the hibernatecallback interface. The hibernatecallback class is implemented according to specific requirements during the call.
I would like to emphasize the red part of the Code. In fact, this part is the best embodiment of callback. callbak must be a class object that implements the hibernatecallback interface, the specific implementation of execute (Session S) is implemented in this implementation class, but we cannot display the calling of specific methods in this implementation class, such as execute (), I just called the method in the form of an interface. I am dizzy. After talking a lot about it, I am getting confused. Forget it. Let's continue to write the example. Let's look at it with examples, it may be faster to understand.
2. Return the interface hibernatecallback:
Package kick. hibernate;
Import net. SF. hibernate. hibernateexception;
Import net. SF. hibernate. Session;
Public interface hibernatecallback {
Object execute (session) throws hibernateexception;
}
Now, we can use this template as follows:
// Implement the hibernatecallback class according to specific requirements during the call. Here I am implementing the stored business
Hibernatetemplate. Run (
New hibernatecallback (){
Public object execute (session) throws hibernateexception {
Session. Save (User );
Return NULL;
}
} // This is actually an anonymous class
);
However, this is not as simple as spring. Don't worry, "There will be bread." Oh, we will.
3. implement our own hibernatesupport class:
From the code above, we can see that we need to implement the hibernatecallback interface by ourselves, and the code is repeated every time we implement it. So let's abstract these implementations into our hibernatesupport class. Look at the code above and we will know that the purpose of implementing the hibernatecallback interface is to call the session. Save () method, that is, the session method. The Code is as follows:
Package kick. hibernate;
Import java. Io. serializable;
Import net. SF. hibernate. hibernateexception;
Import net. SF. hibernate. Session;
Public class hibernatesupport {
Public object save (final object) throws hibernateexception {
Return hibernatetemplate. Run (New hibernatecallback (){
Public object execute (session) throws hibernateexception {
Session. Save (object );
Return NULL;
}
});
}
Public object save (final object, final serializable ID) throws hibernateexception {
Return hibernatetemplate. Run (New hibernatecallback (){
Public object execute () throws hibernateexception {
Session. Save (object, ID );
Return NULL;
}
});
}
Public object saveorupdate (final object) throws hibernateexception {
Return hibernatetemplate. Run (New hibernatecallback (){
Public object execute (session) throws hibernateexception {
Session. saveorupdate (object );
Return NULL;
}
});
}
................................................................................................
................................................................................................
................................................................................................
Call other session methods.
}
4. Abstract rootdao:
This class is an abstract class and inherits this class when implementing its own DAO class. This class has a hibernatesupport object. You can use the gethibernatetemplate () method in the subclass to obtain the object and then call its corresponding method. The implementation code is as follows:
Package kick. hibernate. Dao;
Import net. SF. hibernate. Session;
Import kick. hibernate. hibernatetemplateimpl;
Public abstract class rootdao {
Private hibernatesupport temp = NULL;
/**
* @ Return returns the temp.
*/
Public hibernatetemplateimpl gethibernatetemplate (session ){
Return new hibernatesupport ();
}
}
5. Example:
Define your own DAO class. The implementation code is as follows:
Public class userdaoimpl extends rootdao implements userdaointerface {
Public void saveuser (User user) throws kickexception {
Gethibernatetemplate (). saveorupdate (User );
}
................................................................................................
Implement other methods
................................................................................................
}
No? The red code implements the hibernatesupport of spring.