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 {
void enqueMails(List<MailModel> mailModelList);
MailModel getHighPriorityMail();
void clearOnServerShutdown();
}

@ -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<MailModel> 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<MailModel> 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");
}
}

@ -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

Loading…
Cancel
Save