写点什么

Java 回调 (Callback) 接口学习使用

用户头像
魏杰
关注
发布于: 2020 年 09 月 22 日
Java 回调(Callback)接口学习使用

1.什么是回调(Callback)?

回调函数,简称回调,是指通过函数参数传递到其他代码的某一块可执行代码的引用,这一设计允许了底层代码调用在高层定义的子程序。



简单来说:A调用了B的b()方法,那b方法如果包含对A中实现的逻辑的调用,这个过程就被称作回调。



2.Java代码示例

package callback;
public class MQ {
class Producer{
public void send(String msg, MsgStateCallback msgStateCallback) {
System.out.println("send msg :" + msg);
int ACK = 1;
if (ACK == 0){
msgStateCallback.succ();
}else {
msgStateCallback.fail();
}
}
}
interface MsgStateCallback{
void succ();
void fail();
}
public static void main(String[] args) {
MQ mq = new MQ();
Producer producer = mq.new Producer();
String msg = "my msg";
producer.send(msg, new MsgStateCallback() {
@Override
public void succ() {
System.out.println("消息发送成功");
}
@Override
public void fail() {
System.out.println("消息发送失败");
}
});
}
}



Java回调的四种写法

1.反射

Java 的反射机制允许我们获取类的信息,其中包括类的方法。我们将以 Method 类型去获取回调函数,然后传递给请求函数。

package callback;
import java.lang.reflect.Method;
public class InvokeCallback {
class Request{
public void send(Class clazz, Method method) throws Exception {
Thread.sleep(3000);
System.out.println("收到响应");
method.invoke(clazz.newInstance());
}
}
/**
* 记得加上static,如果不加上会找不到该类
*/
static class CallBack{
public void processResponse(){
System.out.println("响应处理");
}
}
public static void main(String[] args) throws InterruptedException, NoSuchMethodException {
final InvokeCallback invokeCallback = new InvokeCallback();
final Request request = invokeCallback.new Request();
new Thread(new Runnable() {
@Override
public void run() {
try {
request.send(CallBack.class, CallBack.class.getMethod("processResponse"));
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
System.out.println("请求发送完成");
Thread.sleep(1000 * 1000);
}
}



2.直接调用

将参数改成Callback示例

package callback;
public class DirectCallback {
class Request{
public void send(InvokeCallback.CallBack callBack) throws Exception {
// 模拟等待响应
Thread.sleep(3000);
System.out.println("[Request]:收到响应");
callBack.processResponse();
}
}
public static void main(String[] args) throws InterruptedException {
DirectCallback directCallback = new DirectCallback();
final Request request = directCallback.new Request();
System.out.println("发送请求");
new Thread(new Runnable() {
@Override
public void run() {
try {
request.send(new InvokeCallback.CallBack());
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
System.out.println("请求已经发送");
Thread.sleep(10 * 1000);
}
}



3.接口调用

Callback作为接口传入参数

Java代码示例中就是接口调用方式!!!

package callback;
import java.lang.reflect.Method;
public class InterfaceCallback {
class Request{
public void send(ResponseCallback responseCallback) throws Exception {
Thread.sleep(3000);
responseCallback.processResponse();
}
}
interface ResponseCallback{
void processResponse();
}
public static void main(String[] args) throws InterruptedException {
InterfaceCallback interfaceCallback = new InterfaceCallback();
final Request request = interfaceCallback.new Request();
System.out.println("发送请求");
new Thread(new Runnable() {
@Override
public void run() {
try {
request.send(new ResponseCallback() {
@Override
public void processResponse() {
System.out.println("收到响应");
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
System.out.println("请求已发送");
Thread.sleep(10000);
}
}



4.Lambda表达式

package callback;
import java.lang.reflect.Method;
public class LambdaCallback {
class Request{
public void send(ResponseCallback responseCallback) throws Exception {
Thread.sleep(3000);
responseCallback.processResponse();
}
}
interface ResponseCallback{
void processResponse();
}
public static void main(String[] args) throws Exception {
LambdaCallback lambdaCallback = new LambdaCallback();
Request request = lambdaCallback.new Request();
System.out.println("发送请求");
new Thread(() -> {
try {
request.send(() -> System.out.println("处理响应"));
} catch (Exception e) {
e.printStackTrace();
}
} ).start();
System.out.println("请求已发送");
}
}



参考博客:

https://juejin.im/post/6844904066129084429

http://gaocegege.com/Blog/java/java-callback

用户头像

魏杰

关注

还未添加个人签名 2020.09.02 加入

还未添加个人简介

评论

发布
暂无评论
Java 回调(Callback)接口学习使用