From 3325789b0f7be536530f477ead49246e48a57074 Mon Sep 17 00:00:00 2001 From: chandrasegar Date: Thu, 15 Feb 2024 10:53:12 +0530 Subject: [PATCH] implemented the clear in the server --- .../mailsender/Service/MailQueueService.java | 3 ++ .../Service/MailQueueServiceImpl.java | 37 +++++++++---------- .../mailsender/Service/MailServiceImpl.java | 21 +++++++++-- 3 files changed, 38 insertions(+), 23 deletions(-) diff --git a/src/main/java/entgra/mailsender/Service/MailQueueService.java b/src/main/java/entgra/mailsender/Service/MailQueueService.java index 95d8a91..c86aabd 100644 --- a/src/main/java/entgra/mailsender/Service/MailQueueService.java +++ b/src/main/java/entgra/mailsender/Service/MailQueueService.java @@ -7,6 +7,9 @@ import java.util.List; public interface MailQueueService { void enqueMails(List mailModelList); + MailModel getHighPriorityMail(); + void clearOnServerShutdown(); + } diff --git a/src/main/java/entgra/mailsender/Service/MailQueueServiceImpl.java b/src/main/java/entgra/mailsender/Service/MailQueueServiceImpl.java index f1f1660..d396f82 100644 --- a/src/main/java/entgra/mailsender/Service/MailQueueServiceImpl.java +++ b/src/main/java/entgra/mailsender/Service/MailQueueServiceImpl.java @@ -21,9 +21,8 @@ public class MailQueueServiceImpl implements MailQueueService{ @Autowired MailDAO mailDAO; -// private volatile Thread -// processingThread; -// private volatile boolean isProcessingMails; + private volatile Thread processingThread; + private volatile boolean isProcessingMails; // private final MailDAO mailDAO; @Override @@ -58,22 +57,22 @@ public class MailQueueServiceImpl implements MailQueueService{ return null; } -// public void clearOnServerShutdown(){ -// BlockingQueue priorityQueue = PriorityQueueHolder.getInstance().getPriorityQueue(); -// if (processingThread != null){ -// while (isProcessingMails){ -// try { -// Thread.sleep(1000); -// }catch (InterruptedException e){ -// Thread.currentThread().interrupt(); -// logger.info("Error waiting for the processing cycle to complete" + e.getMessage()); -// } -// isProcessingMails = priorityQueue.peek() != null; -// } -// processingThread.interrupt(); -// } -// logger.info("Server shutdown"); -// } + public void clearOnServerShutdown(){ + BlockingQueue priorityQueue = PriorityQueueHolder.getInstance().getPriorityQueue(); + if (processingThread != null){ + while (isProcessingMails){ + try { + Thread.sleep(1000); + }catch (InterruptedException e){ + Thread.currentThread().interrupt(); + logger.info("Error waiting for the processing cycle to complete" + e.getMessage()); + } + isProcessingMails = priorityQueue.peek() != null; + } + processingThread.interrupt(); + } + logger.info("Server shutdown"); + } } diff --git a/src/main/java/entgra/mailsender/Service/MailServiceImpl.java b/src/main/java/entgra/mailsender/Service/MailServiceImpl.java index 0d4fcb0..d0b45af 100644 --- a/src/main/java/entgra/mailsender/Service/MailServiceImpl.java +++ b/src/main/java/entgra/mailsender/Service/MailServiceImpl.java @@ -3,27 +3,39 @@ package entgra.mailsender.Service; import entgra.mailsender.DAO.MailDAO; import entgra.mailsender.DTO.MailModel; import entgra.mailsender.util.PriorityQueueHolder; +import jakarta.annotation.PreDestroy; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.stereotype.Service; -import java.util.*; -import java.util.function.Supplier; +import java.util.List; import java.util.logging.Logger; @Service public class MailServiceImpl implements MailService { Logger logger = Logger.getLogger(String.valueOf(MailServiceImpl.class)); + private volatile boolean shutdownRequested = false; + + @Autowired + private MailQueueService mailQueueService; + +// private final mailQueueService mailQueueService; + + + @PreDestroy + public void shutdown() { + shutdownRequested = true; + mailQueueService.clearOnServerShutdown(); + } @Autowired private JavaMailSender javaMailSender; @Autowired private MailDAO mailDAO; - @Autowired - private MailQueueService mailQueueService; + public void sendEmail(MailModel emailModel){ @@ -34,6 +46,7 @@ public class MailServiceImpl implements MailService { } mailDAO.addMail(emailModel); // save the mail details in the database + syncMailWithDB(); //here we need to check the unsent mail details