class 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);

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

}