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;
}
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;
}
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();
}
}
}
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;
}
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);
}
}
}
}
Aggregations