Search in sources :

Example 11 with LockFailedException

use of org.killbill.commons.locker.LockFailedException in project killbill by killbill.

the class PaymentPluginDispatcher method dispatchWithExceptionHandling.

public static <ReturnType> ReturnType dispatchWithExceptionHandling(@Nullable final Account account, final String pluginNames, final Callable<PluginDispatcherReturnType<ReturnType>> callable, final PluginDispatcher<ReturnType> pluginDispatcher) throws PaymentApiException {
    final UUID accountId = account != null ? account.getId() : null;
    final String accountExternalKey = account != null ? account.getExternalKey() : "";
    try {
        log.debug("Calling plugin(s) {}", pluginNames);
        final ReturnType result = pluginDispatcher.dispatchWithTimeout(callable);
        log.debug("Successful plugin(s) call of {} for account {} with result {}", pluginNames, accountExternalKey, result);
        return result;
    } catch (final TimeoutException e) {
        final String errorMessage = String.format("Call TIMEOUT for accountId='%s' accountExternalKey='%s' plugin='%s'", accountId, accountExternalKey, pluginNames);
        log.warn(errorMessage);
        throw new PaymentApiException(ErrorCode.PAYMENT_PLUGIN_TIMEOUT, accountId, errorMessage);
    } catch (final InterruptedException e) {
        Thread.currentThread().interrupt();
        final String errorMessage = String.format("Call was interrupted for accountId='%s' accountExternalKey='%s' plugin='%s'", accountId, accountExternalKey, pluginNames);
        log.warn(errorMessage, e);
        throw new PaymentApiException(ErrorCode.PAYMENT_INTERNAL_ERROR, MoreObjects.firstNonNull(e.getMessage(), errorMessage));
    } catch (final ExecutionException e) {
        if (e.getCause() instanceof PaymentApiException) {
            throw (PaymentApiException) e.getCause();
        } else if (e.getCause() instanceof LockFailedException) {
            final String format = String.format("Failed to lock accountExternalKey='%s'", accountExternalKey);
            log.warn(format);
            throw new PaymentApiException(ErrorCode.PAYMENT_INTERNAL_ERROR, format);
        } else {
            // Unwraps the ExecutionException (e.getCause()), since it's a dispatch implementation detail
            throw new PaymentApiException(e.getCause(), ErrorCode.PAYMENT_INTERNAL_ERROR, MoreObjects.firstNonNull(e.getMessage(), ""));
        }
    }
}
Also used : LockFailedException(org.killbill.commons.locker.LockFailedException) PaymentApiException(org.killbill.billing.payment.api.PaymentApiException) UUID(java.util.UUID) PluginDispatcherReturnType(org.killbill.billing.payment.dispatcher.PluginDispatcher.PluginDispatcherReturnType) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

LockFailedException (org.killbill.commons.locker.LockFailedException)11 GlobalLock (org.killbill.commons.locker.GlobalLock)9 UUID (java.util.UUID)3 IOException (java.io.IOException)2 LinkedList (java.util.LinkedList)2 InternalCallContext (org.killbill.billing.callcontext.InternalCallContext)2 InvoiceApiException (org.killbill.billing.invoice.api.InvoiceApiException)2 InvoiceItemModelDao (org.killbill.billing.invoice.dao.InvoiceItemModelDao)2 InvoiceModelDao (org.killbill.billing.invoice.dao.InvoiceModelDao)2 PluginProperty (org.killbill.billing.payment.api.PluginProperty)2 Test (org.testng.annotations.Test)2 Predicate (com.google.common.base.Predicate)1 ImmutableList (com.google.common.collect.ImmutableList)1 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 List (java.util.List)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 DateTime (org.joda.time.DateTime)1 LocalDate (org.joda.time.LocalDate)1 InternalTenantContext (org.killbill.billing.callcontext.InternalTenantContext)1