I have create a Class OrderFetcher that implements Callable interface returns a Integer from Call Method as below
public class OrderFetcher implements Callable<Integer> {
@Override
public Integer call() throws Exception {
sleep(200);
Random random = new Random();
return random.nextInt(10);
}
}
I have also created another two classes InventoryFetcher & PaymentFetcher and these also implements callable interface
public class InventoryFetcher implements Callable {
public InventoryFetcher(Integer orderId) {
this.orderId = orderId;
}
Integer orderId;
@Override
public Boolean call() throws Exception {
sleep(200);
return this.orderId%2==0?true:false;
}
}
public class PaymentFetcher implements Callable {
Boolean flag;
public PaymentFetcher(Boolean flag) {
this.flag = flag;
}
@Override
public String call() throws Exception {
sleep(200);
return flag?"Payment Accepted":"Payment Rejected";
}
}
I tried them with Futures and it works well where i am calling the OrderFetcher to return a value and pass that in InventoryFethcer and the pass the output from InventoryFetcher to PaymnetFethcer class. Ones output is Input to other and so on.
But how can i acheve this in CompletableFuture? It asking for a supplier interface implementation