gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 03:52:06 25 4
gpt4 key购买 nike

我遇到以下异常:

Could not find PortableFactory for factory-id: 1 com.hazelcast.nio.serialization.HazelcastSerializationException: Could not find PortableFactory for factory-id: 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");
}
}
}

最佳答案

是的,就像您确定工厂和类需要可用一样。目前没有内置解决方案可以不为比简单的获取/放置更复杂的用例共享类。我有 JSON 支持和一些其他 cooking 想法,但还没有真正完成。

关于serialization - Hazelcast:可移植序列化是否需要在客户端和服务器之间共享对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29909787/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com