1

我正在寻找在任务执行中查找本地 hazelcast 成员的方法。我正在使用的版本是 2.5-SNAPSHOT。教程中的片段似乎不起作用:

public class Echo implements Callable<String>, Serializable {
    public String call() {
        return Hazelcast.getCluster().getLocalMember().toString() + ":" + input;
    }
}

现在调用已弃用的 getCluster() 会触发创建和连接新成员。

目前,我设法通过 HazelcastInstance.getCluster().getLocalMember() 获得它,但我不想通过这种注入使我的可调用对象复杂化。

您能否建议查找本地成员的正确方法?

4

1 回答 1

5

可调用对象应实现 HazelcastInstanceAware。

这里的例子:

public class EchoExample {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Config config = new Config();
        HazelcastInstance instance1 = Hazelcast.newHazelcastInstance(config);
        HazelcastInstance instance2 = Hazelcast.newHazelcastInstance(config);
        ExecutorService executorService = instance1.getExecutorService("exec1");
        Future<String> future = executorService.submit(new Echo());
        System.out.println(future.get());

    }

    static class Echo implements HazelcastInstanceAware, Serializable, Callable<String> {
        transient HazelcastInstance localInstance;

        public String call() throws Exception {
            return localInstance.toString();
        }

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

}
于 2013-04-04T12:47:28.260 回答