use of org.folio.rest.resource.handlers.FileDataHandler in project raml-module-builder by folio-org.
the class ProcessUploads method parseFile.
/**
* Main work done by the FileDataHandler which reads in line by line and passes on that line
* to the correct Importer implementation for line processing
* @param fileSize
* @param conf
* @param replyHandler - the handler returns a job object with success and error counter parameters
* to be persisted by the job runner
*/
private void parseFile(long fileSize, Job conf, Handler<AsyncResult<Job>> replyHandler) {
String file = conf.getParameters().get(0).getValue();
vertx.fileSystem().open(file, new OpenOptions(), ar -> {
if (ar.succeeded()) {
AsyncFile rs = ar.result();
rs.handler(new FileDataHandler(vertx, conf, fileSize, importerCache.get(conf.getName()), reply -> {
if (reply.failed()) {
if (reply.cause().getMessage().contains(RTFConsts.STATUS_ERROR_THRESHOLD)) {
log.error("Stopping import... Error threshold exceeded for file " + file);
try {
// can throw an exception if the error threshold is met at
// the last bulk where the endHandler is called before the stop on error is called
rs.pause().close();
} catch (Exception e) {
log.error("Error threshold hit on last block of data ", e);
}
replyHandler.handle(io.vertx.core.Future.failedFuture(RTFConsts.STATUS_ERROR_THRESHOLD));
}
} else {
replyHandler.handle(io.vertx.core.Future.succeededFuture(reply.result()));
}
}));
rs.exceptionHandler(t -> {
log.error("Error reading from file " + file, t);
replyHandler.handle(io.vertx.core.Future.failedFuture(RTFConsts.STATUS_ERROR));
});
rs.endHandler(v -> {
rs.close(ar2 -> {
if (ar2.failed()) {
log.error("Error closing file " + file, ar2.cause());
}
});
});
} else {
log.error("Error opening file " + file, ar.cause());
replyHandler.handle(io.vertx.core.Future.failedFuture(RTFConsts.STATUS_ERROR));
}
});
}
Aggregations