Compare commits

...

2 Commits

@ -15,6 +15,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import javax.mail.MessagingException; import javax.mail.MessagingException;
import javax.naming.ServiceUnavailableException;
import java.io.IOException; import java.io.IOException;
import java.sql.SQLException; import java.sql.SQLException;
@ -37,7 +38,7 @@ public class MailController {
msg = "Invalid email address"; msg = "Invalid email address";
logger.error(msg, e); logger.error(msg, e);
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(msg); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(msg);
} catch (MessagingException | SQLException | IOException exception) { } catch (MessagingException | SQLException | IOException | ServiceUnavailableException exception) {
msg = "Failed to send email"; msg = "Failed to send email";
logger.error(msg, exception); logger.error(msg, exception);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(msg + exception); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(msg + exception);

@ -56,7 +56,7 @@ public class MailDAOImpl implements MailDAO {
if (mailModel.getAttachment() != null) { if (mailModel.getAttachment() != null) {
stmt.setBytes(4, mailModel.getAttachment().getBytes()); stmt.setBytes(4, mailModel.getAttachment().getBytes());
} else { } else {
stmt.setNull(4, Types.BLOB); // Assuming attachment column is of type BLOB stmt.setNull(4, Types.BLOB);
} }
stmt.setString(5, parametersJson); stmt.setString(5, parametersJson);
@ -85,7 +85,8 @@ public class MailDAOImpl implements MailDAO {
@Override @Override
public List<MailModel> getUnsentMessages() { public List<MailModel> 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<MailModel> unsentMails = new ArrayList<>(); List<MailModel> unsentMails = new ArrayList<>();
@ -103,7 +104,6 @@ public class MailDAOImpl implements MailDAO {
logger.info(e.getMessage()); logger.info(e.getMessage());
throw new DatabaseAccessException("Error accessing database", e); throw new DatabaseAccessException("Error accessing database", e);
} }
logger.info(unsentMails.toString());
return unsentMails; return unsentMails;
} }
@ -120,6 +120,7 @@ public class MailDAOImpl implements MailDAO {
try (ResultSet rs = stmt.executeQuery()) { try (ResultSet rs = stmt.executeQuery()) {
while (rs.next()) { while (rs.next()) {
mailModel.setEmailId(rs.getInt("EMAIL_ID")); mailModel.setEmailId(rs.getInt("EMAIL_ID"));
mailModel.setUuId(rs.getString("UUID"));
mailModel.setEmailAddress(rs.getString("EMAIL_ADDRESS")); mailModel.setEmailAddress(rs.getString("EMAIL_ADDRESS"));
mailModel.setMsgTemplate(rs.getString("MSG_TEMPLATE")); mailModel.setMsgTemplate(rs.getString("MSG_TEMPLATE"));
mailModel.setPriority(rs.getInt("PRIORITY")); mailModel.setPriority(rs.getInt("PRIORITY"));

@ -16,6 +16,7 @@ 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 javax.naming.ServiceUnavailableException;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -42,11 +43,11 @@ public class MailServiceImpl implements MailService {
@Autowired @Autowired
private MailDAO mailDAO; private MailDAO mailDAO;
public void sendEmail(MailModel emailModel) throws MailException, SQLException { public void sendEmail(MailModel emailModel) throws MailException, SQLException, ServiceUnavailableException {
if (shutdownRequested) { if (shutdownRequested) {
String msg = "Server is shutting down. Mail requests will not be accepted."; String msg = "Server is shutting down. Mail requests will not be accepted.";
logger.info(msg); logger.info(msg);
throw new BadRequestException(msg); throw new ServiceUnavailableException(msg);
} }
if (isValidEmailAddress(emailModel.getEmailAddress())) { if (isValidEmailAddress(emailModel.getEmailAddress())) {
@ -110,7 +111,7 @@ public class MailServiceImpl implements MailService {
} }
//Store the sent mail to the table //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() { public void syncMailWithDB() {
try { try {
List<MailModel> mailModels = mailDAO.getUnsentMessages(); List<MailModel> mailModels = mailDAO.getUnsentMessages();
if (!mailModels.isEmpty()) { if (!mailModels.isEmpty()) {
mailQueueService.enqueMails(mailModels); mailQueueService.enqueMails(mailModels);
} }
@ -136,7 +138,7 @@ public class MailServiceImpl implements MailService {
while (PriorityQueueHolder.getInstance().getPriorityQueue().peek() != null) { while (PriorityQueueHolder.getInstance().getPriorityQueue().peek() != null) {
MailModel prioritizedMail = mailDAO.getMailDetails(mailQueueService.getHighPriorityMail().getUuId()); MailModel prioritizedMail = mailDAO.getMailDetails(mailQueueService.getHighPriorityMail().getUuId());
logger.info("uuid : " ,prioritizedMail.getUuId());
javaMailSender.send(mimeMessage -> { javaMailSender.send(mimeMessage -> {
@ -162,8 +164,6 @@ public class MailServiceImpl implements MailService {
} }
logger.info(prioritizedMail.getUuId());
//Store the sent mail to the table //Store the sent mail to the table
mailDAO.addToSentMail(prioritizedMail.getUuId()); mailDAO.addToSentMail(prioritizedMail.getUuId());

@ -5,12 +5,13 @@ import entgra.mailsender.exception.BadRequestException;
import entgra.mailsender.exception.MailException; import entgra.mailsender.exception.MailException;
import javax.mail.MessagingException; import javax.mail.MessagingException;
import javax.naming.ServiceUnavailableException;
import java.io.IOException; import java.io.IOException;
import java.sql.SQLException; import java.sql.SQLException;
public interface MailService { 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); boolean isValidEmailAddress(String email);

Loading…
Cancel
Save