Hazelcast:可移植序列化是否需要在客户端和服务器之间共享对象?

时间:2015-04-28 03:30:36

标签: serialization client-server hazelcast

我收到以下异常:

  

找不到工厂ID的PortableFactory:1   com.hazelcast.nio.serialization.HazelcastSerializationException:可以   找不到工厂ID的PortableFactory:1

在客户端,我有以下代码:

public class ClientTest {

    public static void main(String[] args) {

        List<String> nodes = new ArrayList<String>();
        nodes.add("localhost:5701");

        ClientConfig clientConfig = new ClientConfig();
        ClientNetworkConfig networkConfig = new ClientNetworkConfig();
        networkConfig.setAddresses(nodes);
        clientConfig.setNetworkConfig(networkConfig);

        SerializationConfig serCong = clientConfig.getSerializationConfig();
        serCong.addPortableFactory(1, new UserFactoryImpl());
        serCong.setPortableVersion(1);

        HazelcastInstance hzClient1 = HazelcastClient.newHazelcastClient(clientConfig);

        IMap<String, User> map = hzClient1.getMap("user");
        System.out.println(map.size() + "hiten");
        User user1 = new User();
        user1.setFirstName("hiten");
        user1.setLastName("singh");
        map.put("1", user1);


        //hz1.getLifecycleService().terminate();
        System.out.println(map.size() + "after");

        User user2 = new User();
        user2.setFirstName("hiten1");
        user2.setLastName("singh1");
        map.put("2", user2);

        UserEntryProcessor entryProc = new UserEntryProcessor();
        User userRes = (User)map.executeOnKey("1", entryProc);
    }

    static class UserEntryProcessor implements EntryProcessor<String, User>, HazelcastInstanceAware {

        private transient HazelcastInstance hazelcastInstance;

        @Override
        public Object process(Entry<String, User> entry) {
            User user = entry.getValue();
            if(user != null) {
                System.out.println(user.getFirstName());
            }

            return user;
        }

        @Override
        public EntryBackupProcessor<String, User> getBackupProcessor() {
            return null;
        }

        @Override
        public void setHazelcastInstance(HazelcastInstance hazelcastInstance) {
            this.hazelcastInstance = hazelcastInstance;
        }

    }

    static class UserFactoryImpl implements PortableFactory{

        public final static int USER_PORTABLE_ID = 1;

        public final static int FACTORY_ID = 1;

        public Portable create(int classId) {
            switch (classId) {
                case USER_PORTABLE_ID:
                    return new User();
            }
            return null;
        }
    }

    static class User implements Portable {

        private String firstName;
        private String lastName;

        public String getLastName() {
            return lastName;
        }

        public void setLastName(String lastName) {
            this.lastName = lastName;
        }

        public String getFirstName() {
            return firstName;
        }

        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }

        @Override
        public int getFactoryId() {
            return UserFactoryImpl.FACTORY_ID;
        }

        @Override
        public int getClassId() {
            return UserFactoryImpl.USER_PORTABLE_ID;
        }

        @Override
        public void writePortable(PortableWriter writer) throws IOException {
            writer.writeUTF("first_name", firstName);
            writer.writeUTF("last_name", lastName);
        }

        @Override
        public void readPortable(PortableReader reader) throws IOException {
            firstName = reader.readUTF("first_name");
            lastName = reader.readUTF("last_name");
        }
    }
}

1 个答案:

答案 0 :(得分:3)

是的,就像你弄清楚工厂一样,并且需要提供课程。目前没有内置的解决方案可以不比简单的获取/放置更复杂的用例共享类。我有JSON支持和其他一些想法,但还没有真正完成。