Java mail sender sending duplicate emails

I have a problem with JavaMailSender

that sends double or triple messages.

In the controller:

while(size > 0) //# of emails I want to send, for example 5
{
    Item item= itemRepository.findFirstBySentFalseAndValidTrue(); //getting only emails that are not sent!
    item.getData(); // API call that adds some info to the item, takes between 5-15 seconds per item    
    if(item.isValid())
    {

            item.setHeaderName(dataService.setHeader(item.getStore(), LocaleContextHolder.getLocale()));
            item.setAddress(dataService.getAddress(item.getStore(), LocaleContextHolder.getLocale()) + item.getId());

            String email = dataService.getEmail(item.getStore());
            String footer = dataService.getFooter(item.getStore(), LocaleContextHolder.getLocale());

            if(!item.isSent()) //I check if sent here but still get double messages!
            {
                if(mailSenderService.sendEmail(item, "emails/templates/item", email, footer))
                {
                    item.setSent(true);
                }
            }

    itemRepository.save(item);
    size--;
    }
}

      

In my mailbox, send the service class:

    final MimeMessage mimeMessage = this.mailSender.createMimeMessage();
    final MimeMessageHelper message = new MimeMessageHelper(mimeMessage,true, "UTF-8");

    Context ctx =  new Context(LocaleContextHolder.getLocale());
    message.setFrom(email);
    message.setTo("test@gmail.com");

    ResourceBundle labels = ResourceBundle.getBundle("messages", LocaleContextHolder.getLocale());
    mimeMessage.setHeader("Content-Type", encodingOptions);
    mimeMessage.setSubject(labels.getString("email.itme.title"), "UTF-8");


    ctx.setVariable("data", item);
    ctx.setVariable("footer", footer);

    try{        
        String processedTemplate =  templateEngine.process(template, ctx);
        mimeMessage.setContent(processedTemplate, encodingOptions);
        this.mailSender.send(mimeMessage);

    } catch(Exception e){
      e.printStackTrace();
    }

    return true;
}

      

I'm just guessing this is a problem in my email sending class, but maybe the api call is too slow that the method is taking too long to execute and sending duplicate messages? Sometimes when I only send a few emails (like 3) it is ok and it doesn't send duplicates. When I want to send 20 or 30, everything takes about 15 minutes and I get duplicates (I can't control the response time of the API, sometimes it's faster than ever, its really slow). Is there a way to debug it and see which method is called, when and why I am getting these duplicates?

Edit: This is my implementation of itemRepository:

 @Repository
 @Qualifier(value = "itemRepository")
 @Transactional
 public interface itemRepository extends CrudRepository<Item, Long> {

 public Item save(Item item);
 public Item findFirstBySentFalseAndValidTrue();
 }

      

+3


source to share


1 answer


This part:

while(size > 0) //# of emails I want to send, for example 5
{
    Item item= itemRepository.findFirstBySentFalseAndValidTrue(); //getting only emails that are not sent!

      



Doesn't make any sense. You specify a static number of emails you want to send, then never decrease that value , then call one email from the repository that hasn't been sent and do something with it. This is bad design. You are probably getting duplicates because nothing prevents your program from re-fetching the same record with this line of code: Item item= itemRepository.findFirstBySentFalseAndValidTrue();

before it has a chance to update the record.

Instead, you should query your repository for a list of unsent emails that match some criteria, then loop through that returned list to send the email, and then update the entry in the repository.

0


source







All Articles