mirror of
https://codeup.aliyun.com/67c68d4e484ca2f0a13ac3c1/ydc/jsowell-charger-web.git
synced 2026-04-21 11:35:12 +08:00
同步获取响应数据
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
package com.jsowell.common.protocol;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public abstract class Message {
|
||||
|
||||
protected Byte messageType;
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.jsowell.common.protocol;
|
||||
|
||||
import com.jsowell.common.protocol.Message;
|
||||
import com.jsowell.common.protocol.RpcRequest;
|
||||
import com.jsowell.common.protocol.RpcResponse;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
public class MessageConstant {
|
||||
|
||||
public final static Byte rpcRequest = 1;
|
||||
public final static Byte rpcResponse = 2;
|
||||
|
||||
public static Map<Byte, Class<? extends Message>> messageTypeMap = new ConcurrentHashMap<>();
|
||||
|
||||
static {
|
||||
messageTypeMap.put(rpcRequest, RpcRequest.class);
|
||||
messageTypeMap.put(rpcResponse, RpcResponse.class);
|
||||
}
|
||||
|
||||
public static Class<? extends Message> getMessageClass(Byte messageType){
|
||||
return messageTypeMap.get(messageType);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.jsowell.common.protocol;
|
||||
|
||||
import com.jsowell.common.protocol.Message;
|
||||
import lombok.Data;
|
||||
import lombok.ToString;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
@Data
|
||||
@ToString
|
||||
public class RpcRequest extends Message {
|
||||
|
||||
private String id;
|
||||
|
||||
private String param;
|
||||
|
||||
public RpcRequest() {
|
||||
this.id = UUID.randomUUID().toString();
|
||||
super.messageType = MessageConstant.rpcRequest;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.jsowell.common.protocol;
|
||||
|
||||
import com.jsowell.common.protocol.Message;
|
||||
import lombok.Data;
|
||||
import lombok.ToString;
|
||||
|
||||
@Data
|
||||
@ToString
|
||||
public class RpcResponse extends Message {
|
||||
|
||||
private String id;
|
||||
|
||||
private String result;
|
||||
|
||||
public RpcResponse() {
|
||||
super.messageType = MessageConstant.rpcResponse;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package com.jsowell.common.protocol;
|
||||
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class SyncPromise {
|
||||
|
||||
// 用于接收结果
|
||||
// private RpcResponse rpcResponse;
|
||||
|
||||
private byte[] rpcResult;
|
||||
|
||||
private final CountDownLatch countDownLatch = new CountDownLatch(1);
|
||||
|
||||
// 用于判断是否超时
|
||||
private boolean isTimeout = false;
|
||||
|
||||
public boolean isTimeout() {
|
||||
return isTimeout;
|
||||
}
|
||||
|
||||
public byte[] getRpcResult() {
|
||||
return rpcResult;
|
||||
}
|
||||
|
||||
public void setRpcResult(byte[] rpcResult) {
|
||||
this.rpcResult = rpcResult;
|
||||
}
|
||||
|
||||
/**
|
||||
* 同步等待返回结果
|
||||
*/
|
||||
// public RpcResponse get(long timeout, TimeUnit unit) throws InterruptedException {
|
||||
// // 等待阻塞,超时时间内countDownLatch减到0,将提前唤醒,以此作为是否超时判断
|
||||
// boolean earlyWakeUp = countDownLatch.await(timeout, unit);
|
||||
//
|
||||
// if(earlyWakeUp) {
|
||||
// // 超时时间内countDownLatch减到0,提前唤醒,说明已有结果
|
||||
// return rpcResponse;
|
||||
// } else {
|
||||
// // 超时时间内countDownLatch没有减到0,自动唤醒,说明超时时间内没有等到结果
|
||||
// isTimeout = true;
|
||||
// return null;
|
||||
// }
|
||||
// }
|
||||
|
||||
public byte[] get2(long timeout, TimeUnit unit) throws InterruptedException {
|
||||
// 等待阻塞,超时时间内countDownLatch减到0,将提前唤醒,以此作为是否超时判断
|
||||
boolean earlyWakeUp = countDownLatch.await(timeout, unit);
|
||||
|
||||
if(earlyWakeUp) {
|
||||
// 超时时间内countDownLatch减到0,提前唤醒,说明已有结果
|
||||
return rpcResult;
|
||||
} else {
|
||||
// 超时时间内countDownLatch没有减到0,自动唤醒,说明超时时间内没有等到结果
|
||||
isTimeout = true;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public void wake() {
|
||||
countDownLatch.countDown();
|
||||
}
|
||||
|
||||
// public RpcResponse getRpcResponse() {
|
||||
// return rpcResponse;
|
||||
// }
|
||||
// public void setRpcResponse(RpcResponse rpcResponse) {
|
||||
// this.rpcResponse = rpcResponse;
|
||||
// }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.jsowell.common.util;
|
||||
|
||||
import com.jsowell.common.protocol.SyncPromise;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
@Slf4j
|
||||
public class RpcUtil {
|
||||
|
||||
private final static Map<String, SyncPromise> syncPromiseMap = new ConcurrentHashMap<>();
|
||||
|
||||
public static Map<String, SyncPromise> getSyncPromiseMap() {
|
||||
return syncPromiseMap;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user