我正在使用最新的Hazelcast v3.8,但在MultiTask
包或任何其他包中找不到com.hazelcast.core
。
最新版本的Hazelcast中MultiTask
的替代品是什么?
我想控制我的任务运行的物理节点。我找到了Hazelcast distributed executor in use的一个很好的例子。
你可以看到很好的例子:
public void echoOnMembers(String input, Set<Member> members) throws Exception {
MultiTask<String> task = new MultiTask<String>(new Echo(input), members);
ExecutorService executorService = Hazelcast.getExecutorService();
executorService.execute(task);
Collection<String> results = task.get();
}
现在我想要相同的代码但是可以在v3.8上运行。请帮忙)
答案 0 :(得分:12)
MultiTask
,以支持更具表现力,更直接的ExecutorService
API。您可以使用以下IExecutorService
方法:
void executeOnMembers(Runnable command, Collection<Member> members);
void executeOnMembers(Runnable command, MemberSelector memberSelector);
void executeOnAllMembers(Runnable command);
<T> Map<Member, Future<T>> submitToMembers(Callable<T> task, Collection<Member> members);
<T> Map<Member, Future<T>> submitToMembers(Callable<T> task, MemberSelector memberSelector);
<T> Map<Member, Future<T>> submitToAllMembers(Callable<T> task);
还有上述方法的回调版本。有关详情,请参阅: http://docs.hazelcast.org/docs/latest/javadoc/com/hazelcast/core/IExecutorService.html
echoOnMembers(...)
示例的替代品是:
public void echoOnMembers( String input, Set<Member> members ) throws Exception {
HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance();
IExecutorService executorService =
hazelcastInstance.getExecutorService( "default" );
Map<Member, Future<String>> futures = executorService
.submitToMembers( new Echo( input ), members );
for ( Future<String> future : futures.values() ) {
String echoResult = future.get();
// ...
}
}
有关详细信息,请参阅Hazelcast文档和代码示例: http://docs.hazelcast.org/docs/latest/manual/html-single/index.html#executing-code-in-the-cluster
https://github.com/hazelcast/hazelcast-code-samples/tree/master/distributed-executor