Skip to content

Commit d2d4977

Browse files
committed
docs: [java]-[mvn]-设置接口超时时间
1 parent 2235b1c commit d2d4977

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

clients/java/wechat-ferry-mvn/src/main/java/com/wechat/ferry/handle/WeChatSocketClient.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.wechat.ferry.entity.proto.Wcf.Request;
2424
import com.wechat.ferry.entity.proto.Wcf.Response;
2525
import com.wechat.ferry.entity.proto.Wcf.WxMsg;
26+
import com.wechat.ferry.exception.BizException;
2627
import com.wechat.ferry.service.SDK;
2728
import com.wechat.ferry.utils.HttpClientUtil;
2829
import com.wechat.ferry.utils.XmlJsonConvertUtil;
@@ -120,14 +121,21 @@ public void connectRPC(String url, SDK INSTANCE) {
120121

121122
public Response sendCmd(Request req) {
122123
try {
124+
// 设置超时时间 20s
125+
cmdSocket.setSendTimeout(20000);
123126
ByteBuffer bb = ByteBuffer.wrap(req.toByteArray());
124127
cmdSocket.send(bb);
125128
ByteBuffer ret = ByteBuffer.allocate(BUFFER_SIZE);
126129
long size = cmdSocket.receive(ret, true);
127130
return Response.parseFrom(Arrays.copyOfRange(ret.array(), 0, (int)size));
128131
} catch (Exception e) {
129-
log.error("命令调用失败: ", e);
130-
return null;
132+
if ("Timed out".equals(e.getMessage())) {
133+
log.error("请求超时: ", e);
134+
throw new BizException("请求超时:1.接口耗时太长,2.服务与客户端失去联系,请重启本服务!详细异常信息:" + e.getMessage());
135+
} else {
136+
log.error("命令调用失败: ", e);
137+
throw new BizException("命令调用失败:" + e.getMessage());
138+
}
131139
}
132140
}
133141

0 commit comments

Comments
 (0)