Search in sources :

Example 1 with NotificationMessage

use of org.finra.herd.model.dto.NotificationMessage in project herd by FINRAOS.

the class DefaultNotificationMessageBuilder method buildBusinessObjectFormatVersionChangeMessages.

@Override
public List<NotificationMessage> buildBusinessObjectFormatVersionChangeMessages(BusinessObjectFormatKey businessObjectFormatKey, String oldBusinessObjectFormatVersion) {
    // Create a result list.
    List<NotificationMessage> notificationMessages = new ArrayList<>();
    // Get notification message definitions.
    NotificationMessageDefinitions notificationMessageDefinitions = configurationDaoHelper.getXmlClobPropertyAndUnmarshallToObject(NotificationMessageDefinitions.class, ConfigurationValue.HERD_NOTIFICATION_BUSINESS_OBJECT_FORMAT_VERSION_CHANGE_MESSAGE_DEFINITIONS.getKey());
    // Continue processing if notification message definitions are configured.
    if (notificationMessageDefinitions != null && CollectionUtils.isNotEmpty(notificationMessageDefinitions.getNotificationMessageDefinitions())) {
        // Create a context map of values that can be used when building the message.
        Map<String, Object> velocityContextMap = getBusinessObjectFormatVersionChangeMessageVelocityContextMap(businessObjectFormatKey, oldBusinessObjectFormatVersion);
        // Generate notification message for each notification message definition.
        for (NotificationMessageDefinition notificationMessageDefinition : notificationMessageDefinitions.getNotificationMessageDefinitions()) {
            // Validate the notification message type.
            if (StringUtils.isBlank(notificationMessageDefinition.getMessageType())) {
                throw new IllegalStateException(String.format("Notification message type must be specified. Please update \"%s\" configuration entry.", ConfigurationValue.HERD_NOTIFICATION_BUSINESS_OBJECT_FORMAT_VERSION_CHANGE_MESSAGE_DEFINITIONS.getKey()));
            }
            // Validate the notification message destination.
            if (StringUtils.isBlank(notificationMessageDefinition.getMessageDestination())) {
                throw new IllegalStateException(String.format("Notification message destination must be specified. Please update \"%s\" configuration entry.", ConfigurationValue.HERD_NOTIFICATION_BUSINESS_OBJECT_FORMAT_VERSION_CHANGE_MESSAGE_DEFINITIONS.getKey()));
            }
            // Evaluate the template to generate the message text.
            String messageText = evaluateVelocityTemplate(notificationMessageDefinition.getMessageVelocityTemplate(), velocityContextMap, "businessObjectFormatVersionChangeEvent");
            // Build a list of optional message headers.
            List<MessageHeader> messageHeaders = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(notificationMessageDefinition.getMessageHeaderDefinitions())) {
                for (MessageHeaderDefinition messageHeaderDefinition : notificationMessageDefinition.getMessageHeaderDefinitions()) {
                    messageHeaders.add(new MessageHeader(messageHeaderDefinition.getKey(), evaluateVelocityTemplate(messageHeaderDefinition.getValueVelocityTemplate(), velocityContextMap, String.format("businessObjectFormatVersionChangeEvent_messageHeader_%s", messageHeaderDefinition.getKey()))));
                }
            }
            // Create a notification message and add it to the result list.
            notificationMessages.add(new NotificationMessage(notificationMessageDefinition.getMessageType(), notificationMessageDefinition.getMessageDestination(), messageText, messageHeaders));
        }
    }
    // Return the results.
    return notificationMessages;
}
Also used : NotificationMessageDefinition(org.finra.herd.model.api.xml.NotificationMessageDefinition) NotificationMessageDefinitions(org.finra.herd.model.api.xml.NotificationMessageDefinitions) MessageHeaderDefinition(org.finra.herd.model.api.xml.MessageHeaderDefinition) NotificationMessage(org.finra.herd.model.dto.NotificationMessage) ArrayList(java.util.ArrayList) MessageHeader(org.finra.herd.model.dto.MessageHeader)

Example 2 with NotificationMessage

use of org.finra.herd.model.dto.NotificationMessage in project herd by FINRAOS.

the class DefaultNotificationMessageBuilder method buildStorageUnitStatusChangeMessages.

@Override
public List<NotificationMessage> buildStorageUnitStatusChangeMessages(BusinessObjectDataKey businessObjectDataKey, String storageName, String newStorageUnitStatus, String oldStorageUnitStatus) {
    // Create a result list.
    List<NotificationMessage> notificationMessages = new ArrayList<>();
    // Get notification message definitions.
    NotificationMessageDefinitions notificationMessageDefinitions = configurationDaoHelper.getXmlClobPropertyAndUnmarshallToObject(NotificationMessageDefinitions.class, ConfigurationValue.HERD_NOTIFICATION_STORAGE_UNIT_STATUS_CHANGE_MESSAGE_DEFINITIONS.getKey());
    // Continue processing if notification message definitions are configured.
    if (notificationMessageDefinitions != null && CollectionUtils.isNotEmpty(notificationMessageDefinitions.getNotificationMessageDefinitions())) {
        // Create a context map of values that can be used when building the message.
        Map<String, Object> velocityContextMap = getStorageUnitStatusChangeMessageVelocityContextMap(businessObjectDataKey, storageName, newStorageUnitStatus, oldStorageUnitStatus);
        // Generate notification message for each notification message definition.
        for (NotificationMessageDefinition notificationMessageDefinition : notificationMessageDefinitions.getNotificationMessageDefinitions()) {
            // Validate the notification message type.
            if (StringUtils.isBlank(notificationMessageDefinition.getMessageType())) {
                throw new IllegalStateException(String.format("Notification message type must be specified. Please update \"%s\" configuration entry.", ConfigurationValue.HERD_NOTIFICATION_STORAGE_UNIT_STATUS_CHANGE_MESSAGE_DEFINITIONS.getKey()));
            }
            // Validate the notification message destination.
            if (StringUtils.isBlank(notificationMessageDefinition.getMessageDestination())) {
                throw new IllegalStateException(String.format("Notification message destination must be specified. Please update \"%s\" configuration entry.", ConfigurationValue.HERD_NOTIFICATION_STORAGE_UNIT_STATUS_CHANGE_MESSAGE_DEFINITIONS.getKey()));
            }
            // Evaluate the template to generate the message text.
            String messageText = evaluateVelocityTemplate(notificationMessageDefinition.getMessageVelocityTemplate(), velocityContextMap, "storageUnitStatusChangeEvent");
            // Build a list of optional message headers.
            List<MessageHeader> messageHeaders = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(notificationMessageDefinition.getMessageHeaderDefinitions())) {
                for (MessageHeaderDefinition messageHeaderDefinition : notificationMessageDefinition.getMessageHeaderDefinitions()) {
                    messageHeaders.add(new MessageHeader(messageHeaderDefinition.getKey(), evaluateVelocityTemplate(messageHeaderDefinition.getValueVelocityTemplate(), velocityContextMap, String.format("storageUnitStatusChangeEvent_messageHeader_%s", messageHeaderDefinition.getKey()))));
                }
            }
            // Create a notification message and add it to the result list.
            notificationMessages.add(new NotificationMessage(notificationMessageDefinition.getMessageType(), notificationMessageDefinition.getMessageDestination(), messageText, messageHeaders));
        }
    }
    // Return the results.
    return notificationMessages;
}
Also used : NotificationMessageDefinition(org.finra.herd.model.api.xml.NotificationMessageDefinition) NotificationMessageDefinitions(org.finra.herd.model.api.xml.NotificationMessageDefinitions) MessageHeaderDefinition(org.finra.herd.model.api.xml.MessageHeaderDefinition) NotificationMessage(org.finra.herd.model.dto.NotificationMessage) ArrayList(java.util.ArrayList) MessageHeader(org.finra.herd.model.dto.MessageHeader)

Example 3 with NotificationMessage

use of org.finra.herd.model.dto.NotificationMessage in project herd by FINRAOS.

the class PublishNotificationMessagesAdvice method publishNotificationMessages.

/**
 * Publishes all notification messages stored in the "in-memory" notification message queue.
 *
 * @param joinPoint the join point
 *
 * @return the return value of the method at the join point
 * @throws Throwable if any errors were encountered
 */
@Around("serviceMethods()")
public Object publishNotificationMessages(ProceedingJoinPoint joinPoint) throws Throwable {
    MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
    Method method = methodSignature.getMethod();
    boolean publishNotificationMessages = method.isAnnotationPresent(PublishNotificationMessages.class);
    // Proceed to the join point (i.e. call the method and let it return).
    try {
        Object returnValue = joinPoint.proceed();
        if (publishNotificationMessages) {
            if (LOGGER.isDebugEnabled()) {
                // Get the target class being called.
                Class<?> targetClass = joinPoint.getTarget().getClass();
                LOGGER.debug("Method is initiating notification message publishing. javaMethod=\"{}.{}\" notificationMessageInMemoryQueueSize={}", targetClass.getName(), methodSignature.getName(), notificationMessageInMemoryQueue.size());
            }
            // Publish all notification messages stored in the "in-memory" notification message queue.
            while (!notificationMessageInMemoryQueue.isEmpty()) {
                // Get notification message from the "in-memory" queue.
                NotificationMessage notificationMessage = notificationMessageInMemoryQueue.remove();
                // Publish the message.
                try {
                    notificationMessagePublishingService.publishNotificationMessage(notificationMessage);
                } catch (Exception sqsException) {
                    // On error, add this notification message to the database queue.
                    try {
                        notificationMessagePublishingService.addNotificationMessageToDatabaseQueue(notificationMessage);
                    } catch (Exception dbException) {
                        // Log the error.
                        LOGGER.error("Failed to add notification message to the database queue. messageType=\"{}\" messageDestination=\"{}\" messageText={}", notificationMessage.getMessageType(), notificationMessage.getMessageDestination(), notificationMessage.getMessageText(), dbException);
                    }
                }
            }
        }
        return returnValue;
    } finally {
        // Removes all of the elements from the queue, since the thread might be reused from the thread pool.
        if (publishNotificationMessages) {
            notificationMessageInMemoryQueue.clear();
        }
    }
}
Also used : MethodSignature(org.aspectj.lang.reflect.MethodSignature) NotificationMessage(org.finra.herd.model.dto.NotificationMessage) Method(java.lang.reflect.Method) Around(org.aspectj.lang.annotation.Around)

Example 4 with NotificationMessage

use of org.finra.herd.model.dto.NotificationMessage in project herd by FINRAOS.

the class NotificationMessagePublishingServiceImpl method publishOldestNotificationMessageFromDatabaseQueueImpl.

/**
 * Publishes and removes from the database queue the oldest notification message.
 *
 * @return true if notification message was successfully published and false otherwise
 */
@SuppressWarnings("unchecked")
protected boolean publishOldestNotificationMessageFromDatabaseQueueImpl() {
    // Initialize the result flag to false.
    boolean result = false;
    // Retrieve the oldest notification message from the database queue, unless the queue is empty.
    NotificationMessageEntity notificationMessageEntity = notificationMessageDao.getOldestNotificationMessage();
    // If message is retrieved, publish and remove it from the queue.
    if (notificationMessageEntity != null) {
        // Get the message headers from the entity.
        List<MessageHeader> messageHeaders = null;
        if (StringUtils.isNotBlank(notificationMessageEntity.getMessageHeaders())) {
            try {
                messageHeaders = jsonHelper.unmarshallJsonToListOfObjects(MessageHeader.class, notificationMessageEntity.getMessageHeaders());
            } catch (IOException e) {
                throw new IllegalStateException(String.format("Failed to unmarshall notification message headers. " + "messageId=%d messageType=%s messageDestination=%s messageText=%s messageHeaders=%s", notificationMessageEntity.getId(), notificationMessageEntity.getMessageType().getCode(), notificationMessageEntity.getMessageDestination(), notificationMessageEntity.getMessageText(), notificationMessageEntity.getMessageHeaders()), e);
            }
        }
        // Publish notification message.
        publishNotificationMessageImpl(new NotificationMessage(notificationMessageEntity.getMessageType().getCode(), notificationMessageEntity.getMessageDestination(), notificationMessageEntity.getMessageText(), messageHeaders));
        // Delete this message from the queue.
        notificationMessageDao.delete(notificationMessageEntity);
        // Set the result flag to true.
        result = true;
    }
    return result;
}
Also used : NotificationMessage(org.finra.herd.model.dto.NotificationMessage) NotificationMessageEntity(org.finra.herd.model.jpa.NotificationMessageEntity) MessageHeader(org.finra.herd.model.dto.MessageHeader) IOException(java.io.IOException)

Example 5 with NotificationMessage

use of org.finra.herd.model.dto.NotificationMessage in project herd by FINRAOS.

the class SearchIndexUpdateHelper method processMessage.

/**
 * Processes a message by adding it to the database "queue" table to ultimately be placed on the real queue by a separate job.
 *
 * @param messageText the message text to place on the queue
 */
private void processMessage(String messageText) {
    boolean isSearchIndexUpdateSqsNotificationEnabled = Boolean.valueOf(configurationHelper.getProperty(ConfigurationValue.SEARCH_INDEX_UPDATE_JMS_LISTENER_ENABLED));
    LOGGER.info("searchIndexUpdateSqsNotificationEnabled={} messageText={}", isSearchIndexUpdateSqsNotificationEnabled, messageText);
    // Only process messages if the service is enabled.
    if (isSearchIndexUpdateSqsNotificationEnabled) {
        // Add the message to the database queue if a message was configured. Otherwise, log a warning.
        if (StringUtils.isBlank(messageText)) {
            LOGGER.warn("Not sending search index update message because it is not configured.");
        } else {
            NotificationMessage notificationMessage = new NotificationMessage(MessageTypeEntity.MessageEventTypes.SQS.name(), getSqsQueueName(), messageText, null);
            // Add the notification message to the database JMS message queue to be processed.
            notificationMessagePublishingService.addNotificationMessageToDatabaseQueue(notificationMessage);
            // Schedule JMS publishing job.
            try {
                systemJobHelper.runSystemJob(JmsPublishingJob.JOB_NAME, null);
            } catch (ObjectAlreadyExistsException objectAlreadyExistsException) {
                // Ignore the error when job is already running.
                LOGGER.info("Failed to schedule JMS publishing job: ObjectAlreadyExistsException occurred");
            } catch (Exception e) {
                LOGGER.error("Failed to schedule JMS publishing job.", e);
            }
        }
    }
}
Also used : NotificationMessage(org.finra.herd.model.dto.NotificationMessage) ObjectAlreadyExistsException(org.quartz.ObjectAlreadyExistsException) ObjectAlreadyExistsException(org.quartz.ObjectAlreadyExistsException)

Aggregations

NotificationMessage (org.finra.herd.model.dto.NotificationMessage)52 Test (org.junit.Test)44 NotificationMessageDefinition (org.finra.herd.model.api.xml.NotificationMessageDefinition)32 NotificationMessageDefinitions (org.finra.herd.model.api.xml.NotificationMessageDefinitions)32 ConfigurationEntity (org.finra.herd.model.jpa.ConfigurationEntity)29 BusinessObjectDataKey (org.finra.herd.model.api.xml.BusinessObjectDataKey)25 AbstractServiceTest (org.finra.herd.service.AbstractServiceTest)25 BusinessObjectDataEntity (org.finra.herd.model.jpa.BusinessObjectDataEntity)24 MessageHeader (org.finra.herd.model.dto.MessageHeader)13 ArrayList (java.util.ArrayList)9 Attribute (org.finra.herd.model.api.xml.Attribute)7 BusinessObjectFormatKey (org.finra.herd.model.api.xml.BusinessObjectFormatKey)6 MessageHeaderDefinition (org.finra.herd.model.api.xml.MessageHeaderDefinition)6 ProceedingJoinPoint (org.aspectj.lang.ProceedingJoinPoint)4 AttributeDefinition (org.finra.herd.model.api.xml.AttributeDefinition)4 SearchIndexUpdateDto (org.finra.herd.model.dto.SearchIndexUpdateDto)4 HashMap (java.util.HashMap)3 BusinessObjectDataInvalidateUnregisteredRequest (org.finra.herd.model.api.xml.BusinessObjectDataInvalidateUnregisteredRequest)3 BusinessObjectFormatEntity (org.finra.herd.model.jpa.BusinessObjectFormatEntity)3 NotificationMessageEntity (org.finra.herd.model.jpa.NotificationMessageEntity)3