@aloxc
2017-12-05T06:37:55.000000Z
字数 1550
阅读 346
ignite
try (Ignite ignite = Ignition.start("fnew/computer.xml")) {
Collection<IgniteCallable<Integer>> calls = new ArrayList<>();
// Iterate through all the words in the sentence and create Callable jobs.
logger.error(">>>开始计算字符串长度,可不用启动ignite服务器");
final String parString = "Count characters using callable";
String[] srcs = parString.split(" ");
for (final String word : srcs) {
calls.add(new IgniteCallable<Integer>() {
@Override
public Integer call() throws Exception {
logger.error("服务器日志 = " + word);
return word.length();
}
});
}
// Execute collection of Callables on the grid.
final Collection<Integer> res = ignite.compute().call(calls);
int sum = 0;
// Add up individual word lengths received from remote nodes.
for (int len : res)
sum += len;
logger.error(">>> 字符串[" + parString + "]中非空白字符数量为'" + sum + "'.");
IgniteCompute compute = ignite.compute().withAsync();//异步调用
compute.call(calls);
final CountDownLatch latch = new CountDownLatch(1);
IgniteFuture<Integer> comFuture = compute.future();
comFuture.listen(new IgniteInClosure<IgniteFuture<Integer>>(){
@Override
public void apply(IgniteFuture<Integer> future) {
logger.error("异步计算结果 = " + future.get() );
latch.countDown();
}
});
latch.await();
logger.error("程序运行完毕");
}
这段代码能运行,并且也有结果,把后面的监听代码调整如下
final CountDownLatch latch = new CountDownLatch(1);
IgniteFuture<Integer> comFuture = compute.future();
comFuture.listen(new IgniteInClosure<IgniteFuture<Integer>>() {
@Override
public void apply(IgniteFuture<Integer> future) {
logger.error("返回结果类型" + future.get().getClass());
logger.error("异步计算结果 = " + future.get() );
latch.countDown();
}
});
latch.await();
future.get().getClass()这行代码就会异常,提示不能把ArrayList转换为Integer。
可以可以把听定代码中的泛型去掉也可以正常执行的