PublishNotificationMessagesAdvice extends AbstractServiceAdvice { @Around("serviceMethods()") public Object publishNotificationMessages(ProceedingJoinPoint joinPoint) throws Throwable { MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature(); Method method = methodSignature.getMethod(); boolean publishNotificationMessages = method.isAnnotationPresent(PublishNotificationMessages.class); try { Object returnValue = joinPoint.proceed(); if (publishNotificationMessages) { if (LOGGER.isDebugEnabled()) { Class<?> targetClass = joinPoint.getTarget().getClass(); LOGGER.debug("Method is initiating notification message publishing. javaMethod=\"{}.{}\" notificationMessageInMemoryQueueSize={}", targetClass.getName(), methodSignature.getName(), notificationMessageInMemoryQueue.size()); } while (!notificationMessageInMemoryQueue.isEmpty()) { NotificationMessage notificationMessage = notificationMessageInMemoryQueue.remove(); try { notificationMessagePublishingService.publishNotificationMessage(notificationMessage); } catch (Exception sqsException) { try { notificationMessagePublishingService.addNotificationMessageToDatabaseQueue(notificationMessage); } catch (Exception dbException) { LOGGER .error("Failed to add notification message to the database queue. messageType=\"{}\" messageDestination=\"{}\" messageText={}", notificationMessage.getMessageType(), notificationMessage.getMessageDestination(), notificationMessage.getMessageText(), dbException); } } } } return returnValue; } finally { if (publishNotificationMessages) { notificationMessageInMemoryQueue.clear(); } } }  @Around("serviceMethods()") Object publishNotificationMessages(ProceedingJoinPoint joinPoint);  }
@Test public void testPublishNotificationMessages() throws Throwable { NotificationMessage notificationMessage = new NotificationMessage(MessageTypeEntity.MessageEventTypes.SQS.name(), AWS_SQS_QUEUE_NAME, MESSAGE_TEXT, Collections.singletonList(new MessageHeader(KEY, VALUE))); ProceedingJoinPoint joinPoint = getMockedProceedingJoinPoint("testPublishNotificationMessages"); doCallRealMethod().when(notificationMessageInMemoryQueue).clear(); doCallRealMethod().when(notificationMessageInMemoryQueue).add(notificationMessage); when(notificationMessageInMemoryQueue.isEmpty()).thenCallRealMethod(); doCallRealMethod().when(notificationMessageInMemoryQueue).remove(); notificationMessageInMemoryQueue.clear(); notificationMessageInMemoryQueue.add(notificationMessage); assertFalse(notificationMessageInMemoryQueue.isEmpty()); publishNotificationMessagesAdvice.publishNotificationMessages(joinPoint); verify(notificationMessageInMemoryQueue, times(2)).clear(); verify(notificationMessageInMemoryQueue).add(notificationMessage); verify(notificationMessageInMemoryQueue, times(3)).isEmpty(); verify(notificationMessageInMemoryQueue).remove(); verify(notificationMessagePublishingService).publishNotificationMessage(notificationMessage); verifyNoMoreInteractionsHelper(); assertTrue(notificationMessageInMemoryQueue.isEmpty()); }
@Test public void testPublishNotificationMessagesAmazonServiceException() throws Throwable { NotificationMessage notificationMessage = new NotificationMessage(MessageTypeEntity.MessageEventTypes.SQS.name(), AWS_SQS_QUEUE_NAME, MESSAGE_TEXT, Collections.singletonList(new MessageHeader(KEY, VALUE))); ProceedingJoinPoint joinPoint = getMockedProceedingJoinPoint("testPublishNotificationMessages"); doCallRealMethod().when(notificationMessageInMemoryQueue).clear(); doCallRealMethod().when(notificationMessageInMemoryQueue).add(notificationMessage); when(notificationMessageInMemoryQueue.isEmpty()).thenCallRealMethod(); doCallRealMethod().when(notificationMessageInMemoryQueue).remove(); doThrow(new AmazonServiceException(ERROR_MESSAGE)).when(notificationMessagePublishingService).publishNotificationMessage(notificationMessage); notificationMessageInMemoryQueue.clear(); notificationMessageInMemoryQueue.add(notificationMessage); assertFalse(notificationMessageInMemoryQueue.isEmpty()); publishNotificationMessagesAdvice.publishNotificationMessages(joinPoint); verify(notificationMessageInMemoryQueue, times(2)).clear(); verify(notificationMessageInMemoryQueue).add(notificationMessage); verify(notificationMessageInMemoryQueue, times(3)).isEmpty(); verify(notificationMessageInMemoryQueue).remove(); verify(notificationMessagePublishingService).publishNotificationMessage(notificationMessage); verify(notificationMessagePublishingService).addNotificationMessageToDatabaseQueue(notificationMessage); verifyNoMoreInteractionsHelper(); assertTrue(notificationMessageInMemoryQueue.isEmpty()); }
@Test public void testPublishNotificationMessagesDatabaseException() throws Throwable { NotificationMessage notificationMessage = new NotificationMessage(MessageTypeEntity.MessageEventTypes.SQS.name(), AWS_SQS_QUEUE_NAME, MESSAGE_TEXT, Collections.singletonList(new MessageHeader(KEY, VALUE))); ProceedingJoinPoint joinPoint = getMockedProceedingJoinPoint("testPublishNotificationMessages"); doCallRealMethod().when(notificationMessageInMemoryQueue).clear(); doCallRealMethod().when(notificationMessageInMemoryQueue).add(notificationMessage); when(notificationMessageInMemoryQueue.isEmpty()).thenCallRealMethod(); doCallRealMethod().when(notificationMessageInMemoryQueue).remove(); doThrow(new AmazonServiceException(ERROR_MESSAGE)).when(notificationMessagePublishingService).publishNotificationMessage(notificationMessage); doThrow(new RuntimeException(ERROR_MESSAGE)).when(notificationMessagePublishingService).addNotificationMessageToDatabaseQueue(notificationMessage); notificationMessageInMemoryQueue.clear(); notificationMessageInMemoryQueue.add(notificationMessage); assertFalse(notificationMessageInMemoryQueue.isEmpty()); publishNotificationMessagesAdvice.publishNotificationMessages(joinPoint); verify(notificationMessageInMemoryQueue, times(2)).clear(); verify(notificationMessageInMemoryQueue).add(notificationMessage); verify(notificationMessageInMemoryQueue, times(3)).isEmpty(); verify(notificationMessageInMemoryQueue).remove(); verify(notificationMessagePublishingService).publishNotificationMessage(notificationMessage); verify(notificationMessagePublishingService).addNotificationMessageToDatabaseQueue(notificationMessage); verifyNoMoreInteractionsHelper(); assertTrue(notificationMessageInMemoryQueue.isEmpty()); }
@Test public void testPublishNotificationMessagesDebugEnabled() throws Throwable { LogLevel originalLogLevel = getLogLevel(PublishNotificationMessagesAdvice.class); try { setLogLevel(PublishNotificationMessagesAdvice.class, LogLevel.DEBUG); NotificationMessage notificationMessage = new NotificationMessage(MessageTypeEntity.MessageEventTypes.SQS.name(), AWS_SQS_QUEUE_NAME, MESSAGE_TEXT, Collections.singletonList(new MessageHeader(KEY, VALUE))); ProceedingJoinPoint joinPoint = getMockedProceedingJoinPoint("testPublishNotificationMessages"); doCallRealMethod().when(notificationMessageInMemoryQueue).clear(); doCallRealMethod().when(notificationMessageInMemoryQueue).add(notificationMessage); when(notificationMessageInMemoryQueue.size()).thenCallRealMethod(); when(notificationMessageInMemoryQueue.isEmpty()).thenCallRealMethod(); doCallRealMethod().when(notificationMessageInMemoryQueue).remove(); notificationMessageInMemoryQueue.clear(); notificationMessageInMemoryQueue.add(notificationMessage); assertFalse(notificationMessageInMemoryQueue.isEmpty()); publishNotificationMessagesAdvice.publishNotificationMessages(joinPoint); verify(notificationMessageInMemoryQueue, times(2)).clear(); verify(notificationMessageInMemoryQueue).add(notificationMessage); verify(notificationMessageInMemoryQueue).size(); verify(notificationMessageInMemoryQueue, times(3)).isEmpty(); verify(notificationMessageInMemoryQueue).remove(); verify(notificationMessagePublishingService).publishNotificationMessage(notificationMessage); verifyNoMoreInteractionsHelper(); assertTrue(notificationMessageInMemoryQueue.isEmpty()); } finally { setLogLevel(PublishNotificationMessagesAdvice.class, originalLogLevel); } }