use of org.finra.herd.model.jpa.NotificationMessageEntity 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.jpa.NotificationMessageEntity in project herd by FINRAOS.
the class NotificationMessagePublishingServiceTest method testPublishOldestNotificationMessageFromDatabaseQueueJsonParseException.
@Test
public void testPublishOldestNotificationMessageFromDatabaseQueueJsonParseException() {
// Prepare database entries required for testing.
NotificationMessageEntity notificationMessageEntity = notificationMessageDaoTestHelper.createNotificationMessageEntity(MESSAGE_TYPE, MESSAGE_DESTINATION, MESSAGE_TEXT);
notificationMessageEntity.setMessageHeaders(INVALID_VALUE);
notificationMessageDao.saveAndRefresh(notificationMessageEntity);
// Try to publish notification message.
try {
notificationMessagePublishingService.publishOldestNotificationMessageFromDatabaseQueue();
fail();
} catch (IllegalStateException e) {
assertEquals(String.format("Failed to unmarshall notification message headers. messageId=%d messageType=%s messageDestination=%s messageText=%s messageHeaders=%s", notificationMessageEntity.getId(), MESSAGE_TYPE, MESSAGE_DESTINATION, MESSAGE_TEXT, INVALID_VALUE), e.getMessage());
}
// Check that the test notification message is still the oldest message in the database queue.
assertEquals(notificationMessageDao.getOldestNotificationMessage(), notificationMessageEntity);
}
use of org.finra.herd.model.jpa.NotificationMessageEntity in project herd by FINRAOS.
the class NotificationMessageDaoTest method testGetOldestNotificationMessage.
@Test
public void testGetOldestNotificationMessage() {
// Create database entries required for testing.
List<NotificationMessageEntity> notificationMessageEntities = Arrays.asList(notificationMessageDaoTestHelper.createNotificationMessageEntity(MESSAGE_TYPE, MESSAGE_DESTINATION, MESSAGE_TEXT), notificationMessageDaoTestHelper.createNotificationMessageEntity(MESSAGE_TYPE_2, MESSAGE_DESTINATION_2, MESSAGE_TEXT_2));
// Retrieve the oldest notification message.
NotificationMessageEntity result = notificationMessageDao.getOldestNotificationMessage();
// Validate the results.
assertEquals(notificationMessageEntities.get(0), result);
}
use of org.finra.herd.model.jpa.NotificationMessageEntity in project herd by FINRAOS.
the class NotificationMessageDaoTestHelper method createNotificationMessageEntity.
/**
* Creates and persists a new notification message entity.
*
* @param messageDestination the destination of the message
* @param messageText the text of the message
*
* @return the newly created notification message entity
*/
public NotificationMessageEntity createNotificationMessageEntity(String messageType, String messageDestination, String messageText) {
// Create a message type entity if needed.
MessageTypeEntity messageTypeEntity = messageTypeDao.getMessageTypeByCode(messageType);
if (messageTypeEntity == null) {
messageTypeEntity = messageTypeDaoTestHelper.createMessageTypeEntity(messageType);
}
// Create a notification message entity.
NotificationMessageEntity notificationMessageEntity = new NotificationMessageEntity();
notificationMessageEntity.setMessageType(messageTypeEntity);
notificationMessageEntity.setMessageDestination(messageDestination);
notificationMessageEntity.setMessageText(messageText);
// Persist and return the newly created entity.
return notificationMessageDao.saveAndRefresh(notificationMessageEntity);
}
use of org.finra.herd.model.jpa.NotificationMessageEntity in project herd by FINRAOS.
the class NotificationMessageDaoImpl method getOldestNotificationMessage.
@Override
public NotificationMessageEntity getOldestNotificationMessage() {
// Create the criteria builder and the criteria.
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery<NotificationMessageEntity> criteria = builder.createQuery(NotificationMessageEntity.class);
// The criteria root is the notification message.
Root<NotificationMessageEntity> notificationMessageEntity = criteria.from(NotificationMessageEntity.class);
// Add the select clause.
criteria.select(notificationMessageEntity);
// Add the order by clause, since we want to return only the oldest notification message (a message with the smallest sequence generated id).
criteria.orderBy(builder.asc(notificationMessageEntity.get(NotificationMessageEntity_.id)));
// Execute the query and ask it to return only the first record.
List<NotificationMessageEntity> resultList = entityManager.createQuery(criteria).setMaxResults(1).getResultList();
// Return the result.
return resultList.size() > 0 ? resultList.get(0) : null;
}
Aggregations