Search in sources :

Example 1 with Importer

use of org.folio.rest.resource.interfaces.Importer in project raml-module-builder by folio-org.

the class ProcessUploads method init.

@Override
public void init(Vertx vertx) {
    this.vertx = vertx;
    /*
     * get all importer implementations in the classpath
     */
    ArrayList<Class<?>> impls = new ArrayList<>();
    try {
        impls = InterfaceToImpl.convert2Impl(RTFConsts.PACKAGE_OF_IMPLEMENTATIONS, RTFConsts.PACKAGE_OF_HOOK_INTERFACES + ".Importer", true);
    } catch (Exception e) {
        // no impl found
        log.error(e);
    }
    /*
     * loop on importer impl, extract the address field and create a event bus handler on each
     * of the implementation's addresses
     */
    for (int i = 0; i < impls.size(); i++) {
        Importer importer = null;
        String[] address = new String[] { null };
        try {
            importer = (Importer) impls.get(i).newInstance();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        if (importer != null) {
            address[0] = importer.getImportAddress();
            if (address[0] == null) {
                // throw exception
                log.error("Notification address is null, can not register job ");
            } else {
                // cache the importer impl
                importerCache.put(address[0], importer);
                /*
           * register each address from each Importer impl on the event bus
           */
                MessageConsumer<Object> consumer = vertx.eventBus().consumer(address[0]);
                consumer.handler(message -> {
                    log.debug("Received a message to " + address[0] + ": " + message.body());
                    JobConf cObj = (JobConf) message.body();
                    registerJob(cObj, message);
                });
                log.info("Import Job " + impls.get(i).getName() + " Initialized, registered on address " + address[0]);
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) JsonObject(io.vertx.core.json.JsonObject) JobConf(org.folio.rest.jaxrs.model.JobConf) Importer(org.folio.rest.resource.interfaces.Importer)

Example 2 with Importer

use of org.folio.rest.resource.interfaces.Importer 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));
        }
    });
}
Also used : OpenOptions(io.vertx.core.file.OpenOptions) Importer(org.folio.rest.resource.interfaces.Importer) AsyncFile(io.vertx.core.file.AsyncFile) OpenOptions(io.vertx.core.file.OpenOptions) FileDataHandler(org.folio.rest.resource.handlers.FileDataHandler) Vertx(io.vertx.core.Vertx) Message(io.vertx.core.eventbus.Message) HashMap(java.util.HashMap) Job(org.folio.rest.jaxrs.model.Job) LoggerFactory(io.vertx.core.logging.LoggerFactory) ArrayList(java.util.ArrayList) Messages(org.folio.rest.tools.messages.Messages) InterfaceToImpl(org.folio.rest.tools.utils.InterfaceToImpl) Map(java.util.Map) JobAPI(org.folio.rest.resource.interfaces.JobAPI) JsonObject(io.vertx.core.json.JsonObject) RTFConsts(org.folio.rest.tools.RTFConsts) AsyncResult(io.vertx.core.AsyncResult) Handler(io.vertx.core.Handler) MessageConsumer(io.vertx.core.eventbus.MessageConsumer) Logger(io.vertx.core.logging.Logger) JobConf(org.folio.rest.jaxrs.model.JobConf) AsyncFile(io.vertx.core.file.AsyncFile) FileDataHandler(org.folio.rest.resource.handlers.FileDataHandler)

Aggregations

JsonObject (io.vertx.core.json.JsonObject)2 ArrayList (java.util.ArrayList)2 JobConf (org.folio.rest.jaxrs.model.JobConf)2 Importer (org.folio.rest.resource.interfaces.Importer)2 AsyncResult (io.vertx.core.AsyncResult)1 Handler (io.vertx.core.Handler)1 Vertx (io.vertx.core.Vertx)1 Message (io.vertx.core.eventbus.Message)1 MessageConsumer (io.vertx.core.eventbus.MessageConsumer)1 AsyncFile (io.vertx.core.file.AsyncFile)1 OpenOptions (io.vertx.core.file.OpenOptions)1 Logger (io.vertx.core.logging.Logger)1 LoggerFactory (io.vertx.core.logging.LoggerFactory)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Job (org.folio.rest.jaxrs.model.Job)1 FileDataHandler (org.folio.rest.resource.handlers.FileDataHandler)1 JobAPI (org.folio.rest.resource.interfaces.JobAPI)1 RTFConsts (org.folio.rest.tools.RTFConsts)1 Messages (org.folio.rest.tools.messages.Messages)1