diff --git a/src/main/java/entgra/mailsender/Controller/MailController.java b/src/main/java/entgra/mailsender/Controller/MailController.java index e00a51d..a85b2c4 100644 --- a/src/main/java/entgra/mailsender/Controller/MailController.java +++ b/src/main/java/entgra/mailsender/Controller/MailController.java @@ -15,6 +15,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.mail.MessagingException; +import javax.naming.ServiceUnavailableException; import java.io.IOException; import java.sql.SQLException; @@ -37,7 +38,7 @@ public class MailController { msg = "Invalid email address"; logger.error(msg, e); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(msg); - } catch (MessagingException | SQLException | IOException exception) { + } catch (MessagingException | SQLException | IOException | ServiceUnavailableException exception) { msg = "Failed to send email"; logger.error(msg, exception); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(msg + exception); diff --git a/src/main/java/entgra/mailsender/DAO/Impl/MailDAOImpl.java b/src/main/java/entgra/mailsender/DAO/Impl/MailDAOImpl.java index 413062f..fa17d3d 100644 --- a/src/main/java/entgra/mailsender/DAO/Impl/MailDAOImpl.java +++ b/src/main/java/entgra/mailsender/DAO/Impl/MailDAOImpl.java @@ -56,7 +56,7 @@ public class MailDAOImpl implements MailDAO { if (mailModel.getAttachment() != null) { stmt.setBytes(4, mailModel.getAttachment().getBytes()); } else { - stmt.setNull(4, Types.BLOB); // Assuming attachment column is of type BLOB + stmt.setNull(4, Types.BLOB); } stmt.setString(5, parametersJson); @@ -85,7 +85,8 @@ public class MailDAOImpl implements MailDAO { @Override public List getUnsentMessages() { - String sql = "SELECT * FROM EMAIL WHERE UUID NOT IN (SELECT UUID FROM SENTEMAIL)";//use expiry date and use left join for get the id + + String sql = "SELECT E.* FROM EMAIL E LEFT JOIN SENTEMAIL SE ON E.UUID = SE.UUID WHERE SE.UUID IS NULL AND (E.EXPIRY_AT IS NULL OR E.EXPIRY_AT > CURRENT_DATE)"; List unsentMails = new ArrayList<>(); @@ -103,7 +104,6 @@ public class MailDAOImpl implements MailDAO { logger.info(e.getMessage()); throw new DatabaseAccessException("Error accessing database", e); } - logger.info(unsentMails.toString()); return unsentMails; } @@ -120,6 +120,7 @@ public class MailDAOImpl implements MailDAO { try (ResultSet rs = stmt.executeQuery()) { while (rs.next()) { mailModel.setEmailId(rs.getInt("EMAIL_ID")); + mailModel.setUuId(rs.getString("UUID")); mailModel.setEmailAddress(rs.getString("EMAIL_ADDRESS")); mailModel.setMsgTemplate(rs.getString("MSG_TEMPLATE")); mailModel.setPriority(rs.getInt("PRIORITY")); diff --git a/src/main/java/entgra/mailsender/Service/Impl/MailServiceImpl.java b/src/main/java/entgra/mailsender/Service/Impl/MailServiceImpl.java index 98c7c59..4d16f35 100644 --- a/src/main/java/entgra/mailsender/Service/Impl/MailServiceImpl.java +++ b/src/main/java/entgra/mailsender/Service/Impl/MailServiceImpl.java @@ -16,6 +16,7 @@ import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.stereotype.Service; +import javax.naming.ServiceUnavailableException; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; @@ -42,11 +43,11 @@ public class MailServiceImpl implements MailService { @Autowired private MailDAO mailDAO; - public void sendEmail(MailModel emailModel) throws MailException, SQLException { + public void sendEmail(MailModel emailModel) throws MailException, SQLException, ServiceUnavailableException { if (shutdownRequested) { String msg = "Server is shutting down. Mail requests will not be accepted."; logger.info(msg); - throw new BadRequestException(msg); + throw new ServiceUnavailableException(msg); } if (isValidEmailAddress(emailModel.getEmailAddress())) { @@ -110,7 +111,7 @@ public class MailServiceImpl implements MailService { } //Store the sent mail to the table - mailDAO.addToSentMail(prioritizedMail.getUuId()); + // mailDAO.addToSentMail(prioritizedMail.getUuId()); }); } @@ -126,6 +127,7 @@ public class MailServiceImpl implements MailService { public void syncMailWithDB() { try { List mailModels = mailDAO.getUnsentMessages(); + if (!mailModels.isEmpty()) { mailQueueService.enqueMails(mailModels); } @@ -136,7 +138,7 @@ public class MailServiceImpl implements MailService { while (PriorityQueueHolder.getInstance().getPriorityQueue().peek() != null) { MailModel prioritizedMail = mailDAO.getMailDetails(mailQueueService.getHighPriorityMail().getUuId()); - logger.info("uuid : " ,prioritizedMail.getUuId()); + javaMailSender.send(mimeMessage -> { @@ -162,8 +164,6 @@ public class MailServiceImpl implements MailService { } - logger.info(prioritizedMail.getUuId()); - //Store the sent mail to the table mailDAO.addToSentMail(prioritizedMail.getUuId()); diff --git a/src/main/java/entgra/mailsender/Service/MailService.java b/src/main/java/entgra/mailsender/Service/MailService.java index 1fdf793..f15b28c 100644 --- a/src/main/java/entgra/mailsender/Service/MailService.java +++ b/src/main/java/entgra/mailsender/Service/MailService.java @@ -5,12 +5,13 @@ import entgra.mailsender.exception.BadRequestException; import entgra.mailsender.exception.MailException; import javax.mail.MessagingException; +import javax.naming.ServiceUnavailableException; import java.io.IOException; import java.sql.SQLException; public interface MailService { - void sendEmail(MailModel emailModel) throws MessagingException, IOException, BadRequestException, MailException, SQLException; + void sendEmail(MailModel emailModel) throws MessagingException, IOException, MailException, SQLException, ServiceUnavailableException; boolean isValidEmailAddress(String email);