Search in sources :

Example 11 with NotificationQueue

use of org.killbill.notificationq.api.NotificationQueue in project killbill by killbill.

the class PushNotificationListener method saveRetryPushNotificationInQueue.

private void saveRetryPushNotificationInQueue(final UUID tenantId, final String url, final NotificationJson notificationJson, final int attemptRetryNumber) {
    final PushNotificationKey key = new PushNotificationKey(tenantId, notificationJson.getAccountId() != null ? UUID.fromString(notificationJson.getAccountId()) : null, notificationJson.getEventType(), notificationJson.getObjectType(), notificationJson.getObjectId() != null ? UUID.fromString(notificationJson.getObjectId()) : null, attemptRetryNumber + 1, url);
    final TenantContext tenantContext = contextFactory.createTenantContext(tenantId);
    final DateTime nextNotificationTime = getNextNotificationTime(key.getAttemptNumber(), internalCallContextFactory.createInternalTenantContextWithoutAccountRecordId(tenantContext));
    if (nextNotificationTime == null) {
        log.warn("Max attempt number reached for push notification url='{}', tenantId='{}'", key.getUrl(), key.getTenantId());
        return;
    }
    log.debug("Push notification is scheduled to send at {} for url='{}', tenantId='{}'", nextNotificationTime, key.getUrl(), key.getTenantId());
    final Long accountRecordId = internalCallContextFactory.getRecordIdFromObject(key.getAccountId(), ObjectType.ACCOUNT, tenantContext);
    final Long tenantRecordId = internalCallContextFactory.getRecordIdFromObject(key.getTenantId(), ObjectType.TENANT, tenantContext);
    try {
        final NotificationQueue notificationQueue = notificationQueueService.getNotificationQueue(DefaultServerService.SERVER_SERVICE, PushNotificationRetryService.QUEUE_NAME);
        notificationQueue.recordFutureNotification(nextNotificationTime, key, null, MoreObjects.firstNonNull(accountRecordId, new Long(0)), tenantRecordId);
    } catch (NoSuchNotificationQueue noSuchNotificationQueue) {
        log.error("Failed to push notification url='{}', tenantId='{}'", key.getUrl(), key.getTenantId(), noSuchNotificationQueue);
    } catch (IOException e) {
        log.error("Failed to push notification url='{}', tenantId='{}'", key.getUrl(), key.getTenantId(), e);
    }
}
Also used : NoSuchNotificationQueue(org.killbill.notificationq.api.NotificationQueueService.NoSuchNotificationQueue) InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) TenantContext(org.killbill.billing.util.callcontext.TenantContext) NotificationQueue(org.killbill.notificationq.api.NotificationQueue) NoSuchNotificationQueue(org.killbill.notificationq.api.NotificationQueueService.NoSuchNotificationQueue) IOException(java.io.IOException) DateTime(org.joda.time.DateTime)

Example 12 with NotificationQueue

use of org.killbill.notificationq.api.NotificationQueue in project killbill by killbill.

the class DefaultNextBillingDatePoster method insertNextBillingFromTransactionInternal.

private void insertNextBillingFromTransactionInternal(final EntitySqlDaoWrapperFactory entitySqlDaoWrapperFactory, final UUID subscriptionId, final Boolean isDryRunForInvoiceNotification, final DateTime futureNotificationTime, final DateTime targetDate, final InternalCallContext internalCallContext) {
    final NotificationQueue nextBillingQueue;
    try {
        nextBillingQueue = notificationQueueService.getNotificationQueue(DefaultInvoiceService.INVOICE_SERVICE_NAME, DefaultNextBillingDateNotifier.NEXT_BILLING_DATE_NOTIFIER_QUEUE);
        // If we see existing notification for the same date (and isDryRunForInvoiceNotification mode), we don't insert a new notification
        final Iterable<NotificationEventWithMetadata<NextBillingDateNotificationKey>> futureNotifications = nextBillingQueue.getFutureNotificationFromTransactionForSearchKeys(internalCallContext.getAccountRecordId(), internalCallContext.getTenantRecordId(), entitySqlDaoWrapperFactory.getHandle().getConnection());
        boolean existingFutureNotificationWithSameDate = false;
        for (final NotificationEventWithMetadata<NextBillingDateNotificationKey> input : futureNotifications) {
            final boolean isEventDryRunForNotifications = input.getEvent().isDryRunForInvoiceNotification() != null ? input.getEvent().isDryRunForInvoiceNotification() : false;
            final LocalDate notificationEffectiveLocaleDate = internalCallContext.toLocalDate(futureNotificationTime);
            final LocalDate eventEffectiveLocaleDate = internalCallContext.toLocalDate(input.getEffectiveDate());
            if (notificationEffectiveLocaleDate.compareTo(eventEffectiveLocaleDate) == 0 && ((isDryRunForInvoiceNotification && isEventDryRunForNotifications) || (!isDryRunForInvoiceNotification && !isEventDryRunForNotifications))) {
                existingFutureNotificationWithSameDate = true;
            }
        // Go through all results to close the connection
        }
        if (!existingFutureNotificationWithSameDate) {
            log.info("Queuing next billing date notification at {} for subscriptionId {}", futureNotificationTime.toString(), subscriptionId.toString());
            nextBillingQueue.recordFutureNotificationFromTransaction(entitySqlDaoWrapperFactory.getHandle().getConnection(), futureNotificationTime, new NextBillingDateNotificationKey(subscriptionId, targetDate, isDryRunForInvoiceNotification), internalCallContext.getUserToken(), internalCallContext.getAccountRecordId(), internalCallContext.getTenantRecordId());
        } else if (log.isDebugEnabled()) {
            log.debug("*********************   SKIPPING Queuing next billing date notification at {} for subscriptionId {} *******************", futureNotificationTime.toString(), subscriptionId.toString());
        }
    } catch (final NoSuchNotificationQueue e) {
        log.error("Attempting to put items on a non-existent queue (NextBillingDateNotifier).", e);
    } catch (final IOException e) {
        log.error("Failed to serialize notificationKey for subscriptionId {}", subscriptionId);
    }
}
Also used : NoSuchNotificationQueue(org.killbill.notificationq.api.NotificationQueueService.NoSuchNotificationQueue) NotificationEventWithMetadata(org.killbill.notificationq.api.NotificationEventWithMetadata) NoSuchNotificationQueue(org.killbill.notificationq.api.NotificationQueueService.NoSuchNotificationQueue) NotificationQueue(org.killbill.notificationq.api.NotificationQueue) IOException(java.io.IOException) LocalDate(org.joda.time.LocalDate)

Example 13 with NotificationQueue

use of org.killbill.notificationq.api.NotificationQueue in project killbill by killbill.

the class ParentInvoiceCommitmentPoster method insertParentInvoiceFromTransactionInternal.

public void insertParentInvoiceFromTransactionInternal(final EntitySqlDaoWrapperFactory entitySqlDaoWrapperFactory, final UUID invoiceId, final DateTime futureNotificationTime, final InternalCallContext internalCallContext) {
    final NotificationQueue commitInvoiceQueue;
    try {
        commitInvoiceQueue = notificationQueueService.getNotificationQueue(DefaultInvoiceService.INVOICE_SERVICE_NAME, ParentInvoiceCommitmentNotifier.PARENT_INVOICE_COMMITMENT_NOTIFIER_QUEUE);
        // If we see existing notification for the same date we don't insert a new notification
        final Iterable<NotificationEventWithMetadata<ParentInvoiceCommitmentNotificationKey>> futureNotifications = commitInvoiceQueue.getFutureNotificationFromTransactionForSearchKeys(internalCallContext.getAccountRecordId(), internalCallContext.getTenantRecordId(), entitySqlDaoWrapperFactory.getHandle().getConnection());
        boolean existingFutureNotificationWithSameDate = false;
        for (final NotificationEventWithMetadata<ParentInvoiceCommitmentNotificationKey> input : futureNotifications) {
            final LocalDate notificationEffectiveLocaleDate = internalCallContext.toLocalDate(futureNotificationTime);
            final LocalDate eventEffectiveLocaleDate = internalCallContext.toLocalDate(input.getEffectiveDate());
            if (notificationEffectiveLocaleDate.compareTo(eventEffectiveLocaleDate) == 0) {
                existingFutureNotificationWithSameDate = true;
            }
        // Go through all results to close the connection
        }
        if (!existingFutureNotificationWithSameDate) {
            log.info("Queuing parent invoice commitment notification at {} for invoiceId {}", futureNotificationTime.toString(), invoiceId.toString());
            commitInvoiceQueue.recordFutureNotificationFromTransaction(entitySqlDaoWrapperFactory.getHandle().getConnection(), futureNotificationTime, new ParentInvoiceCommitmentNotificationKey(invoiceId), internalCallContext.getUserToken(), internalCallContext.getAccountRecordId(), internalCallContext.getTenantRecordId());
        } else if (log.isDebugEnabled()) {
            log.debug("*********************   SKIPPING Queuing parent invoice commitment notification at {} for invoiceId {} *******************", futureNotificationTime.toString(), invoiceId.toString());
        }
    } catch (final NoSuchNotificationQueue e) {
        log.error("Attempting to put items on a non-existent queue (ParentInvoiceCommitmentNotifier).", e);
    } catch (final IOException e) {
        log.error("Failed to serialize notificationKey for invoiceId {}", invoiceId);
    }
}
Also used : NoSuchNotificationQueue(org.killbill.notificationq.api.NotificationQueueService.NoSuchNotificationQueue) NotificationEventWithMetadata(org.killbill.notificationq.api.NotificationEventWithMetadata) NoSuchNotificationQueue(org.killbill.notificationq.api.NotificationQueueService.NoSuchNotificationQueue) NotificationQueue(org.killbill.notificationq.api.NotificationQueue) IOException(java.io.IOException) LocalDate(org.joda.time.LocalDate)

Example 14 with NotificationQueue

use of org.killbill.notificationq.api.NotificationQueue in project killbill by killbill.

the class DefaultSubscriptionInternalApi method getFutureNotificationsForAccount.

@Override
public Iterable<DateTime> getFutureNotificationsForAccount(final InternalCallContext internalCallContext) {
    try {
        final NotificationQueue notificationQueue = notificationQueueService.getNotificationQueue(DefaultSubscriptionBaseService.SUBSCRIPTION_SERVICE_NAME, DefaultSubscriptionBaseService.NOTIFICATION_QUEUE_NAME);
        final Iterable<NotificationEventWithMetadata<NotificationEvent>> futureNotifications = notificationQueue.getFutureNotificationForSearchKeys(internalCallContext.getAccountRecordId(), internalCallContext.getTenantRecordId());
        return Iterables.transform(futureNotifications, new Function<NotificationEventWithMetadata<NotificationEvent>, DateTime>() {

            @Nullable
            @Override
            public DateTime apply(final NotificationEventWithMetadata<NotificationEvent> input) {
                return input.getEffectiveDate();
            }
        });
    } catch (final NoSuchNotificationQueue noSuchNotificationQueue) {
        throw new IllegalStateException(noSuchNotificationQueue);
    }
}
Also used : NoSuchNotificationQueue(org.killbill.notificationq.api.NotificationQueueService.NoSuchNotificationQueue) NotificationEventWithMetadata(org.killbill.notificationq.api.NotificationEventWithMetadata) NotificationEvent(org.killbill.notificationq.api.NotificationEvent) NotificationQueue(org.killbill.notificationq.api.NotificationQueue) NoSuchNotificationQueue(org.killbill.notificationq.api.NotificationQueueService.NoSuchNotificationQueue) PlanPhasePriceOverride(org.killbill.billing.catalog.api.PlanPhasePriceOverride) DateTime(org.joda.time.DateTime) Nullable(javax.annotation.Nullable) PlanPhasePriceOverride(org.killbill.billing.catalog.api.PlanPhasePriceOverride)

Example 15 with NotificationQueue

use of org.killbill.notificationq.api.NotificationQueue in project killbill by killbill.

the class TestNextBillingDateNotifier method testInvoiceNotifier.

@Test(groups = "slow")
public void testInvoiceNotifier() throws Exception {
    final SubscriptionBase subscription = invoiceUtil.createSubscription();
    final UUID subscriptionId = subscription.getId();
    final DateTime now = clock.getUTCNow();
    final NotificationQueue nextBillingQueue = notificationQueueService.getNotificationQueue(DefaultInvoiceService.INVOICE_SERVICE_NAME, DefaultNextBillingDateNotifier.NEXT_BILLING_DATE_NOTIFIER_QUEUE);
    nextBillingQueue.recordFutureNotification(now, new NextBillingDateNotificationKey(subscriptionId, now, Boolean.FALSE), internalCallContext.getUserToken(), internalCallContext.getAccountRecordId(), internalCallContext.getTenantRecordId());
    // Move time in the future after the notification effectiveDate
    ((ClockMock) clock).setDeltaFromReality(3000);
    await().atMost(1, MINUTES).until(new Callable<Boolean>() {

        @Override
        public Boolean call() throws Exception {
            return testInvoiceNotificationQListener.getEventCount() == 1;
        }
    });
    Assert.assertEquals(testInvoiceNotificationQListener.getEventCount(), 1);
    Assert.assertEquals(testInvoiceNotificationQListener.getLatestSubscriptionId(), subscriptionId);
}
Also used : SubscriptionBase(org.killbill.billing.subscription.api.SubscriptionBase) NotificationQueue(org.killbill.notificationq.api.NotificationQueue) ClockMock(org.killbill.clock.ClockMock) UUID(java.util.UUID) DateTime(org.joda.time.DateTime) Test(org.testng.annotations.Test)

Aggregations

NotificationQueue (org.killbill.notificationq.api.NotificationQueue)16 NoSuchNotificationQueue (org.killbill.notificationq.api.NotificationQueueService.NoSuchNotificationQueue)15 IOException (java.io.IOException)9 NotificationEventWithMetadata (org.killbill.notificationq.api.NotificationEventWithMetadata)7 DateTime (org.joda.time.DateTime)4 NotificationEvent (org.killbill.notificationq.api.NotificationEvent)3 LocalDate (org.joda.time.LocalDate)2 EntitySqlDaoWrapperFactory (org.killbill.billing.util.entity.dao.EntitySqlDaoWrapperFactory)2 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 UUID (java.util.UUID)1 Nullable (javax.annotation.Nullable)1 InternalTenantContext (org.killbill.billing.callcontext.InternalTenantContext)1 PlanPhasePriceOverride (org.killbill.billing.catalog.api.PlanPhasePriceOverride)1 NextBillingDateNotificationKey (org.killbill.billing.invoice.notification.NextBillingDateNotificationKey)1 DefaultPaymentAttempt (org.killbill.billing.payment.api.DefaultPaymentAttempt)1 PaymentAttempt (org.killbill.billing.payment.api.PaymentAttempt)1 PaymentAttemptModelDao (org.killbill.billing.payment.dao.PaymentAttemptModelDao)1 PaymentRetryNotificationKey (org.killbill.billing.payment.retry.PaymentRetryNotificationKey)1 SubscriptionBase (org.killbill.billing.subscription.api.SubscriptionBase)1