没有为命名空间注册DataSerializeFactory

时间:2015-02-19 17:07:28

标签: serialization deserialization hazelcast

我在HZ 3.4和3.4.1中尝试过但输出相同

我正在尝试将虚拟数据导入我的Hazelcast群集,具有以下功能

    HazelcastInstance cluster = HazelcastClient.newHazelcastClient(this.conf);
    Map<String, Customer> mapCustomers = cluster.getMap("customers");

    System.out.println(mapCustomers.size());
    System.out.println("hello world");


    for (int customerID = 0; customerID < 2000; customerID++) {
        Customer p = new Customer();
        mapCustomers.put(Integer.toString(customerID), p);
        System.out.println("inserted customer number " + Integer.toString(customerID));
    }
    cluster.shutdown();

当我第一次运行此代码时,没有问题,输出就像这样

0
hello world
inserted customer number 0
inserted customer number 1
inserted customer number 2
inserted customer number 3

问题是如果我已经尝试运行导入功能,那么每次我都有

4
hello world
Exception in thread "main"
com.hazelcast.nio.serialization.HazelcastSerializationException: No DataSerializerFactory registered for namespace: 1
at com.hazelcast.nio.serialization.DataSerializer.read(DataSerializer.java:98)
at com.hazelcast.nio.serialization.DataSerializer.read(DataSerializer.java:39)
at com.hazelcast.nio.serialization.StreamSerializerAdapter.toObject(StreamSerializerAdapter.java:65)
at com.hazelcast.nio.serialization.SerializationServiceImpl.toObject(SerializationServiceImpl.java:260)
at com.hazelcast.client.spi.ClientProxy.toObject(ClientProxy.java:173)
at com.hazelcast.client.spi.ClientProxy.invoke(ClientProxy.java:128)
at com.hazelcast.client.proxy.ClientMapProxy.put(ClientMapProxy.java:352)
at com.hazelcast.client.proxy.ClientMapProxy.put(ClientMapProxy.java:200)
at com.test.queries.Importer.importDummyData(Importer.java:42)
at run_import.main(run_import.java:9)

我正在使用DataSerializableFactory

public class SerializerFactory implements DataSerializableFactory {

public static final int FACTORY_ID = 1;

public static final int CUSTOMER_TYPE = 1;

public static final int ORDER_TYPE = 2;

@Override
public IdentifiedDataSerializable create(int typeId) {
    switch (typeId) {
        case CUSTOMER_TYPE:
            return  new Customer();
        case ORDER_TYPE:
            return new Order();
    }
    return null;
}

}

public class Customer implements IdentifiedDataSerializable {
 ....
@Override
public int getFactoryId() {
    return SerializerFactory.FACTORY_ID;
}

@Override
public int getId() {
    return SerializerFactory.CUSTOMER_TYPE;
}
....
}

和我的xml配置:

....
<serialization>
    <data-serializable-factories>
        <data-serializable-factory factory-id="1">
            SerializerFactory
        </data-serializable-factory>
    </data-serializable-factories>
    <portable-version>0</portable-version>
</serialization>
....

1 个答案:

答案 0 :(得分:4)

您必须在两端,服务器和客户端上配置DataSerializableFactory。这也意味着代码需要在两个位置都可用:)