简体   繁体   中英

How can a Java application know what emails were sent, in the event of an improper shutdown

I have a web application that sends off emails to cover various functionality, just like a lot of apps do. It utilises use the JavaMail API to send messages.

Some emails are sent off in response to user events (submitting forms etc) and some are sent off asynchronously during the lifecycle of the app.

Now my app maybe shutdown at any point, mostly by stopping a servlet container. It my also go down suddenly if the jvm process is killed or the server is shutdown, or power dies.

My problem is the case where it goes down while attempting to send an email. After restarting the app, How could it know that an email was sent, so it avoids sending duplicates, or that it wasn't, so it can resend?

Any help, thoughts and suggestions greatly appreciated.

Thanks

You will need to have a mechanism that can say reliably what needs to be done from here. This is frequently a database of some kind (Derby is an embedded database in Java).

Regarding if a given email has been sent, Have a real SMTP server doing the actual delivery, and just let your application send to that and consider the acceptance of the SMTP server enough to mark it sent in your application.

Mail isn't transactional, so you need to decide what failures you need to handle and whether it's acceptable to duplicate messages in certain failure cases.

Once you give the message to the mail server to send, it's possible for many things to fail even though the mail server accepted the message and will eventually send it. It's almost impossible to know after a failure whether the mail server accepted the message or not. The simplest approach is to resend the message in that case and hope that failures are rare enough that it won't matter. Of course, that implies that you keep track of which messages you intend to send, so you can resend them if necessary.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM