use of org.apache.eventmesh.runtime.core.protocol.tcp.client.session.send.EventMeshTcpSendResult in project incubator-eventmesh by apache.
the class MessageTransferTask method run.
@Override
public void run() {
long taskExecuteTime = System.currentTimeMillis();
Command cmd = pkg.getHeader().getCmd();
Command replyCmd = getReplyCmd(cmd);
String protocolType = "EventMeshMessage";
if (pkg.getHeader().getProperties() != null && pkg.getHeader().getProperty(Constants.PROTOCOL_TYPE) != null) {
protocolType = (String) pkg.getHeader().getProperty(Constants.PROTOCOL_TYPE);
}
ProtocolAdaptor<ProtocolTransportObject> protocolAdaptor = ProtocolPluginFactory.getProtocolAdaptor(protocolType);
Package msg = new Package();
// EventMeshMessage eventMeshMessage = (EventMeshMessage) pkg.getBody();
int retCode = 0;
EventMeshTcpSendResult sendStatus;
CloudEvent event = null;
try {
event = protocolAdaptor.toCloudEvent(pkg);
if (event == null) {
throw new Exception("event is null");
}
String content = new String(event.getData().toBytes(), StandardCharsets.UTF_8);
if (content.length() > eventMeshTCPServer.getEventMeshTCPConfiguration().eventMeshEventSize) {
throw new Exception("event size exceeds the limit: " + eventMeshTCPServer.getEventMeshTCPConfiguration().eventMeshEventSize);
}
// do acl check in sending msg
if (eventMeshTCPServer.getEventMeshTCPConfiguration().eventMeshServerSecurityEnable) {
String remoteAddr = RemotingHelper.parseChannelRemoteAddr(ctx.channel());
Acl.doAclCheckInTcpSend(remoteAddr, session.getClient(), event.getSubject(), cmd.value());
}
if (eventMeshTCPServer.getRateLimiter().tryAcquire(TRY_PERMIT_TIME_OUT, TimeUnit.MILLISECONDS)) {
synchronized (session) {
long sendTime = System.currentTimeMillis();
event = addTimestamp(event, cmd, sendTime);
sendStatus = session.upstreamMsg(pkg.getHeader(), event, createSendCallback(replyCmd, taskExecuteTime, event), startTime, taskExecuteTime);
if (StringUtils.equals(EventMeshTcpSendStatus.SUCCESS.name(), sendStatus.getSendStatus().name())) {
messageLogger.info("pkg|eventMesh2mq|cmd={}|Msg={}|user={}|wait={}ms|cost={}ms", cmd, event, session.getClient(), taskExecuteTime - startTime, sendTime - startTime);
} else {
throw new Exception(sendStatus.getDetail());
}
}
} else {
msg.setHeader(new Header(replyCmd, OPStatus.FAIL.getCode(), "Tps overload, global flow control", pkg.getHeader().getSeq()));
ctx.writeAndFlush(msg).addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
Utils.logSucceedMessageFlow(msg, session.getClient(), startTime, taskExecuteTime);
}
});
logger.warn("======Tps overload, global flow control, rate:{}! PLEASE CHECK!========", eventMeshTCPServer.getRateLimiter().getRate());
return;
}
} catch (Exception e) {
logger.error("MessageTransferTask failed|cmd={}|event={}|user={}|errMsg={}", cmd, event, session.getClient(), e);
if (!cmd.equals(RESPONSE_TO_SERVER)) {
msg.setHeader(new Header(replyCmd, OPStatus.FAIL.getCode(), e.getStackTrace().toString(), pkg.getHeader().getSeq()));
Utils.writeAndFlush(msg, startTime, taskExecuteTime, session.getContext(), session);
}
}
}
Aggregations