Search in sources :

Example 1 with ASTransactionDao

use of io.kamax.mxisd.storage.ormlite.dao.ASTransactionDao in project mxisd by kamax-io.

the class AppSvcManager method processTransaction.

public CompletableFuture<String> processTransaction(String txnId, InputStream is) {
    ensureEnabled();
    if (StringUtils.isEmpty(txnId)) {
        throw new IllegalArgumentException("Transaction ID cannot be empty");
    }
    synchronized (this) {
        Optional<ASTransactionDao> dao = store.getTransactionResult(cfg.getUser().getMain(), txnId);
        if (dao.isPresent()) {
            log.info("AS Transaction {} already processed - returning computed result", txnId);
            return CompletableFuture.completedFuture(dao.get().getResult());
        }
        CompletableFuture<String> f = transactionsInProgress.get(txnId);
        if (Objects.nonNull(f)) {
            log.info("Returning future for transaction {}", txnId);
            return f;
        }
        transactionsInProgress.put(txnId, new CompletableFuture<>());
    }
    CompletableFuture<String> future = transactionsInProgress.get(txnId);
    Instant start = Instant.now();
    log.info("Processing AS Transaction {}: start", txnId);
    try {
        List<JsonObject> events = GsonUtil.asList(GsonUtil.getArray(parser.parse(is), "events"), JsonObject.class);
        is.close();
        log.debug("{} event(s) parsed", events.size());
        processTransaction(events);
        Instant end = Instant.now();
        String result = "{}";
        try {
            log.info("Saving transaction details to store");
            store.insertTransactionResult(cfg.getUser().getMain(), txnId, end, result);
        } finally {
            log.debug("Removing CompletedFuture from transaction map");
            transactionsInProgress.remove(txnId);
        }
        log.info("Processed AS transaction {} in {} ms", txnId, (Instant.now().toEpochMilli() - start.toEpochMilli()));
        future.complete(result);
    } catch (Exception e) {
        log.error("Unable to properly process transaction {}", txnId, e);
        future.completeExceptionally(e);
    }
    log.info("Processing AS Transaction {}: end", txnId);
    return future;
}
Also used : Instant(java.time.Instant) ASTransactionDao(io.kamax.mxisd.storage.ormlite.dao.ASTransactionDao) JsonObject(com.google.gson.JsonObject) ConfigurationException(io.kamax.mxisd.exception.ConfigurationException) NotAllowedException(io.kamax.mxisd.exception.NotAllowedException) IOException(java.io.IOException) HttpMatrixException(io.kamax.mxisd.exception.HttpMatrixException)

Example 2 with ASTransactionDao

use of io.kamax.mxisd.storage.ormlite.dao.ASTransactionDao in project mxisd by kamax-io.

the class OrmLiteSqlStorage method getTransactionResult.

@Override
public Optional<ASTransactionDao> getTransactionResult(String localpart, String txnId) {
    return withCatcher(() -> {
        ASTransactionDao dao = new ASTransactionDao();
        dao.setLocalpart(localpart);
        dao.setTransactionId(txnId);
        List<ASTransactionDao> daoList = asTxnDao.queryForMatchingArgs(dao);
        if (daoList.size() > 1) {
            throw new InternalServerError("Lookup for Transaction " + txnId + " for localpart " + localpart + " returned more than one result");
        }
        if (daoList.isEmpty()) {
            return Optional.empty();
        }
        return Optional.of(daoList.get(0));
    });
}
Also used : ASTransactionDao(io.kamax.mxisd.storage.ormlite.dao.ASTransactionDao) InternalServerError(io.kamax.mxisd.exception.InternalServerError)

Aggregations

ASTransactionDao (io.kamax.mxisd.storage.ormlite.dao.ASTransactionDao)2 JsonObject (com.google.gson.JsonObject)1 ConfigurationException (io.kamax.mxisd.exception.ConfigurationException)1 HttpMatrixException (io.kamax.mxisd.exception.HttpMatrixException)1 InternalServerError (io.kamax.mxisd.exception.InternalServerError)1 NotAllowedException (io.kamax.mxisd.exception.NotAllowedException)1 IOException (java.io.IOException)1 Instant (java.time.Instant)1