Search in sources :

Example 1 with NotificationMessageEntity

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;
}
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 2 with NotificationMessageEntity

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);
}
Also used : NotificationMessageEntity(org.finra.herd.model.jpa.NotificationMessageEntity) Test(org.junit.Test)

Example 3 with 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);
}
Also used : NotificationMessageEntity(org.finra.herd.model.jpa.NotificationMessageEntity) Test(org.junit.Test)

Example 4 with NotificationMessageEntity

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);
}
Also used : NotificationMessageEntity(org.finra.herd.model.jpa.NotificationMessageEntity) MessageTypeEntity(org.finra.herd.model.jpa.MessageTypeEntity)

Example 5 with 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;
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) NotificationMessageEntity(org.finra.herd.model.jpa.NotificationMessageEntity)

Aggregations

NotificationMessageEntity (org.finra.herd.model.jpa.NotificationMessageEntity)9 Test (org.junit.Test)5 NotificationMessage (org.finra.herd.model.dto.NotificationMessage)3 MessageHeader (org.finra.herd.model.dto.MessageHeader)2 MessageTypeEntity (org.finra.herd.model.jpa.MessageTypeEntity)2 IOException (java.io.IOException)1 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)1