SettableFuture ACK通过mq确认

import com.google.common.util.concurrent.SettableFuture;
import org.springframework.util.StringUtils;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class AckManager {
    public final static Map<String, SettableFuture<AckDTO>> FUTURE_MAP = new ConcurrentHashMap<>();
    public final static int TIMEOUT = 5000;

    public static void createMessage(String id) {
        if (!StringUtils.isEmpty(id)) {
            FUTURE_MAP.putIfAbsent(id, SettableFuture.create());
        }
    }

    public static void setMessage(String id, AckDTO ackDTO) {
        if (!StringUtils.isEmpty(id) && FUTURE_MAP.containsKey(id)) {
            SettableFuture<AckDTO> future = FUTURE_MAP.get(id);
            future.set(ackDTO);
        } else {
            System.err.println("Not Found Message " + id);
        }
    }

    public static void setError(String id, Exception e) {
        if (!StringUtils.isEmpty(id) && FUTURE_MAP.containsKey(id)) {
            SettableFuture<AckDTO> future = FUTURE_MAP.get(id);
            future.setException(e);
        } else {
            System.err.println("Not Found Message " + id);
        }
    }

    public static AckDTO getMessage(String id) {
        if (!StringUtils.isEmpty(id) && FUTURE_MAP.containsKey(id)) {
            SettableFuture<AckDTO> future = FUTURE_MAP.get(id);
            AckDTO ackDTO;
            try {
                // 正常返回
                ackDTO = future.get(TIMEOUT, TimeUnit.MILLISECONDS);
            } catch (InterruptedException e) {
                // 系统异常返回
                ackDTO = new AckDTO(id, 501, "error", e.getMessage());
            } catch (ExecutionException e) {
                // 自定义异常返回
                ackDTO = new AckDTO(id, 502, "error", e.getMessage());
            } catch (TimeoutException e) {
                // 超时返回
                ackDTO = new AckDTO(id, 503, "error", e.getMessage());
            }
            FUTURE_MAP.remove(id);
            return ackDTO;
        } else {
            // 已消费
            return new AckDTO(id, 404, "error", "Not Found Message");
        }
    }
}
public class AckDTO {
    private String id;
    private Integer code;
    private String type;
    private Object value;

    public AckDTO() {
    }

    public AckDTO(String id, Integer code, String type, Object value) {
        this.id = id;
        this.code = code;
        this.type = type;
        this.value = value;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public Object getValue() {
        return value;
    }

    public void setValue(Object value) {
        this.value = value;
    }
}

首先客户端:

调用服务端接口(此时应该包含一个唯一变量,比如id),调用后则创建ack再等待

private String id="唯一的id";
service.接口(id);
AckManager.createMessage(id);//创建ack
AckManager.getMessage(id);//等待获取确认的信息,时间为AckManager设置的时间

接着在mq的消费端监听ack的消息,

    @Override
    public void onMessage(String message) {
        //将消息内容转换成ack,并从ack中拿到唯一的id,并进行设置值;(转换异常等自行处理)
        AckDTO ack= JsonUtil.parse(message, AckDTO.class);
        AckManager.setMessage(ack.getId(), ack);
    }

服务端:

在接收到客户端的请求,并完成相应处理 后,把处理后的结果封装到ack中并放到mq中


private id = "获取从客户端传过来的id";
//封装执行结果到ack中
AckDTO ack = new AckDTO(id, 200, "success", "发送成功");
//发送到mq中
rocketMQTemplate.syncSend(ackTopic, JsonUtil.toJson(ack));

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值