繁体   English   中英

Java 8 CompletableFuture,流和超时

[英]Java 8 CompletableFuture , Stream and Timeouts

我正在尝试使用CompletableFutureStream同时处理一些数据,到目前为止,我有:

public static void main(String[] args) throws InterruptedException, ExecutionException {
    System.out.println("start");

    List<String> collect = Stream.of("1", "2", "3", "4", "5",
            "6", "7")
            .map(x -> CompletableFuture.supplyAsync(getStringSupplier(x)))
            .collect(Collectors.toList())
            .stream()
            .map(CompletableFuture::join)
            .collect(Collectors.toList());
    System.out.println("stop out!");
}


public static Supplier<String> getStringSupplier(String text) {
    return () -> {

        System.out.println("start " + text);
        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("stop " + text);
        return "asd" + text;
    };
}

输出很好:

start start 1 start 4 start 3 start 2 start 5 start 6 start 7 stop 4 stop 1 stop 5 stop 2 stop 6 stop 3 stop 7 stop out!

但是现在我想为该工作添加超时。 可以说应在1秒后将其取消。 并返回null或其他一些值来collect列表。 (我希望一些表示原因的值)。

我该如何实现?

预先感谢您的帮助。

我发现了这样做的方法:

 private static final ScheduledExecutorService scheduler =
        Executors.newScheduledThreadPool(
                1,
                new ThreadFactoryBuilder()
                        .setDaemon(true)
                        .setNameFormat("failAfter-%d")
                        .build());

public static void main(String[] args) throws InterruptedException, ExecutionException {
    System.out.println("start");
    final CompletableFuture<Object> oneSecondTimeout = failAfter(Duration.ofSeconds(1))
            .exceptionally(xxx -> "timeout exception");
    List<Object> collect = Stream.of("1", "2", "3", "4", "5", "6", "7")
            .map(x -> CompletableFuture.anyOf(createTaskSupplier(x)
                    , oneSecondTimeout))
            .collect(Collectors.toList())
            .stream()
            .map(CompletableFuture::join)
            .collect(Collectors.toList());
    System.out.println("stop out!");
    System.out.println(collect);
}

public static CompletableFuture<String> createTaskSupplier(String x) {
    return CompletableFuture.supplyAsync(getStringSupplier(x))
            .exceptionally(xx -> "PROCESSING ERROR : " + xx.getMessage());
}


public static Supplier<String> getStringSupplier(String text) {
    return () -> {

        System.out.println("start " + text);
        try {
            TimeUnit.MILLISECONDS.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if (text.equals("1")) {
            throw new RuntimeException("LOGIC ERROR");
        }
        try {
            if (text.equals("7"))
                TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("stop " + text);
        return "result " + text;
    };
}

public static <T> CompletableFuture<T> failAfter(Duration duration) {
    final CompletableFuture<T> promise = new CompletableFuture<>();
    scheduler.schedule(() -> {
        final TimeoutException ex = new TimeoutException("Timeout after " + duration);
        return promise.completeExceptionally(ex);
    }, duration.toMillis(), MILLISECONDS);
    return promise;
}

它返回:

 start
 start 1
 start 3
 start 4
 start 2
 start 5
 start 6
 start 7
 stop 6
 stop 4
 stop 3
 stop 5
 stop 2
 stop out!
 [PROCESSING ERROR : java.lang.RuntimeException: LOGIC ERROR, result 2, result 3, result 4, result 5, result 6, timeout exception]`

您对此有何看法,您能否发现该解决方案的任何缺陷?

对于不限于Java 8的其他人,可以使用Java 9中引入的completeOnTimeout方法。

List<String> collect = Stream.of("1", "2", "3", "4", "5", "6", "7")
        .map(x -> CompletableFuture.supplyAsync(getStringSupplier(x))
                .completeOnTimeout(null , 1, SECONDS))
        .filter(Objects::nonNull)
        .collect(toList())
        .stream()
        .map(CompletableFuture::join)
        .collect(toList());

您可以将作业包装在另一个CompletableFuture中,如果超过给定的时间,它将发出TimeoutException。 如果要特别处理,可以将TimeoutException catch块分开。

    List<String> collect = null;
    try {
        collect = CompletableFuture.supplyAsync(() ->
                Stream.of("1", "2", "3", "4", "5",
                        "6", "7")
                        .map(x -> CompletableFuture.supplyAsync(getStringSupplier(x)))
                        .collect(Collectors.toList())
                        .stream()
                        .map(CompletableFuture::join)
                        .collect(Collectors.toList())
        ).get(5, TimeUnit.SECONDS);
    } catch (InterruptedException | ExecutionException | TimeoutException e) {
        e.printStackTrace();
        //separate out the TimeoutException if you want to handle it differently 

    }

    System.out.println(collect); //would be null in case of any exception

您可以尝试使用具有执行程序参数(CompletableFuture.supplyAsync(getStringSupplier(x),timeoutExecutorService))的 CompletableFuture的重载supplyAsync方法,并可以引用timeoutExecutorService的链接

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM