简体   繁体   English

使用compose创建CompletableFutures的链接列表

[英]Create linked list of CompletableFutures with compose

I want to create a chain of CompletableFutures. 我想创建一个CompletableFutures链。

I'm trying to build things up as follows. 我正在尝试建立如下的东西。

Task 1 performs something and returns a String as result when Task 1 is done I want to start Task 2 with as input the result of Task 1. Task 2 when this is ready it returns an Integer and so on... 任务1完成后,任务1执行一些操作并返回字符串作为结果。我想以任务1的结果作为输入启动任务2。任务2准备就绪后,它返回一个整数,依此类推...

So it should be very dynamical so I already have this: 所以它应该非常动态,所以我已经有了:

      try {
            CompletableFuture<String> task1Future = CompletableFuture.supplyAsync(new Task1());
            CompletableFuture<String> result = task1Future.thenCompose(task1Result -> CompletableFuture.supplyAsync(new Task2(task1Result)));
            System.out.println(result.get());
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }


        public class Task1 implements Supplier<String> {

    public Task1() {
        System.out.println("Task 1 started");
    }

    @Override
    public String get() {
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            throw new IllegalStateException(e);
        }
        return "Result Task 1";
    }
}

What I know try to achieve is build a Wrapper (a sort of linked list): 我知道尝试实现的是构建包装器(一种链接列表):

List of Task where Task should be: 任务列表,其中任务应为:

public class Task {
    private Supplier startTask;
    private Task followUpTask;

    public Task(Supplier startTask, Task followUpTask) {
        this.startTask = startTask;
        this.followUpTask = followUpTask;
    }
}

But I'm now stuck because I don't know how to do the chaining and to make Task more generic that it can be initiated with whatever is the result of the previous task. 但是我现在陷入了困境,因为我不知道如何进行链接以及如何使Task更通用,以至于无论上一个任务的结果如何,它都可以启动它。

So I need to have a method to contruct the CompletableFuture and to just say start() and that everything happens. 所以我需要一种方法来构造CompletableFuture并只说start(),一切都会发生。

Can someone help me on the way? 有人可以帮我吗?

You should define Task2 as a Function, since it accepts previous String result and produces new Integer result: 您应该将Task2定义为函数,因为它接受先前的String结果并产生新的Integer结果:

public static class Task2 implements Function<String, Integer> {

    public Task2() {
        System.out.println("Task 2 started");
    }

    @Override
    public Integer apply(String s) {
        return s.length();
    }
}

then you can chain them as follows: 那么您可以按如下所示链接它们:

   CompletableFuture<String> task1Future = CompletableFuture.supplyAsync(new Task1());
   CompletableFuture<Integer> result = task1Future.thenApply(new Task2());
   System.out.println(result.get());

Just in case you want all your tasks to implement Function, you can start the chain as follows: 万一您希望所有任务都实现功能,可以按以下方式启动链:

CompletableFuture<String> task1Future = CompletableFuture.completedFuture("S")
    .thenApply(new Task1());

where completedFuture("S") holds the argument for the first task: 其中completedFuture("S")保存第一个任务的参数:

public static class Task1 implements Function<String, String>  {

    public Task1() {
        System.out.println("Task 1 started");
    }

    @Override
    public String apply(String s) {
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            throw new IllegalStateException(e);
        }
        return "Result Task 1";
    }
}

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

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