Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Export big json file
private ObjectMapper jsonMapper = new ObjectMapper();
private ExecutorService executorService = Executors.newFixedThreadPool(5);
public ListenableFuture<Boolean> export(UUID customerId) {
try (PipedInputStream in = new PipedInputStream();
PipedOutputStream pipedOut = new PipedOutputStream(in);
GZIPOutputStream out = new GZIPOutputStream(pipedOut)) {
Stopwatch stopwatch = Stopwatch.createStarted();
ObjectWriter writer = jsonMapper.writer().withDefaultPrettyPrinter();
try(SequenceWriter sequenceWriter = writer.writeValues(out)) {
Future<?> storageFuture = executorService.submit(() ->
storageProvider.storeFile(getFilePath(customerId), in));
int batchCounter = 0;
while (true) {
List<Record> batch = readDatabaseBatch(batchCounter++);
for (Record record : batch) {
if (batch.isEmpty()) {
// if there are no more batches, stop.
// wait for storing to complete
// send the customer a notification and a download link
}"Exporting took {} seconds", stopwatch.stop().elapsed(TimeUnit.SECONDS));
return AsyncResult.forValue(true);
} catch (Exception ex) {
logger.error("Failed to export data", ex);
return AsyncResult.forValue(false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.