com.xlrit.gears.plugin.mail.MailBatchSender Maven / Gradle / Ivy
The newest version!
package com.xlrit.gears.plugin.mail;
import java.time.OffsetDateTime;
import java.util.*;
import java.util.stream.Collectors;
import com.xlrit.gears.base.execution.Execution;
import com.xlrit.gears.base.schedule.ScheduledRunnable;
import com.xlrit.gears.base.model.Mail;
import com.xlrit.gears.base.repository.MailRepository;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.mail.MailAuthenticationException;
import org.springframework.mail.MailSendException;
import org.springframework.stereotype.Component;
import static com.xlrit.gears.plugin.mail.MailHelper.HEADER_MAIL_ID;
@Component
@RequiredArgsConstructor
public class MailBatchSender extends ScheduledRunnable {
private static final Logger LOG = LoggerFactory.getLogger(MailBatchSender.class);
private final MailProperties mailProperties;
private final MailRepository mailRepository;
private final MailHelper mailHelper;
@Override
protected boolean isEnabled() {
return mailProperties.isEnabled(MailProperties.Mode.BATCH);
}
@Override
protected String getCronExpression() {
return mailProperties.getCron();
}
@Override
protected void runScheduled() {
LOG.debug("Running now...");
try {
while (true) {
List mails = mailRepository.getMailsToSend(mailProperties.getBatchSize());
LOG.debug("Mails to send: {}", mails.size());
if (mails.isEmpty()) break;
sendMails(mails);
}
}
catch (MailAuthenticationException e) {
LOG.error("Unable to send mails due to authentication failure: " + e.getMessage(), e);
}
}
private void sendMails(List mails) {
MimeMessage[] messages = mails.stream()
.map(mailHelper::createMessage)
.filter(Objects::nonNull)
.toArray(MimeMessage[]::new);
OffsetDateTime now = OffsetDateTime.now();
SendResult result = doSendMails(messages);
LOG.info("{} mails sent; {} failures", result.getSuccesfull().size(), result.getFailed().size());
Set failedIds = result.getFailedIds();
for (Mail mail : mails) {
if (failedIds.contains(mail.getId())) {
mail.setAttempts(mail.getAttempts() + 1);
mail.setLastAttempt(now);
}
else {
mailRepository.remove(mail, Execution.NONE);
}
}
}
private SendResult doSendMails(MimeMessage[] messages) {
try {
mailHelper.send(messages);
return new SendResult(List.of(messages), List.of());
}
catch (MailSendException e) {
Map