一 代码
1 接口
/**
* Copyright (C), 2020-2020, 软件公司
* FileName: CallBack
* Author: cakin
* Date: 2020/4/25
* Description: 回调接口
*/
package asymsg;
/**
* @className: CallBack
* @description: 回调接口
* @date: 2020/4/25
* @author: cakin
*/
public interface CallBack {
/**
* 功能描述:执行回调方法
*
* @param jsonString 将处理后的结果作为参数返回给回调方法
* @author cakin
* @date 2020/4/25
*/
void execute( String jsonString );
}
2 发送者
/**
* Copyright (C), 2020-2020, 软件公司
* FileName: Local
* Author: cakin
* Date: 2020/4/25
* Description: 简单本地发送异步消息的类
*/
package asymsg;
import GPer.Teacher;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
/**
* @ClassName: Local
* @Description: 简单本地发送异步消息的类
* @Date: 2020/4/25
* @Author: cakin
*/
@Slf4j
public class Local implements CallBack, Runnable {
/**
* 远程接收消息的类,模拟point-to-point
*/
private Remote remote;
/**
* 发送出去的消息
*/
private String message;
public Local( Remote remote, String message ) {
super();
this.remote = remote;
this.message = message;
}
public Local() {
}
/**
* @className: Local
* @description: 发送消息
* @date: 2020/4/25
* @author: cakin
*/
public void sendMessage() {
/**
* 当前线程的名称
*/
log.info("1:" + Thread.currentThread().getName());
/**
* 创建一个新的线程发送消息
*/
SendThead thread = new SendThead("SendThead");
thread.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException( Thread t, Throwable e ) {
log.info(thread.getName() + ":" + e.getMessage());
}
});
thread.start();
/**
* 当前线程继续执行
*/
log.info("2:Message has been sent by Local~!");
}
/**
* 功能描述:发送消息后的回调函数
*
* @param jsonString 回调参数
* @author cakin
* @date 2020/4/25
*/
public void execute( String jsonString ) {
/**
* 打印返回的消息*
*/
log.info("5:" + jsonString);
/**
* 打印发送消息的线程名称
*/
log.info("6:" + Thread.currentThread().getName());
/**
* 中断发送消息的线程
*/
Thread.interrupted();
}
public static void main( String[] args ) {
Local local = new Local(new Remote(), "Hello");
local.sendMessage();
}
public void run() {
remote.executeMessage(message, this);
}
public class SendThead extends Thread {
public SendThead( String name ) {
}
public void run() {
remote.executeMessage(JSON.toJSONString(new Teacher("cakin")), new Local());
}
}
}
3 接收者
/**
* Copyright (C), 2020-2020, 软件公司
* FileName: Remote
* Author: cakin
* Date: 2020/4/25
* Description:处理消息的远程类
*/
package asymsg;
import GPer.Teacher;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import common.Constant;
import lombok.extern.slf4j.Slf4j;
/**
* @className: Remote
* @description: 处理消息的远程类
* @date: 2020/4/25
* @author: cakin
*/
@Slf4j
public class Remote {
/**
* 处理消息
*
* @param msg 接收的消息
* @param callBack 回调函数处理类
*/
public void executeMessage( String msg, CallBack callBack ) {
/**
* 模拟远程类正在处理其他事情,可能需要花费许多时间
*/
try {
Thread.sleep(Constant.NUM1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
/**
* 处理完其他事情,现在来处理消息
*/
JSONObject json = JSONObject.parseObject(msg);
Teacher teacher = JSON.toJavaObject(json, Teacher.class);
log.info("3:" + teacher.toString());
log.info("4:I hava executed the message by Local");
/**
* 执行回调
*/
String jsonString = "cakin";
callBack.execute(jsonString);
}
}
二 测试
10:06:38.076 [main] INFO asymsg.Local - 1:main
10:06:38.082 [main] INFO asymsg.Local - 2:Message has been sent by Local~!
10:06:39.267 [Thread-0] INFO asymsg.Remote - 3:Teacher(name=cakin)
10:06:39.267 [Thread-0] INFO asymsg.Remote - 4:I hava executed the message by Local
10:06:39.268 [Thread-0] INFO asymsg.Local - 5:cakin
10:06:39.268 [Thread-0] INFO asymsg.Local - 6:Thread-0
三参考