模仿Kafka异步回调

文章作者: 小王是个弟弟
文章链接: https://kpretty.tech/archives/kafka-problem-2
版权声明: 本站所有文章均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 小王是个弟弟

结构

在这里插入图片描述

Callback.java

import java.util.Map;

/**
 * 模仿 kafka 的这个接口用于接收回调信息
 */
public interface Callback {
    void onCompletion(Map<String, String> offset, Exception exception);
}

CallbackImpl.java

import java.util.Map;

public class CallbackImpl implements Callback {
    @Override
    public void onCompletion(Map<String, String> offset, Exception exception) {
        if (null == exception) {
            System.out.println("======"+offset+"======");
        } else {
            exception.printStackTrace();
        }
    }
}

RemoteServer.java

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;

/**
 * 这个就相当于 kafka 实际发消息的类,将消息异步发送(也可以同步)给服务器后返回元数据信息。
 * 首先分析一下既要异步也要支持同步那就需要使用 Future 类了,利用它的 get() 方法实现同步即可!!!
 */
public class RemoteServer<T> {
    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public Future<Map<String, String>> send(T message) {
        return send(message, null);
    }

    public Future<Map<String, String>> send(T message, Callback callback) {

        FutureTask<Map<String, String>> futureTask = new FutureTask<>( () -> {
                    if (callback != null) {
                        if ("error".equals(message)) {
                            callback.onCompletion(null, new RuntimeException(sdf.format(new Date())+"因为你发了error,所以报错了"));
                        } else {
                            callback.onCompletion(getMap(message), null);
                        }
                    }
                    return getMap(message);
                }//里面是一个callable的实现
            );

        new Thread(futureTask).start();

        return futureTask;
    }

    public Map<String, String> getMap(T message) {
        HashMap<String, String> map = new HashMap<>();
        map.put("message", message.toString());
        map.put("offset", message.hashCode() + "");
        map.put("time", sdf.format(new Date()));
        return map;
    }
}

LocalServer.java

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.ExecutionException;

public class LocalServer {
    /**
     * 本地测试
     * 正常消息异步发送+回调
     */
   public static void main(String[] args){
        String message = "message";
        RemoteServer<String> remoteServer = new RemoteServer<>();
        for (int i = 0; i < 10; i++) {
            remoteServer.send(message,new CallbackImpl());
            System.out.println(new Date() + "发过去了");
        }
    }

    /**
     * 正常消息同步发送
     */
    /*public static void main(String[] args) throws ExecutionException, InterruptedException {
        String message = "error";
        RemoteServer<String> remoteServer = new RemoteServer<>();
        for (int i = 0; i < 10; i++) {
            System.out.println(remoteServer.send(message).get());// {offset=96784904, time=2022-04-05 14:52:57, message=error}
            System.out.println(new Date() + "发过去了");       //  Tue Apr 05 14:52:57 CST 2022发过去了
        }
    }*/


/**
 * 错误消息异步发送+回调
 */
/*    public static void main(String[] args){
        String message = "error";
        RemoteServer<String> remoteServer = new RemoteServer<>();
        for (int i = 0; i < 10; i++) {
            remoteServer.send(message, new CallbackImpl());
            System.out.println(new Date() + "发过去了");
        }
    }
*/
}

正常消息异步发送+回调

在这里插入图片描述

正常消息同步发送

在这里插入图片描述

错误消息异步发送+回调

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值