Reputation: 427
Hi i am using apache POI and for writing into workbook i am using SXSSFWorkbook since data will be huge,
Eveything looks good, but while converting workbook to inputstream it's taking huge time for exporting reports.
Here is my code
public StreamedContent generateStreamRep(String fileName, Workbook wb) {
try (ByteArrayOutputStream dsf = new ByteArrayOutputStream();){
wb.write(dsf);
file = new DefaultStreamedContent(ByteSource.wrap(dsf.toByteArray()).openStream(), "xlsx", fileName);
}
I was using IOUtils now switched to com.google.common.io.ByteSource
Looked into PipedStreams but not getting proper resource.
Upvotes: 1
Views: 564
Reputation: 2605
A Minimal Reproducible Example would help to provide a more complete answer.
However the following code uses PipedStreams to accelerate the operations shown in the code of the question. However the benefits you get from this approach cannot be larger than the fastest of the 2 parallel processes. Or, stated differently, the final duration cannot be faster than the slower of the parallel operations. In a PipedStreams approach you need 2 Streams. A PipedOutputStream where data will be written and a PipedInputStream were data will be consumed. To benefit from this approach you need to run these two operations in parallel.
public StreamedContent generateStreamRep(final String fileName, final Workbook wb) {
try (final PipedOutputStream dsf = new PipedOutputStream ();
final PipedInputStream sink=new PipedInputStream (dsf);){
final ExecutorService executorService= Executors.newSingleThreadExecutor();
//Write to output stream
executorService.execute(()->wb.write(dsf));
//read from input stream
file = new DefaultStreamedContent(sink, "xlsx", fileName);
executorService.shutdown();
//wait until task is finished or until a maximum time, which ever comes first. Normally in this case the task is already finished
executorService.awaitTermination(2,TimeUnit.Minutes);
}
Upvotes: 1