October 2018
Intermediate to advanced
556 pages
15h 18m
English
The using factory method allows the creation of a stream depending on a disposable resource. It implements the try-with-resources approach in reactive programming. Let's assume that it is a requirement to wrap a blocking API represented with the following, intentionally simplified, Connection class:
public class Connection implements AutoCloseable { // (1) private final Random rnd = new Random(); public Iterable<String> getData() { // (2) if (rnd.nextInt(10) < 3) { // (2.1) throw new RuntimeException("Communication error"); } return Arrays.asList("Some", "data"); // (2.2) } public void close() { // (3) log.info("IO Connection closed"); } public static Connection newConnection() { // (4) ...