Scheduled Message Scheduled with cron send three times

3

Hi! I have a routine in a JSF + Spring 3.0.5 system that sends an email containing 4 attachments in pdfs every day 11 at 7:01 AM to 11 addresses. My problem is that when the application is in production on a 32-bit Windows Server 2003 server, 3 messages are sent to each e-mail address. On the other hand, when the application is in a development environment, only one message is sent to each contact. What should happen in production.

This routine was implemented with Spring as follows:

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class EnviarEmailService {

    @SuppressWarnings("unused")
    // second, minute, hour, day of month, month, day(s) of week
    @Scheduled(cron = "30 01 07 11 * *")
    private void job() {
      try{
        enviarEmail();
      }catch(Exception e){
        e.printStackTrace();
      }
    }
}

SendEmail method:

public void enviarEmail() {
    try {

        ResourceBundle resource = ResourceBundle.getBundle("configuracao",Locale.getDefault());

        String corpoEmail = "<META HTTP-EQUIV='Content-Type' CONTENT='text/html; charset=ISO-8859-1'>"
                + resource.getString("mensagem").toString();
        corpoEmail = corpoEmail + getCorpoEmailAux().toString();

        List<String> destinatarios = new ArrayList<String>();

        String[] destinos = resource.getString("destinatarios").toString().split(";");

        for (String email : destinos) {
            destinatarios.add(email);
        }

        List<String> anexos = new ArrayList<String>();

        anexos.add(medido.getDiretorioReal("/pdf/medido.pdf"));

        anexos.add(vencido.getDiretorioReal("/pdf/vencido.pdf"));

        anexos.add(inconsiste.getDiretorioReal("/pdf/inconsiste.pdf"));

        anexos.add(recorrente.getDiretorioReal("/pdf/recorrente.pdf"));

        Email email = new Email("smtp", 
                "mail.cogerh.com.br", 
                "587",
                "true", 
                resource.getString("remetente").toString(),
                resource.getString("senha").toString(), 
                "ISO-8859-1",
                Email.TIPO_TEXTO_HTML, 
                resource.getString("remetente_nome").toString(), 
                "true", "*");
        email.conectar();

        email.prepararEmail(resource.getString("assunto").toString(), corpoEmail, destinatarios, anexos);
        email.enviar();
    } catch (Exception e) {
        // TODO: handle exception
    }

Auxiliary methods:

public void prepararEmail(String assunto, String corpoEmail,
            List<String> destinatarios, List<String> anexos) {

        this.assunto = assunto;
        this.corpoEmail = corpoEmail;
        this.destinatarios = destinatarios;
        this.anexos = anexos;

        try {
            mensagem = new MimeMessage(session);
            mensagem.setFrom(new InternetAddress(getUsuario(), getNomeUsuario()));
            mensagem.setSubject(getAssunto());
            mensagem.setSentDate(new Date());

            if (destinatarios != null && !destinatarios.isEmpty()) {
                for (String destinatario : destinatarios) {

                        mensagem.addRecipient(Message.RecipientType.TO, new InternetAddress(destinatario, destinatario));

                }
            }

            textoMensagemCompleta = new MimeBodyPart();
            textoMensagemCompleta.setContent(getCorpoEmail(), getTipoTextoEmail() + "\"ISO-8859-1\"");
            parteMensagemCompleta = new MimeMultipart();
            parteMensagemCompleta.addBodyPart(textoMensagemCompleta);
            if (anexos != null & !anexos.isEmpty()) {
                for (int index = 0; index < anexos.size(); index++) {
                    MimeBodyPart tempMimeBodyPart = new MimeBodyPart();
                    FileDataSource fds = new FileDataSource(anexos.get(index).toString());
                    tempMimeBodyPart.setDataHandler(new DataHandler(fds));
                    tempMimeBodyPart.setFileName(fds.getName());
                    parteMensagemCompleta.addBodyPart(tempMimeBodyPart);
                }
            }

            mensagem.setContent(parteMensagemCompleta);

        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (MessagingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    public void enviar() {
        try {
            Transport.send(mensagem);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

The strategy I thought to fix this problem was:

  • Add a table in the "monthly sent already" database linked to the month and year that would bar a second or third send.
  

(It did not work because they were inserted at the same time, as if   there were 3 threads inserting this control at the same time into the database).

If you've been through this can you give me a help? Thank you!

    
asked by anonymous 11.05.2018 / 15:14

0 answers