在 Hazelcast 中使用 ArrayList 的派生作为值时需要自定义序列化器
Need for custom serializers while using derivatives of ArrayList as value in Hazelcast
我有一个 IMAP,它的键是 String
,值是 ArrayList
的导数。我需要 运行 EntryProcessor
这张地图的一个键。另请注意,Employee
是一个实现了 Serializable
接口的 POJO。
当我执行下面给出的代码时,代码打印 "Why so !" 并且我得到 ClassCastException
其中提到 java.util.ArrayList
不能转换为 Employees
在 process()
下面给出的 ListValueEntryProcessor
方法。
Q1。我了解到我需要为我的类型 (Employees
) 添加自定义序列化程序,以便它可以序列化为 Employees
对象而不是 ArrayList
对象。我想知道为什么必须为内置类型添加 "custom serializer",例如 ArrayList
,其项目也标记为 Serializable
?
public class Employees extends ArrayList implements Serializable
{
private static final long serialVersionUID = 1L;
/**
Constructs a new employees object
*/
public Employees()
{
super();
}
}
HazelcastInstance hazelcastInstance = HazelcastHelper.getHazelcastInstance();
IMap<String, Employees> empMap = hazelcastInstance.getMap("employeesMap");
Employees empList = new Employees();
Employee employee = new Employee();
empList.add(employee);
empMap.put("companyId", employees);
empMap.executeOnKey("companyId", new IncSalaryEntryProcessor());
public static class ListValueEntryProcessor extends AbstractEntryProcessor<String, Employees>
{
private static final long serialVersionUID = 1L;
@Override
public Object process(Entry<String, Employees> arg0)
{
if(! (arg0.getValue() instanceof Employees))
{
System.out.println("Why so !");
}
// ClassCastException thrown here.
Employees empList = arg0.getValue();
return true;
}
}
这是我们这边的一个错误。我创建了一个错误报告:
https://github.com/hazelcast/hazelcast/issues/6455
以下代码应该可以暂时解决您的问题:
public class Main {
public static void main(String[] args){
HazelcastInstance hz = Hazelcast.newHazelcastInstance();
IMap<String,Employees> map = hz.getMap("foo");
map.put("1", new Employees());
Employees employees = map.get("1");
System.out.println(employees);
}
static class Employees extends ArrayList implements DataSerializable {
@Override
public void writeData(ObjectDataOutput out) throws IOException {
out.writeInt(size());
for(Object item: this){
out.writeObject(item);
}
}
@Override
public void readData(ObjectDataInput in) throws IOException {
int size = in.readInt();
for(int k=0;k<size;k++){
add(in.readObject());
}
}
}
}
我有一个 IMAP,它的键是 String
,值是 ArrayList
的导数。我需要 运行 EntryProcessor
这张地图的一个键。另请注意,Employee
是一个实现了 Serializable
接口的 POJO。
当我执行下面给出的代码时,代码打印 "Why so !" 并且我得到 ClassCastException
其中提到 java.util.ArrayList
不能转换为 Employees
在 process()
下面给出的 ListValueEntryProcessor
方法。
Q1。我了解到我需要为我的类型 (Employees
) 添加自定义序列化程序,以便它可以序列化为 Employees
对象而不是 ArrayList
对象。我想知道为什么必须为内置类型添加 "custom serializer",例如 ArrayList
,其项目也标记为 Serializable
?
public class Employees extends ArrayList implements Serializable
{
private static final long serialVersionUID = 1L;
/**
Constructs a new employees object
*/
public Employees()
{
super();
}
}
HazelcastInstance hazelcastInstance = HazelcastHelper.getHazelcastInstance();
IMap<String, Employees> empMap = hazelcastInstance.getMap("employeesMap");
Employees empList = new Employees();
Employee employee = new Employee();
empList.add(employee);
empMap.put("companyId", employees);
empMap.executeOnKey("companyId", new IncSalaryEntryProcessor());
public static class ListValueEntryProcessor extends AbstractEntryProcessor<String, Employees>
{
private static final long serialVersionUID = 1L;
@Override
public Object process(Entry<String, Employees> arg0)
{
if(! (arg0.getValue() instanceof Employees))
{
System.out.println("Why so !");
}
// ClassCastException thrown here.
Employees empList = arg0.getValue();
return true;
}
}
这是我们这边的一个错误。我创建了一个错误报告:
https://github.com/hazelcast/hazelcast/issues/6455
以下代码应该可以暂时解决您的问题:
public class Main {
public static void main(String[] args){
HazelcastInstance hz = Hazelcast.newHazelcastInstance();
IMap<String,Employees> map = hz.getMap("foo");
map.put("1", new Employees());
Employees employees = map.get("1");
System.out.println(employees);
}
static class Employees extends ArrayList implements DataSerializable {
@Override
public void writeData(ObjectDataOutput out) throws IOException {
out.writeInt(size());
for(Object item: this){
out.writeObject(item);
}
}
@Override
public void readData(ObjectDataInput in) throws IOException {
int size = in.readInt();
for(int k=0;k<size;k++){
add(in.readObject());
}
}
}
}