[关闭]
@aloxc 2017-12-05T06:37:55.000000Z 字数 1550 阅读 346

ignite的异步计算结果bug

ignite

直接看代码吧

  1. try (Ignite ignite = Ignition.start("fnew/computer.xml")) {
  2. Collection<IgniteCallable<Integer>> calls = new ArrayList<>();
  3. // Iterate through all the words in the sentence and create Callable jobs.
  4. logger.error(">>>开始计算字符串长度,可不用启动ignite服务器");
  5. final String parString = "Count characters using callable";
  6. String[] srcs = parString.split(" ");
  7. for (final String word : srcs) {
  8. calls.add(new IgniteCallable<Integer>() {
  9. @Override
  10. public Integer call() throws Exception {
  11. logger.error("服务器日志 = " + word);
  12. return word.length();
  13. }
  14. });
  15. }
  16. // Execute collection of Callables on the grid.
  17. final Collection<Integer> res = ignite.compute().call(calls);
  18. int sum = 0;
  19. // Add up individual word lengths received from remote nodes.
  20. for (int len : res)
  21. sum += len;
  22. logger.error(">>> 字符串[" + parString + "]中非空白字符数量为'" + sum + "'.");
  23. IgniteCompute compute = ignite.compute().withAsync();//异步调用
  24. compute.call(calls);
  25. final CountDownLatch latch = new CountDownLatch(1);
  26. IgniteFuture<Integer> comFuture = compute.future();
  27. comFuture.listen(new IgniteInClosure<IgniteFuture<Integer>>(){
  28. @Override
  29. public void apply(IgniteFuture<Integer> future) {
  30. logger.error("异步计算结果 = " + future.get() );
  31. latch.countDown();
  32. }
  33. });
  34. latch.await();
  35. logger.error("程序运行完毕");
  36. }

这段代码能运行,并且也有结果,把后面的监听代码调整如下

  1. final CountDownLatch latch = new CountDownLatch(1);
  2. IgniteFuture<Integer> comFuture = compute.future();
  3. comFuture.listen(new IgniteInClosure<IgniteFuture<Integer>>() {
  4. @Override
  5. public void apply(IgniteFuture<Integer> future) {
  6. logger.error("返回结果类型" + future.get().getClass());
  7. logger.error("异步计算结果 = " + future.get() );
  8. latch.countDown();
  9. }
  10. });
  11. latch.await();

future.get().getClass()这行代码就会异常,提示不能把ArrayList转换为Integer。
可以可以把听定代码中的泛型去掉也可以正常执行的

x

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注