JID 是一个用来反序列化、更新和重新序列化 Map 对象,在 i5 2.53GHz 处理器的机器上,完成 10000 条目的操作仅需 4 毫秒。序列化和重新序列化的时间很大程度上依赖于表条目的大小和复杂度,增量式的序列化和反序列化用于达到高性能。
今天我试验了下使用 JID 进行 Java 对象的序列化过程,现将这个过程与大家分享,至于在何种场景下使用大家自己发挥。
首先 JID 依赖于 JActor 和 SLF4J ,你下载的 JID 压缩包中就包含这二者的 jar 文件。
大家先不需要关心代码中牵扯的一些类是作何用途的,先把例子跑起来!
我们这里假设要对一个 User 对象进行序列化,User 对象如下:
-
-
-
-
- public static class User extends AppJid {
- private StringJid getNameJid() throws Exception {
- return (StringJid) _iGet(0);
- }
-
- private IntegerJid getAgeJid() throws Exception {
- return (IntegerJid) _iGet(1);
- }
-
- public String getName() throws Exception {
- return getNameJid().getValue();
- }
-
- public void setName(String name) throws Exception {
- getNameJid().setValue(name);
- }
-
- public int getAge() throws Exception {
- return getAgeJid().getValue();
- }
-
- public void setAge(int age) throws Exception {
- getAgeJid().setValue(age);
- }
- }
|
然后每个要做序列化的对象都需要有个对应的工厂类,这里是 UserFactory:
- public static class UserFactory extends AppJidFactory {
- final public static UserFactory fac = new UserFactory();
-
- public UserFactory() {
- super("User", JidFactories.STRING_JID_TYPE, JidFactories.INTEGER_JID_TYPE);
- }
-
- protected User instantiateActor() throws Exception {
- return new User();
- }
- }
|
其中特别注意 UserFactory 构造函数里的 "User" 这个参数,下面需要用到。