I would like to use the Java Streams API to create a pipeline and then terminate it with iterator()
, but I want it to "prepare X elements" asynchronously in advance. (Streams API not required, but preferred).
The situation I have is:
- Our application loads data from remote files (over network) to a database
- Opening a remote file (i.e. executing the pipeline synchronously for a single element) takes a non-trivial amount of time
- I cannot open too many files at once (results in connection timeout)
- The order in which the files are loaded matters
The Java Streams API can create the pipeline of commands to do, but to my knowledge, it cannot satisfy both requirements above. It can do either single execution:
files.stream().map(this::convertToInputStream).iterator()
which exacerbates the first requirement. Or it can do wholesale execution:
files.stream().map(this::convertToInputStream).collect(toList())
which fails the second.
I have implemented the requirements using a Deque<InputStream>
and Thread
logic to keep it populated up to a certain element count, but it is not pretty. I am asking if anyone knows of a way to create pipelines like so (perhaps using libraries) in a more elegant fashion. It seemed like a reasonable use case for the Streams API.