use of io.seata.core.protocol.AbstractMessage in project seata by seata.
the class MergeResultMessageConvertor method convert2Proto.
@Override
public MergedResultMessageProto convert2Proto(MergeResultMessage mergeResultMessage) {
final short typeCode = mergeResultMessage.getTypeCode();
final AbstractMessageProto abstractMessage = AbstractMessageProto.newBuilder().setMessageType(MessageTypeProto.forNumber(typeCode)).build();
List<Any> lists = new ArrayList<>();
for (AbstractMessage msg : mergeResultMessage.msgs) {
final PbConvertor pbConvertor = ProtobufConvertManager.getInstance().fetchConvertor(msg.getClass().getName());
lists.add(Any.pack((Message) pbConvertor.convert2Proto(msg)));
}
MergedResultMessageProto mergedWarpMessageProto = MergedResultMessageProto.newBuilder().setAbstractMessage(abstractMessage).addAllMsgs(lists).build();
return mergedWarpMessageProto;
}
use of io.seata.core.protocol.AbstractMessage in project seata by seata.
the class MergedWarpMessageCodec method encode.
@Override
public <T> void encode(T t, ByteBuf out) {
MergedWarpMessage mergedWarpMessage = (MergedWarpMessage) t;
List<AbstractMessage> msgs = mergedWarpMessage.msgs;
final ByteBuf buffer = Unpooled.buffer(1024);
// write placeholder for content length
buffer.writeInt(0);
buffer.writeShort((short) msgs.size());
for (final AbstractMessage msg : msgs) {
final ByteBuf subBuffer = Unpooled.buffer(1024);
short typeCode = msg.getTypeCode();
MessageSeataCodec messageCodec = MessageCodecFactory.getMessageCodec(typeCode);
messageCodec.encode(msg, subBuffer);
buffer.writeShort(msg.getTypeCode());
buffer.writeBytes(subBuffer);
}
final int length = buffer.readableBytes();
final byte[] content = new byte[length];
// minus the placeholder length itself
buffer.setInt(0, length - 4);
buffer.readBytes(content);
if (msgs.size() > 20) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("msg in one packet:" + msgs.size() + ",buffer size:" + content.length);
}
}
out.writeBytes(content);
}
use of io.seata.core.protocol.AbstractMessage 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);
}
}
}
use of io.seata.core.protocol.AbstractMessage in project seata by seata.
the class MergeResultMessageCodec method decode.
/**
* Decode.
*
* @param mergeResultMessage the merge result message
* @param byteBuffer the byte buffer
*/
protected void decode(MergeResultMessage mergeResultMessage, ByteBuffer byteBuffer) {
// msgs size
short msgNum = byteBuffer.getShort();
AbstractResultMessage[] msgs = new AbstractResultMessage[msgNum];
for (int idx = 0; idx < msgNum; idx++) {
short typeCode = byteBuffer.getShort();
AbstractMessage abstractResultMessage = MessageCodecFactory.getMessage(typeCode);
MessageSeataCodec messageCodec = MessageCodecFactory.getMessageCodec(typeCode);
messageCodec.decode(abstractResultMessage, byteBuffer);
msgs[idx] = (AbstractResultMessage) abstractResultMessage;
}
mergeResultMessage.setMsgs(msgs);
}
use of io.seata.core.protocol.AbstractMessage in project seata by seata.
the class MergeMessageConvertorTest method test.
@Test
public void test() {
MergedWarpMessage mergedWarpMessage = new MergedWarpMessage();
final ArrayList<AbstractMessage> msgs = new ArrayList<>();
final GlobalBeginRequest globalBeginRequest = buildGlobalBeginRequest();
msgs.add(globalBeginRequest);
mergedWarpMessage.msgs = msgs;
MergedWarpMessageConvertor pbConvertor = new MergedWarpMessageConvertor();
MergedWarpMessageProto globalBeginRequestProto = pbConvertor.convert2Proto(mergedWarpMessage);
MergedWarpMessage model = pbConvertor.convert2Model(globalBeginRequestProto);
GlobalBeginRequest decodeModel = (GlobalBeginRequest) model.msgs.get(0);
assertThat(decodeModel.getTransactionName()).isEqualTo(globalBeginRequest.getTransactionName());
assertThat(decodeModel.getTimeout()).isEqualTo(globalBeginRequest.getTimeout());
assertThat(decodeModel.getTypeCode()).isEqualTo(globalBeginRequest.getTypeCode());
}
Aggregations