Search in sources :

Example 6 with LockFailedException

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

the class InvoiceApiHelper method dispatchToInvoicePluginsAndInsertItems.

public List<InvoiceItem> dispatchToInvoicePluginsAndInsertItems(final UUID accountId, final boolean isDryRun, final WithAccountLock withAccountLock, final CallContext context) throws InvoiceApiException {
    GlobalLock lock = null;
    try {
        lock = locker.lockWithNumberOfTries(LockerType.ACCNT_INV_PAY.toString(), accountId.toString(), invoiceConfig.getMaxGlobalLockRetries());
        final Iterable<Invoice> invoicesForPlugins = withAccountLock.prepareInvoices();
        final List<InvoiceModelDao> invoiceModelDaos = new LinkedList<InvoiceModelDao>();
        for (final Invoice invoiceForPlugin : invoicesForPlugins) {
            // Call plugin
            final List<InvoiceItem> additionalInvoiceItems = invoicePluginDispatcher.getAdditionalInvoiceItems(invoiceForPlugin, isDryRun, context);
            invoiceForPlugin.addInvoiceItems(additionalInvoiceItems);
            // Transformation to InvoiceModelDao
            final InvoiceModelDao invoiceModelDao = new InvoiceModelDao(invoiceForPlugin);
            final List<InvoiceItem> invoiceItems = invoiceForPlugin.getInvoiceItems();
            final List<InvoiceItemModelDao> invoiceItemModelDaos = toInvoiceItemModelDao(invoiceItems);
            invoiceModelDao.addInvoiceItems(invoiceItemModelDaos);
            // Keep track of modified invoices
            invoiceModelDaos.add(invoiceModelDao);
        }
        final InternalCallContext internalCallContext = internalCallContextFactory.createInternalCallContext(accountId, context);
        final List<InvoiceItemModelDao> createdInvoiceItems = dao.createInvoices(invoiceModelDaos, internalCallContext);
        return fromInvoiceItemModelDao(createdInvoiceItems);
    } catch (final LockFailedException e) {
        log.warn("Failed to process invoice items for accountId='{}'", accountId.toString(), e);
        return ImmutableList.<InvoiceItem>of();
    } finally {
        if (lock != null) {
            lock.release();
        }
    }
}
Also used : GlobalLock(org.killbill.commons.locker.GlobalLock) ItemAdjInvoiceItem(org.killbill.billing.invoice.model.ItemAdjInvoiceItem) LockFailedException(org.killbill.commons.locker.LockFailedException) InvoiceItemModelDao(org.killbill.billing.invoice.dao.InvoiceItemModelDao) InvoiceModelDao(org.killbill.billing.invoice.dao.InvoiceModelDao) InternalCallContext(org.killbill.billing.callcontext.InternalCallContext) LinkedList(java.util.LinkedList)

Example 7 with LockFailedException

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

the class InvoiceDispatcher method processParentInvoiceForInvoiceGeneration.

public void processParentInvoiceForInvoiceGeneration(final Account childAccount, final UUID childInvoiceId, final InternalCallContext context) throws InvoiceApiException {
    GlobalLock lock = null;
    try {
        lock = locker.lockWithNumberOfTries(LockerType.ACCNT_INV_PAY.toString(), childAccount.getParentAccountId().toString(), invoiceConfig.getMaxGlobalLockRetries());
        processParentInvoiceForInvoiceGenerationWithLock(childAccount, childInvoiceId, context);
    } catch (final LockFailedException e) {
        log.warn("Failed to process parent invoice for parentAccountId='{}'", childAccount.getParentAccountId().toString(), e);
    } finally {
        if (lock != null) {
            lock.release();
        }
    }
}
Also used : GlobalLock(org.killbill.commons.locker.GlobalLock) LockFailedException(org.killbill.commons.locker.LockFailedException)

Example 8 with LockFailedException

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

the class InvoiceDispatcher method processParentInvoiceForAdjustments.

public void processParentInvoiceForAdjustments(final Account childAccount, final UUID childInvoiceId, final InternalCallContext context) throws InvoiceApiException {
    GlobalLock lock = null;
    try {
        lock = locker.lockWithNumberOfTries(LockerType.ACCNT_INV_PAY.toString(), childAccount.getParentAccountId().toString(), invoiceConfig.getMaxGlobalLockRetries());
        processParentInvoiceForAdjustmentsWithLock(childAccount, childInvoiceId, context);
    } catch (final LockFailedException e) {
        log.warn("Failed to process parent invoice for parentAccountId='{}'", childAccount.getParentAccountId().toString(), e);
    } finally {
        if (lock != null) {
            lock.release();
        }
    }
}
Also used : GlobalLock(org.killbill.commons.locker.GlobalLock) LockFailedException(org.killbill.commons.locker.LockFailedException)

Example 9 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)9 GlobalLock (org.killbill.commons.locker.GlobalLock)7 IOException (java.io.IOException)2 UUID (java.util.UUID)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 LinkedList (java.util.LinkedList)1 List (java.util.List)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 InternalCallContext (org.killbill.billing.callcontext.InternalCallContext)1 InternalTenantContext (org.killbill.billing.callcontext.InternalTenantContext)1 InvoiceItemModelDao (org.killbill.billing.invoice.dao.InvoiceItemModelDao)1 InvoiceModelDao (org.killbill.billing.invoice.dao.InvoiceModelDao)1 ItemAdjInvoiceItem (org.killbill.billing.invoice.model.ItemAdjInvoiceItem)1 Payment (org.killbill.billing.payment.api.Payment)1 PaymentApiException (org.killbill.billing.payment.api.PaymentApiException)1 PaymentModelDao (org.killbill.billing.payment.dao.PaymentModelDao)1