use of io.seata.core.protocol.AbstractResultMessage in project seata by seata.
the class ClientOnResponseProcessor method process.
@Override
public void process(ChannelHandlerContext ctx, RpcMessage rpcMessage) throws Exception {
if (rpcMessage.getBody() instanceof MergeResultMessage) {
MergeResultMessage results = (MergeResultMessage) rpcMessage.getBody();
MergedWarpMessage mergeMessage = (MergedWarpMessage) mergeMsgMap.remove(rpcMessage.getId());
for (int i = 0; i < mergeMessage.msgs.size(); i++) {
int msgId = mergeMessage.msgIds.get(i);
MessageFuture future = futures.remove(msgId);
if (future == null) {
if (LOGGER.isInfoEnabled()) {
LOGGER.info("msg: {} is not found in futures.", msgId);
}
} else {
future.setResultMessage(results.getMsgs()[i]);
}
}
} else {
MessageFuture messageFuture = futures.remove(rpcMessage.getId());
if (messageFuture != null) {
messageFuture.setResultMessage(rpcMessage.getBody());
} else {
if (rpcMessage.getBody() instanceof AbstractResultMessage) {
if (transactionMessageHandler != null) {
transactionMessageHandler.onResponse((AbstractResultMessage) rpcMessage.getBody(), null);
}
}
}
}
}
use of io.seata.core.protocol.AbstractResultMessage in project seata by seata.
the class MergeResultMessageConvertorTest method convert2Proto.
@Test
public void convert2Proto() {
MergeResultMessage mergeResultMessage = new MergeResultMessage();
AbstractResultMessage[] msgs = new AbstractResultMessage[1];
final GlobalCommitResponse globalCommitResponse = new GlobalCommitResponse();
globalCommitResponse.setGlobalStatus(GlobalStatus.AsyncCommitting);
globalCommitResponse.setMsg("msg");
globalCommitResponse.setResultCode(ResultCode.Failed);
globalCommitResponse.setTransactionExceptionCode(TransactionExceptionCode.BranchRegisterFailed);
msgs[0] = globalCommitResponse;
mergeResultMessage.setMsgs(msgs);
MergeResultMessageConvertor convertor = new MergeResultMessageConvertor();
MergedResultMessageProto proto = convertor.convert2Proto(mergeResultMessage);
MergeResultMessage real = convertor.convert2Model(proto);
GlobalCommitResponse realObj = (GlobalCommitResponse) real.getMsgs()[0];
assertThat((realObj.getTypeCode())).isEqualTo(globalCommitResponse.getTypeCode());
assertThat((realObj.getMsg())).isEqualTo(globalCommitResponse.getMsg());
assertThat((realObj.getResultCode())).isEqualTo(globalCommitResponse.getResultCode());
assertThat((realObj.getTransactionExceptionCode())).isEqualTo(globalCommitResponse.getTransactionExceptionCode());
}
use of io.seata.core.protocol.AbstractResultMessage in project seata by seata.
the class AbstractResultMessageCodec method encode.
@Override
public <T> void encode(T t, ByteBuf out) {
AbstractResultMessage abstractResultMessage = (AbstractResultMessage) t;
ResultCode resultCode = abstractResultMessage.getResultCode();
String resultMsg = abstractResultMessage.getMsg();
out.writeByte(resultCode.ordinal());
if (resultCode == ResultCode.Failed) {
if (StringUtils.isNotEmpty(resultMsg)) {
String msg;
if (resultMsg.length() > Short.MAX_VALUE) {
msg = resultMsg.substring(0, Short.MAX_VALUE);
} else {
msg = resultMsg;
}
byte[] bs = msg.getBytes(UTF8);
out.writeShort((short) bs.length);
out.writeBytes(bs);
} else {
out.writeShort((short) 0);
}
}
}
use of io.seata.core.protocol.AbstractResultMessage in project seata by seata.
the class MergeResultMessageConvertor method convert2Model.
@Override
public MergeResultMessage convert2Model(MergedResultMessageProto mergedResultMessageProto) {
MergeResultMessage result = new MergeResultMessage();
List<Any> anys = mergedResultMessageProto.getMsgsList();
List<AbstractResultMessage> temp = new ArrayList<>();
for (Any any : anys) {
final Class clazz = ProtobufConvertManager.getInstance().fetchProtoClass(getTypeNameFromTypeUrl(any.getTypeUrl()));
if (any.is(clazz)) {
try {
Object ob = any.unpack(clazz);
final PbConvertor pbConvertor = ProtobufConvertManager.getInstance().fetchReversedConvertor(clazz.getName());
Object model = pbConvertor.convert2Model(ob);
temp.add((AbstractResultMessage) model);
} catch (InvalidProtocolBufferException e) {
throw new ShouldNeverHappenException(e);
}
}
}
result.setMsgs(temp.toArray(new AbstractResultMessage[temp.size()]));
return result;
}
use of io.seata.core.protocol.AbstractResultMessage in project seata by seata.
the class MergeResultMessageCodec method encode.
@Override
public <T> void encode(T t, ByteBuf out) {
MergeResultMessage mergeResultMessage = (MergeResultMessage) t;
AbstractResultMessage[] msgs = mergeResultMessage.getMsgs();
int writeIndex = out.writerIndex();
out.writeInt(0);
out.writeShort((short) msgs.length);
for (AbstractMessage msg : msgs) {
// get messageCodec
short typeCode = msg.getTypeCode();
// put typeCode
out.writeShort(typeCode);
MessageSeataCodec messageCodec = MessageCodecFactory.getMessageCodec(typeCode);
messageCodec.encode(msg, out);
}
int length = out.readableBytes() - 4;
out.setInt(writeIndex, length);
if (msgs.length > 20) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("msg in one services merge packet:" + msgs.length + ",buffer size:" + length);
}
}
}
Aggregations