我正在使用Spring ListenableFuture执行异步任务。我正在尝试执行以下操作。
但是由于某些原因,它不能按预期工作。
我做错什么了吗
public void run(String... args) throws Exception {
List<MyEntity> entities = Arrays.asList(
new MyEntity("3", "val1"),
new MyEntity("2", "val1"),
new MyEntity(null, "val1"),
new MyEntity("21", "val1")
);
partition(entities, 3).forEach(p -> {
List<ListenableFuture<MyEntity>> futures = new ArrayList<>();
final CountDownLatch countDownLatch = new CountDownLatch(1);
entities.stream().forEach(s -> {
ListenableFuture<MyEntity> future = asyncCassandraOperations.insert(s);
future.addCallback(it -> {
countDownLatch.countDown();
System.out.println("Pass" + it);
}, throwable -> {
countDownLatch.countDown();
System.out.println("fail");
});
futures.add(future);
});
futures.forEach(f -> {
try {
f.get();
} catch (Exception e) {
throw new RuntimeException(e);
}
});
try {
countDownLatch.await();
} catch (Exception e) {
throw new RuntimeException(e);
}
System.out.println("done patch");
});
System.out.println("done all");
}