Good morning,
I'm using spring with Scheduled, before using the quartz but as I uploaded the spring version I decided to abandon the quartz since the first option was enough to meet my need.
In short, I have a task that sends emails, this is the event below.
@Configuration
@EnableScheduling
public class NotificacaoScheduler {
Logger logger = LoggerFactory.getLogger(getClass());
@Autowired
private NotificacaoService notificacao;
@Scheduled(fixedRate = 3600000)//1800000
public void scheduleFixedRateTask() {
notificacao.analisar();
logger.debug("Fixed rate task - " + LocalDateTime.now());
notificacao.enviarPendentes();
logger.debug("Send ok - " + LocalDateTime.now());
}
}
Below the service,
@Service
public class NotificacaoService {
Logger logger = LoggerFactory.getLogger(getClass());
@Autowired
private JavaMailSender mailSender;
@Autowired
private Notificacoes notificacao;
@Autowired
private NotificacoesEnvios envios;
@Transactional
public void analisar() {
List<Notificacao> result = notificacao.buscarHoje();
List<NotificacaoEnvio> notEnvios = new ArrayList<>();
for(Notificacao n : result) {
if(!notificacao.verSeExiste(n)) {
NotificacaoEnvio ne = new NotificacaoEnvio();
ne.setNotificacao(n);
ne.setStatus(false);
notEnvios.add(ne);
}
}
if(!notEnvios.isEmpty()) envios.saveAll(notEnvios);
}
@Transactional
public void enviarPendentes() {
List<NotificacaoEnvio> ne = notificacao.notificacoesPendentes();
List<NotificacaoEnvio> nList = new ArrayList<>();
for (NotificacaoEnvio n : ne) {
try {
MimeMessage mail = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mail);
helper.setTo(n.getNotificacao().getPara().replace(" ", "").split(";"));
if(!n.getNotificacao().getCc().trim().equals(""))
helper.setCc(n.getNotificacao().getCc().replace(" ", "").split(";"));
if(!n.getNotificacao().getCo().trim().equals(""))
helper.setBcc(n.getNotificacao().getCo().replace(" ", "").split(";"));
helper.setSubject(n.getNotificacao().getAssunto());
helper.setText(n.getNotificacao().getModelo().getTexto(),true);
helper.setFrom(n.getNotificacao().getDe(),n.getNotificacao().getAutor());
mailSender.send(mail);
n.setDataEnvio(Calendar.getInstance());
n.setStatus(true);
nList.add(n);
} catch (NullPointerException | MailException | MessagingException | UnsupportedEncodingException e) {
logger.error(e.getMessage());
}
}
if(!nList.isEmpty()) envios.saveAll(nList);
}
}
What happens, that I have a notification that is generated in a pending table, that every 1 hour the pending notifications are sent, the problem is right there, the method analyze () is working correctly, ), it is sending the same notification 2 times (there are not two lines of the same record in my bank). During the tests I did in eclipse, only once was it sent, but there on the tomcat server I have this problem.
Below the base query method
@Override
public List<NotificacaoEnvio> notificacoesPendentes(){
Criteria criteria = manager.unwrap(Session.class).createCriteria(NotificacaoEnvio.class);
criteria.add(Restrictions.eq("data", Calendar.getInstance()));
criteria.add(Restrictions.eq("status", false));
return criteria.list();
}
Hugs and Happy New Year to everyone