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));