Skip to content

Commit

Permalink
超时处理
Browse files Browse the repository at this point in the history
  • Loading branch information
Liubsyy committed Feb 20, 2024
1 parent a8d278e commit 6722c93
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.UUID;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
* @author Liubsyy
Expand Down Expand Up @@ -79,16 +80,20 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
throw new WriteTimeoutException(String.format("发送请求%s失败",traceId),e);
}


try{
JavaSerializeRPCResponse responseModel = (JavaSerializeRPCResponse)future.get(3, TimeUnit.SECONDS);
if(responseModel != null) {
return responseModel.getResult();
}else {
ReceiveHolder.getInstance().deleteWait(traceId);
logger.error("超时请求,抛弃消息{}",traceId);
logger.error("未获取到响应消息{}",traceId);
return null;
}
}catch (Exception e) {
}catch (TimeoutException timeoutException) {
logger.error("超时请求,抛弃消息{}",traceId);
ReceiveHolder.getInstance().deleteWait(traceId);
return null;
} catch (Exception e) {
if(clientConnection.isRunning()) {
throw e;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ public Future<?> initFuture(String uuid){
}

public void deleteWait(String uuid) {
futureMap.remove(uuid);
CompletableFuture<Object> remove = futureMap.remove(uuid);
if(null != remove) {
remove.cancel(true);
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.UUID;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
* @author Liubsyy
Expand Down Expand Up @@ -68,7 +69,6 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
requestModel.setParams(args);

IModelParser modelParser = serializeModule.getSerializer().getModelParser();
Future<?> future = ReceiveHolder.getInstance().initFuture(traceId);

Channel channel = clientConnection.getChannel(group);

Expand All @@ -77,19 +77,21 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
return null;
}

Future<?> future = ReceiveHolder.getInstance().initFuture(traceId);
try{
channel.writeAndFlush(modelParser.toRequest(requestModel)).sync();
}catch (Exception e) {
logger.error("发送请求{}失败",traceId);
ReceiveHolder.getInstance().deleteWait(traceId);
return null;
}

ResponseModel responseModel = (ResponseModel)future.get(3, TimeUnit.SECONDS);
if(responseModel != null) {
try{
ResponseModel responseModel = (ResponseModel)future.get(3, TimeUnit.SECONDS);
return responseModel.getResult();
}else {
ReceiveHolder.getInstance().deleteWait(traceId);
}catch (TimeoutException timeoutException) {
logger.error("超时请求,抛弃消息{}",traceId);
ReceiveHolder.getInstance().deleteWait(traceId);
return null;
}

Expand Down

0 comments on commit 6722c93

Please sign in to comment.