/** * If {@link NotificationParams#getMaxRetries()} is greater than * {@link NotificationRetryEvent#getRetryCount()}, saves the message in a queue for further retry */ protected boolean saveForFurtherRetryAttempt(final NotificationRetryEvent notificationRetryEvent) { final long maxRetries = notificationRetryEvent.getNotificationDeliveryContext().getNotificationParams().getMaxRetries(); final long retryCount = notificationRetryEvent.getRetryCount(); final boolean retry = retryCount < maxRetries ? true : false; if (retry) { repository.save(notificationRetryEvent); LOGGER.info("Message {} enqueued to be processed further", notificationRetryEvent.getMessage()); } else { final String endpoint = notificationRetryEvent.getNotificationDeliveryContext().getNotificationParams().getEndpoint(); LOGGER.warn("Push message {} to {} is rejected because the number of maximum retries ({}) has been achieved.", notificationRetryEvent.getMessage(), endpoint, maxRetries); } return retry; }
@Override public void pushNotification(final NotificationRetryEvent notificationRetryEvent) { final NotificationDeliveryContext notificationContext = notificationRetryEvent.getNotificationDeliveryContext(); final NotificationParams notificationParams = notificationContext.getNotificationParams(); try { final RESTClientImpl restClient = getRestClient(notificationContext.getEntity()); LOGGER.info("Push notification to endpoint {} : {}", notificationParams.getEndpoint(), notificationRetryEvent.getMessage()); final RequestContext rc = new RequestContext("", notificationRetryEvent.getMessage()); rc.setHost(notificationParams.getEndpoint()); rc.setSecretKey(notificationParams.getSecretCallbackKey()); restClient.post(rc); publishPushCounterEvent(notificationContext); } catch (final Exception e) { LOGGER.warn("Error sending push notification {} to {}. Number of retries: {} ", notificationRetryEvent.getMessage(), notificationContext.getNotificationParams().getEndpoint(), notificationRetryEvent.getRetryCount()); if (retryNotificationsEnabled) { saveForFurtherRetryAttempt(notificationRetryEvent); } } }
private SubscriptionMessage parseSubscriptionToSubscribeMessage(final Subscription subscription) { final SubscriptionMessage message = new SubscriptionMessage(); message.setType(subscription.getType().toString()); if (subscription.getNotificationParams() != null) { message.setEndpoint(subscription.getNotificationParams().getEndpoint()); message.setMaxRetries(subscription.getNotificationParams().getMaxRetries()); message.setRetryDelay(subscription.getNotificationParams().getRetryDelay()); } switch (subscription.getType()) { case DATA: message.setProvider(((DataSubscription) subscription).getProviderId()); message.setSensor(((DataSubscription) subscription).getSensorId()); break; case ALARM: message.setAlert(((AlarmSubscription) subscription).getAlertId()); break; case ORDER: message.setProvider(((OrderSubscription) subscription).getOwnerEntityId()); message.setSensor(((OrderSubscription) subscription).getSensorId()); break; } return message; }
@Override public void validateRequestMessageOnPut(final SubscribeInputMessage requestMessage) throws MessageValidationException { Assert.notNull(requestMessage); Assert.notNull(requestMessage.getSubscription()); final Subscription subscription = requestMessage.getSubscription(); if (subscription.getType() == null) { throw new MessageValidationException("To register a subscription is mandatory to fill in the type of the subscription"); } if (subscription.getNotificationParams() == null || !StringUtils.hasText(subscription.getNotificationParams().getEndpoint())) { throw new MessageValidationException("To register a subscription is mandatory to fill in the endpoint of the subscription"); } super.validateRequestMessageOnPut(requestMessage); } }
final long retryDelayMinutes = notification.getNotificationDeliveryContext().getNotificationParams().getRetryDelay(); final int retryCount = notification.getRetryCount(); final double newDelayMillis = Math.pow(2, retryCount) * (retryDelayMinutes * 60 * 1000);
private NotificationParams buildNotificationParams(final SentiloRequest request) { final SubscriptionMessage inputMessage = (SubscriptionMessage) readInternal(SubscriptionMessage.class, request); final String endpoint = inputMessage != null ? inputMessage.getEndpoint() : null; final String secret = inputMessage != null ? inputMessage.getSecretCallbackKey() : null; final long maxRetries = inputMessage != null && inputMessage.getMaxRetries() > 0 ? inputMessage.getMaxRetries() : SentiloConstants.DEFAULT_MAX_RETRIES; final long retryDelay = inputMessage != null && inputMessage.getRetryDelay() > 0 ? inputMessage.getRetryDelay() : SentiloConstants.DEFAULT_RETRY_DELAY; return new NotificationParams(endpoint, secret, maxRetries, retryDelay); }