use of info.xiancloud.core.message.sender.local.DefaultLocalAsyncSender in project xian by happyyangyuan.
the class NonblockingMqttCallBack method messageArrived.
@Override
public void messageArrived(String topicIgnored, MqttMessage message) throws Exception {
try {
/*第一个参数如果包含"."会被eclipse paho转为"/",所以,不要依赖这个参数!*/
String msgFullStr = message.toString();
JSONObject json = JSONObject.parseObject(msgFullStr);
IdManager.makeSureMsgId(json);
MessageType messageType = MessageType.getMessageType(json);
switch(messageType) {
case offline:
LOG.info(String.format("离线广播: %s", json));
break;
case request:
UnitRequest request = Reflection.toType(json, UnitRequest.class);
logMqttFly(request, msgFullStr);
String group = request.getContext().getGroup(), unit = request.getContext().getUnit();
request.getContext().setFromRemote(true);
ISequencer.build(group, unit, json).sequence(// run this runnable if succeeded.
() -> new DefaultLocalAsyncSender(request, new NotifyHandler() {
protected void handle(UnitResponse unitResponse) {
LocalNodeManager.sendBack(unitResponse);
}
}).send(), /*failed directly, call this handler*/
new NotifyHandler() {
protected void handle(UnitResponse unitResponse) {
LocalNodeManager.sendBack(unitResponse);
}
});
break;
case response:
LOG.debug("ssid只在远程请求和响应时才会有值");
UnitResponse response = Reflection.toType(json, UnitResponse.class);
logMqttFly(response, msgFullStr);
String ssid = response.getContext().getSsid();
NotifyHandler handler = LocalNodeManager.handleMap.getIfPresent(ssid);
LocalNodeManager.handleMap.invalidate(ssid);
UnitResponse responseUnitResponse = UnitResponse.create(json);
if (handler == null) {
LOG.error(String.format("ssid=%s的消息没有找到对应的notifyHandler!整个消息内容=%s,", ssid, json), new Throwable());
break;
}
try {
ThreadPoolManager.execute(() -> {
handler.callback(responseUnitResponse);
});
} catch (RejectedExecutionException threadPoolAlreadyShutdown) {
LOG.info("线程池已关闭,这里使用临时线程执行任务,针对停服务时线程池已关闭的情况。");
new Thread(() -> handler.callback(responseUnitResponse)).start();
}
break;
default:
LOG.error("未知的mqtt消息类型:" + messageType, new RuntimeException());
}
} catch (Throwable e) {
LOG.error(e);
} finally {
MsgIdHolder.clear();
}
}
use of info.xiancloud.core.message.sender.local.DefaultLocalAsyncSender in project xian by happyyangyuan.
the class RpcServerDefaultHandler method processMsg.
private void processMsg(final ChannelHandlerContext ctx, final JSONObject json) {
try {
IdManager.makeSureMsgId(json);
if (MessageType.isDefaultRequest(json)) {
UnitRequest request = json.toJavaObject(UnitRequest.class);
request.getContext().setFromRemote(true);
String group = request.getContext().getGroup(), unit = request.getContext().getUnit();
final Consumer<String> backPayloadConsumerOnFailure = payload -> {
LOG.info("rpc server --> client发送失败了,因此这里复用当前长连接响应消息");
ctx.writeAndFlush(payload + Constant.RPC_DELIMITER);
};
ISequencer.build(group, unit, json).sequence(// call this sender if sequencer is succeeded.
new DefaultLocalAsyncSender(request, new NotifyHandler() {
protected void handle(UnitResponse unitResponse) {
LocalNodeManager.sendBack(unitResponse, backPayloadConsumerOnFailure);
}
}), /*failed directly, call this handler*/
new NotifyHandler() {
protected void handle(UnitResponse failureOut) {
LocalNodeManager.sendBack(failureOut, backPayloadConsumerOnFailure);
}
});
} else if (MessageType.isDefaultResponse(json)) {
LOG.debug("这是非常重要的说明:" + "1、客户端发给外部节点的请求期待的响应内容,服务端节在准备好响应结果后立刻与请求端新建一个rpc长连接/复用已存在的长连接,将响应写回去;" + "2、停服务时本地server会先停止,server停止就会关闭socket和server的io线程池,由于server的io线程池和业务线程池是分离的," + "业务线程池会继续运行直到所有任务处理完毕为止。此时,本节点不再有能力接收外部请求了,但是:" + "a.在即将停止的节点内,业务线程池任然需要向外部发送请求以完成业务操作,以及得到响应结果,因此client必须保持打开的。" + "b.在即将停止的节点内,业务线程池需要将本地执行结果返回给远程,这时候server已停,无法复用原管道将结果写回去,因此必须使用依然存活的client" + "将结果写回。" + "因此,有如下逻辑:所有server优先复用当前管道将响应写回去,当SERVER关闭后,业务线程池中后续任务通过未停止的client回写响应结果。");
UnitResponse response = json.toJavaObject(UnitResponse.class);
String ssid = response.getContext().getSsid();
ThreadPoolManager.execute(() -> {
NotifyHandler callback = LocalNodeManager.handleMap.getIfPresent(ssid);
if (callback != null) {
LocalNodeManager.handleMap.invalidate(ssid);
callback.callback(UnitResponse.create(json));
} else {
LOG.error(String.format("ssid=%s的消息没有找到对应的notifyHandler!整个消息内容=%s,", ssid, json), new Throwable());
}
}, response.getContext().getMsgId());
} else
LOG.error("rpc server端只支持request和response两种消息类型,不支持:" + MessageType.getMessageType(json), new RuntimeException());
} catch (Throwable e) {
LOG.error(e);
} finally {
MsgIdHolder.clear();
}
}
Aggregations