0

Currently I have the following code:

    private Flux<String> tailFileManual(Path path) {
      final File file = path.toFile();

      return Flux.using(
            () -> new BufferedReader(new FileReader(file)),
            reader -> Flux.create(emitter -> {
                while (true) {
                    final String line = reader.readLine();
                    if (line == null) {
                        Thread.sleep(250);
                    } else if (line.equals("null")) {
                        emitter.complete();
                        break;
                    } else {
                        emitter.next(line);
                    }
                }
            }),
            BufferedReader::close
    );
}

Running with latest reactor-core (3.1.7), java complains that I have to wrap every thrown type (in this example IOException) with try-catch. Now this code runs as it in rx-java2 using Flowable.using, but for some reason Reactor is forcing me to manually wrap every possible thrown error in try-catch, making this code verbose... Is there something I'm missing?

I don't want to wrap FileReader, Thread.sleep and BufferedReader::close with try-catch, thus missing the whole point of short lambda expressions

Gilad Peleg
  • 2,010
  • 16
  • 29
  • `using` takes a `Consumer super D> resourceCleanup` which cannot throw an `Exception` - this is the same problem as any other exception handling in lambdas. – Boris the Spider May 07 '18 at 08:43
  • This exact same code (with `Flux` converted to `Flowable`) works in `rx-java2` as is. Any explanations why? – Gilad Peleg May 07 '18 at 09:23
  • It's not the same code, the [method](http://reactivex.io/RxJava/2.x/javadoc/io/reactivex/Flowable.html#using-java.util.concurrent.Callable-io.reactivex.functions.Function-io.reactivex.functions.Consumer-) takes a [`io.reactivex.functions.Consumer`](http://reactivex.io/RxJava/2.x/javadoc/io/reactivex/functions/Consumer.html) which `throws Exception`. – Boris the Spider May 07 '18 at 09:54

0 Answers0