同步获取响应数据

This commit is contained in:
Guoqs
2024-07-31 17:14:41 +08:00
parent eed9a7bc2c
commit 4346e9fc1f
6 changed files with 25 additions and 16 deletions

View File

@@ -1,5 +1,6 @@
package rpc;
import com.jsowell.common.util.bean.SerializationUtil;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

View File

@@ -1,5 +1,6 @@
package rpc;
import com.jsowell.common.util.bean.SerializationUtil;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

View File

@@ -1,53 +0,0 @@
package rpc;
import io.protostuff.LinkedBuffer;
import io.protostuff.ProtostuffIOUtil;
import io.protostuff.Schema;
import io.protostuff.runtime.RuntimeSchema;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class SerializationUtil {
private final static Map<Class<?>, Schema<?>> schemaCache = new ConcurrentHashMap<>();
/**
* 序列化
*/
public static <T> byte[] serialize(T object){
LinkedBuffer buffer = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE);
try {
Class<T> cls = (Class<T>) object.getClass();
Schema<T> schema = getSchema(cls);
return ProtostuffIOUtil.toByteArray(object, schema, buffer);
} catch (Exception e) {
throw e;
} finally {
buffer.clear();
}
}
/**
* 反序列化
*/
public static <T> T deserialize(Class<T> cls, byte[] data) {
Schema<T> schema = getSchema(cls);
T message = schema.newMessage();
ProtostuffIOUtil.mergeFrom(data, message, schema);
return message;
}
public static <T> Schema<T> getSchema(Class<T> cls) {
Schema<T> schema = (Schema<T>) schemaCache.get(cls);
if(schema == null) {
schema = RuntimeSchema.getSchema(cls);
schemaCache.put(cls, schema);
}
return schema;
}
}