use of org.bcos.channel.dto.ChannelResponse in project web3sdk by FISCO-BCOS.
the class ChannelResponseCallback method onTimeout.
public final void onTimeout() {
logger.error("发送消息超时:{}", message.getSeq());
ChannelResponse response = new ChannelResponse();
response.setErrorCode(102);
response.setMessageID(message.getSeq());
response.setErrorMessage("发送消息超时");
response.setContent("");
try {
onResponseMessage(response);
} catch (Exception e) {
logger.error("超时处理错误:", e);
}
service.getSeq2Callback().remove(message.getSeq());
timeout.cancel();
}
use of org.bcos.channel.dto.ChannelResponse in project web3sdk by FISCO-BCOS.
the class ChannelResponseCallback2 method retrySendMessage.
public void retrySendMessage() {
Integer errorCode = 0;
try {
// 选取客户端节点
logger.debug("本地节点数:{}", fromConnectionInfos.size());
setFromConnection(null);
if (fromConnectionInfos.size() > 0) {
Random random = new Random();
Integer index = random.nextInt(fromConnectionInfos.size());
logger.debug("选取:{}", index);
setFromConnection(fromConnectionInfos.get(index));
fromConnectionInfos.remove(fromConnectionInfos.get(index));
}
if (getFromConnection() == null) {
// 所有节点已尝试,无法再重试了
logger.error("发送消息失败,所有重试均失败");
errorCode = 99;
throw new Exception("发送消息失败,所有重试均失败");
}
ChannelHandlerContext ctx = fromChannelConnections.getNetworkConnectionByHost(getFromConnection().getHost(), getFromConnection().getPort());
if (ctx != null && ctx.channel().isActive()) {
ByteBuf out = ctx.alloc().buffer();
message.writeHeader(out);
message.writeExtra(out);
ctx.writeAndFlush(out);
logger.debug("发送消息至 " + fromConnection.getHost() + ":" + String.valueOf(fromConnection.getPort()) + " 成功");
} else {
logger.error("发送节点不可用");
retrySendMessage();
}
} catch (Exception e) {
logger.error("发送消息异常", e);
ChannelResponse response = new ChannelResponse();
response.setErrorCode(errorCode);
response.setErrorMessage(e.getMessage());
try {
onResponseMessage(response);
} catch (Exception ee) {
logger.error("异常处理错误", ee);
}
// 彻底失败后,删掉这个seq
if (message.getSeq() != null) {
service.getSeq2Callback().remove(message.getSeq());
}
if (timeout != null) {
timeout.cancel();
}
return;
}
}
use of org.bcos.channel.dto.ChannelResponse in project web3sdk by FISCO-BCOS.
the class Service method asyncSendChannelMessage2.
public void asyncSendChannelMessage2(ChannelRequest request, ChannelResponseCallback2 callback) {
try {
logger.debug("处理链上链下请求: " + request.getMessageID());
callback.setService(this);
ChannelMessage2 channelMessage = new ChannelMessage2();
channelMessage.setSeq(request.getMessageID());
channelMessage.setResult(0);
// 链上链下请求0x30
channelMessage.setType((short) 0x30);
channelMessage.setData(request.getContent().getBytes());
channelMessage.setTopic(request.getToTopic());
try {
List<ConnectionInfo> fromConnectionInfos = new ArrayList<ConnectionInfo>();
// 设置发送节点
ChannelConnections fromChannelConnections = allChannelConnections.get(orgID);
if (fromChannelConnections == null) {
// 没有找到对应的链
// 返回错误
logger.error("没有找到本机构:{}", orgID);
throw new Exception("未找到本机构");
}
fromConnectionInfos.addAll(fromChannelConnections.getConnections());
logger.debug("发送机构:{} 节点数:{}", request.getFromOrg(), fromChannelConnections.getConnections().size());
callback.setFromChannelConnections(fromChannelConnections);
callback.setFromConnectionInfos(fromConnectionInfos);
// 设置消息内容
callback.setRequest(channelMessage);
seq2Callback.put(request.getMessageID(), callback);
if (request.getTimeout() > 0) {
final ChannelResponseCallback2 callbackInner = callback;
callback.setTimeout(timeoutHandler.newTimeout(new TimerTask() {
ChannelResponseCallback2 _callback = callbackInner;
@Override
public void run(Timeout timeout) throws Exception {
// 处理超时逻辑
_callback.onTimeout();
}
}, request.getTimeout(), TimeUnit.MILLISECONDS));
}
callback.retrySendMessage();
} catch (Exception e) {
logger.error("发送消息异常 消息未发出", e);
ChannelResponse response = new ChannelResponse();
response.setErrorCode(100);
response.setMessageID(request.getMessageID());
response.setErrorMessage(e.getMessage());
response.setContent("");
callback.onResponse(response);
return;
}
} catch (Exception e) {
logger.error("系统错误", e);
}
}
use of org.bcos.channel.dto.ChannelResponse in project web3sdk by FISCO-BCOS.
the class Channel2Client method main.
public static void main(String[] args) throws Exception {
if (args.length < 2) {
System.out.println("参数: 目标topic 总请求量");
return;
}
String topic = args[0];
Integer count = Integer.parseInt(args[1]);
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
logger.debug("初始化链上链下客户端");
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
Service service = context.getBean(Service.class);
// 阻塞到连接成功,或通知
service.run();
System.out.println("3s后开始测试...");
Thread.sleep(1000);
System.out.println("2s后开始测试...");
Thread.sleep(1000);
System.out.println("1s后开始测试...");
Thread.sleep(1000);
System.out.println("开始测试");
System.out.println("===================================================================");
for (Integer i = 0; i < count; ++i) {
Thread.sleep(2000);
ChannelRequest request = new ChannelRequest();
request.setToTopic(topic);
request.setMessageID(service.newSeq());
request.setTimeout(5000);
request.setContent("request seq:" + request.getMessageID());
System.out.println(df.format(LocalDateTime.now()) + " 发送请求 seq:" + String.valueOf(request.getMessageID()) + ", 内容:" + request.getContent());
ChannelResponse response = service.sendChannelMessage2(request);
System.out.println(df.format(LocalDateTime.now()) + "收到回包 seq:" + String.valueOf(response.getMessageID()) + ", 错误码:" + response.getErrorCode() + ", 内容:" + response.getContent());
}
}
use of org.bcos.channel.dto.ChannelResponse in project web3sdk by FISCO-BCOS.
the class ChannelClient method main.
public static void main(String[] args) throws Exception {
if (args.length < 3) {
System.out.println("参数: 请求方 接收方 总请求量");
return;
}
String from = args[0];
String to = args[1];
Integer count = Integer.parseInt(args[2]);
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
logger.debug("初始化链上链下客户端");
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
Service service = context.getBean(Service.class);
// 阻塞到连接成功,或通知
service.run();
// service.setPushCallback(new PushCallback());
System.out.println("3s后开始测试...");
Thread.sleep(1000);
System.out.println("2s后开始测试...");
Thread.sleep(1000);
System.out.println("1s后开始测试...");
Thread.sleep(1000);
System.out.println("开始测试");
System.out.println("===================================================================");
for (Integer i = 0; i < count; ++i) {
Thread.sleep(2000);
ChannelRequest request = new ChannelRequest();
request.setFromOrg(from);
request.setToOrg(to);
request.setMessageID(service.newSeq());
request.setContent("request seq:" + request.getMessageID());
System.out.println(df.format(LocalDateTime.now()) + " 发送请求 seq:" + String.valueOf(request.getMessageID()) + ", 内容:" + request.getContent());
ChannelResponse response = service.sendChannelMessage(request);
System.out.println(df.format(LocalDateTime.now()) + "收到回包 seq:" + String.valueOf(response.getMessageID()) + ", 错误码:" + response.getErrorCode() + ", 内容:" + response.getContent());
}
}
Aggregations