I am currently making my own REST API service but I stumbled into a problem. I have a method, which exports an excel file to user:
@Override
public File exportMappings(Long from, Pageable pageable) throws IOException {
File tempFile;
try (AbstractMarshaller marshaller = getMarshaller()) {
Page<Sale> sales = saleRepository.findByIdGreaterThan(from, pageable);
marshaller.marshallSheet(ExportSaleMappingContentResolverImpl.TAB_NAME, saleMappingContentResolver,
sales.iterator());
marshaller.finalizeWritings();
tempFile = marshaller.getFile();
}
return tempFile;
}
The problem is that if I want to export millions of records in my excel file, Page<Sale> sales = saleRepository.findByIdGreaterThan(from, pageable);
will be overloaded with elements and my service will crash because of OutOfMemoryError.
I understand, that I need to create my own Iterator which will be able to divide my query in pieces so page won't be overloaded. But I don't know how to do it.