Search code examples
javahazelcast

HazelcastInstanceAware does not work with nested Objects


Hmmm .. in some cases I have an Object which needs to do operations on a hazelcast instance. So I implemented the HazelcastInstanceAware interface but this seems not to work with nested classes ...

The following core outputs "null" to the console:

public class NullError implements Serializable, HazelcastInstanceAware { private transient HazelcastInstance instance1; private transient HazelcastInstance instance2;

public static void main(String[] args) throws Exception {
    //new NullError().test();
    new NullError().lala();
}

private void lala() throws Exception {
    instance1 = Hazelcast.newHazelcastInstance();
    instance2 = Hazelcast.newHazelcastInstance();

    IMap<Object, Object> foo = instance1.getMap("foo");
    foo.put("foo", new Foo(instance1));

    ((Callable) instance2.getMap("foo").get("foo")).call();
}

public static class Foo implements Serializable, Callable {
    public Bar bar;

    public Foo(HazelcastInstance instance) {
        bar = new Bar(instance);
    }

    @Override
    public Object call() throws Exception {
        return bar.call();
    }
}

public static class Bar implements Callable, Serializable, HazelcastInstanceAware {
    private transient HazelcastInstance i;

    public Bar(HazelcastInstance instance) {
        this.i = instance;
    }

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

    @Override
    public Object call() throws Exception {
        System.out.println(i);
        return null;
    }
}

}

The documentation does not mention HazelcastInstanceAware only be applied to the root object .. any Ideas? Is this a Bug?


Solution

  • The HazelcastInstanceAware only works on the root object to be deserialized. We don't look into an object graph for instances that are implementing this interface.