ProtoWithChat.ChatMsgSend.Builder builder = ProtoWithChat.ChatMsgSend.newBuilder();
builder.setChannelType(ChatChannelType.World.getType());
builder.setMsgType(ChatMsgType.Text.getType());
ProtoWithChat.ChatBody.Builder bodyBuilder = ProtoWithChat.ChatBody.newBuilder();
bodyBuilder.setText("Hello");
builder.setBody(bodyBuilder.build());
try (Jedis jedis = litchi.redis().getJedis()) {
byte[] originBytes = builder.build().toByteArray();
jedis.lpush("hello_chat".getBytes(StandardCharsets.UTF_8), originBytes);
byte[] bytes = jedis.lpop("hello_chat".getBytes(StandardCharsets.UTF_8));
System.out.println(Arrays.equals(bytes, originBytes));
ProtoWithChat.ChatMsgSend obj =ProtoWithChat.ChatMsgSend.parseFrom(bytes);
System.out.println(JsonFormat.printToString(obj));
} catch (InvalidProtocolBufferException e) {
e.printStackTrace();
}
Java Proto buffers 结构在 redis中存取
最新推荐文章于 2023-04-29 00:15:00 发布