2014-11-17 89 views
0

嗯..在某些情況下,我有一個對象需要對榛木實例進行操作。所以我impolemented的HazelcastInstanceAware接口,但這似乎並不用這個詞嵌套類...HazelcastInstanceAware不適用於嵌套對象

以下核心輸出「零」到控制檯:

公共類NullError實現Serializable,HazelcastInstanceAware { 私人短暫HazelcastInstance INSTANCE1 ; 私有瞬態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; 
    } 
} 

}

文檔沒有提及HazelcastInstanceAware僅被施加到根對象..任何想法?這是一個錯誤嗎?

回答

0

HazelcastInstanceAware只對需要反序列化的根對象起作用。我們不會爲實現此接口的實例查看對象圖。

+0

啊好的我明白了。我現在使用的解決方法是在持有當前實例的類中使用靜態變量(或者如果爲null,則創建一個新的變量) – KIC