Merge pull request 'developed clear on shutdown' (#2) from develop into master

Reviewed-on: #2
pull/7/head
commit 4760e3edc7

@ -7,6 +7,9 @@ import java.util.List;
public interface MailQueueService { public interface MailQueueService {
void enqueMails(List<MailModel> mailModelList); void enqueMails(List<MailModel> mailModelList);
MailModel getHighPriorityMail(); MailModel getHighPriorityMail();
void clearOnServerShutdown();
} }

@ -21,9 +21,8 @@ public class MailQueueServiceImpl implements MailQueueService{
@Autowired @Autowired
MailDAO mailDAO; MailDAO mailDAO;
// private volatile Thread private volatile Thread processingThread;
// processingThread; private volatile boolean isProcessingMails;
// private volatile boolean isProcessingMails;
// private final MailDAO mailDAO; // private final MailDAO mailDAO;
@Override @Override
@ -58,22 +57,22 @@ public class MailQueueServiceImpl implements MailQueueService{
return null; return null;
} }
// public void clearOnServerShutdown(){ public void clearOnServerShutdown(){
// BlockingQueue<MailModel> priorityQueue = PriorityQueueHolder.getInstance().getPriorityQueue(); BlockingQueue<MailModel> priorityQueue = PriorityQueueHolder.getInstance().getPriorityQueue();
// if (processingThread != null){ if (processingThread != null){
// while (isProcessingMails){ while (isProcessingMails){
// try { try {
// Thread.sleep(1000); Thread.sleep(1000);
// }catch (InterruptedException e){ }catch (InterruptedException e){
// Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
// logger.info("Error waiting for the processing cycle to complete" + e.getMessage()); logger.info("Error waiting for the processing cycle to complete" + e.getMessage());
// } }
// isProcessingMails = priorityQueue.peek() != null; isProcessingMails = priorityQueue.peek() != null;
// } }
// processingThread.interrupt(); processingThread.interrupt();
// } }
// logger.info("Server shutdown"); logger.info("Server shutdown");
// } }
} }

@ -3,27 +3,39 @@ package entgra.mailsender.Service;
import entgra.mailsender.DAO.MailDAO; import entgra.mailsender.DAO.MailDAO;
import entgra.mailsender.DTO.MailModel; import entgra.mailsender.DTO.MailModel;
import entgra.mailsender.util.PriorityQueueHolder; import entgra.mailsender.util.PriorityQueueHolder;
import jakarta.annotation.PreDestroy;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.*; import java.util.List;
import java.util.function.Supplier;
import java.util.logging.Logger; import java.util.logging.Logger;
@Service @Service
public class MailServiceImpl implements MailService { public class MailServiceImpl implements MailService {
Logger logger = Logger.getLogger(String.valueOf(MailServiceImpl.class)); 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 @Autowired
private JavaMailSender javaMailSender; private JavaMailSender javaMailSender;
@Autowired @Autowired
private MailDAO mailDAO; private MailDAO mailDAO;
@Autowired
private MailQueueService mailQueueService;
public void sendEmail(MailModel emailModel){ public void sendEmail(MailModel emailModel){
@ -34,6 +46,7 @@ public class MailServiceImpl implements MailService {
} }
mailDAO.addMail(emailModel); // save the mail details in the database mailDAO.addMail(emailModel); // save the mail details in the database
syncMailWithDB();
//here we need to check the unsent mail details //here we need to check the unsent mail details

Loading…
Cancel
Save