[英]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. 我有一个Web应用程序,可以发送电子邮件以涵盖各种功能,就像许多应用程序一样。 It utilises use the JavaMail API to send messages.
它利用JavaMail API来发送消息。
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. 现在,我的应用可能随时都可以关闭,主要是通过停止servlet容器。 It my also go down suddenly if the jvm process is killed or the server is shutdown, or power dies.
如果jvm进程被杀死或服务器关闭或电源中断,它也会突然掉线。
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).
这通常是某种数据库(Derby是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. 关于是否已发送给定的电子邮件,请让真实的SMTP服务器进行实际的传递,然后让您的应用程序发送至该服务器,并考虑是否已接受SMTP服务器,以将其标记为已发送到您的应用程序中。
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.
当然,这意味着您要跟踪要发送的消息,因此可以在必要时重新发送它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.