
com.xxl.rpc.serialize.Serializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of xxl-rpc-core Show documentation
Show all versions of xxl-rpc-core Show documentation
A high performance, distributed RPC framework.
package com.xxl.rpc.serialize;
import com.xxl.rpc.serialize.impl.*;
import com.xxl.rpc.util.XxlRpcException;
/**
* serializer
*
* Tips:模板方法模式:定义一个操作中算法的骨架(或称为顶级逻辑),将一些步骤(或称为基本方法)的执行延迟到其子类中;
* Tips:基本方法:抽象方法 + 具体方法final + 钩子方法;
* Tips:Enum 时最好的单例方案;枚举单例会初始化全部实现,此处改为托管Class,避免无效的实例化;
*
* @author xuxueli 2015-10-30 21:02:55
*/
public abstract class Serializer {
public abstract byte[] serialize(T obj);
public abstract Object deserialize(byte[] bytes, Class clazz);
public enum SerializeEnum {
HESSIAN(HessianSerializer.class),
HESSIAN1(Hessian1Serializer.class),
PROTOSTUFF(ProtostuffSerializer.class),
KRYO(KryoSerializer.class),
JACKSON(JacksonSerializer.class);
private Class extends Serializer> serializerClass;
private SerializeEnum (Class extends Serializer> serializerClass) {
this.serializerClass = serializerClass;
}
public Serializer getSerializer() {
try {
return serializerClass.newInstance();
} catch (Exception e) {
throw new XxlRpcException(e);
}
}
public static SerializeEnum match(String name, SerializeEnum defaultSerializer){
for (SerializeEnum item : SerializeEnum.values()) {
if (item.name().equals(name)) {
return item;
}
}
return defaultSerializer;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy