java 让 javamail 会话传输保持打开状态是否可以接受?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4334027/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it acceptable to leave the javamail session Transport open?
提问by Alastair
My application needs to send emails on an ad hoc basis. I'm using javamail's getDefaultSession and getTransport to send the message, and it's all working as expected.
我的应用程序需要临时发送电子邮件。我正在使用 javamail 的 getDefaultSession 和 getTransport 来发送消息,并且一切都按预期工作。
However I've noticed that the send can take a long time - up to seven seconds per send. If I break the steps down, like this:
但是我注意到发送可能需要很长时间 - 每次发送最多 7 秒。如果我分解步骤,如下所示:
Transport transport = session.getTransport("smtp");
transport.connect();
transport.sendMessage( msg, addresses )
transport.close();
...I can see that it's the connect() call that takes almost all of the time, each time.
...我可以看到几乎每次都需要调用 connect() 。
All of the examples I've found do this - get a transport, connect, send, disconnect. But of course they're all single-shot examples, or sending large batches of emails in a single call.
我发现的所有示例都是这样做的 - 获取传输、连接、发送、断开连接。但当然,它们都是单次示例,或者在一次通话中发送大量电子邮件。
I was thinking I could just leave the connection open, like this:
我在想我可以让连接保持打开状态,如下所示:
Transport transport = session.getTransport("smtp");
if (!transport.isConnected())
transport.connect();
transport.sendMessage( msg, addresses )
(There's a variation on that, here: java mail keeping Transport object connected).
(这里有一个变体:java mail保持传输对象连接)。
I'll have to close it eventually, in a shutdown hook of some kind. And I might have to have a fallback (if the connection has been lost but the transport doesn't realise). But is there any reason not to just leave it open like this during the application lifetime?
我最终将不得不在某种关闭挂钩中关闭它。而且我可能不得不进行后备(如果连接丢失但传输没有意识到)。但是有什么理由不让它在应用程序生命周期内像这样保持打开状态?
Thanks, Alastair
谢谢,阿拉斯泰尔
采纳答案by Tomas Narros
I don't really see any problem in keeping a single SMTP connection open, and the use of Transport Object is recommended for connection reuse (see the JavaMail tutorial).
我认为保持单个 SMTP 连接打开并没有什么问题,建议使用传输对象进行连接重用(请参阅JavaMail 教程)。
Additionally, I would recommend you to keep just one smpt connection (through Transport) open at your application, keeping it at a single manager instance (i.e using a singleton pattern), avoiding the eventual cost of keeping a different connection open for every component which needs send a message.
此外,我建议您在您的应用程序中只保持一个 smpt 连接(通过传输)打开,将它保存在单个管理器实例中(即使用单例模式),避免为每个组件保持不同连接打开的最终成本需要发消息。
回答by Yuriy Nakonechnyy
As per @Bill Shannon
's (author of JavaMail) answer here:
Threadsafety in Javamail
根据@Bill Shannon
's(JavaMail 的作者)在此处回答:
Javamail 中的线程安全
Since a Transport represents a connection to a mail server, and only a single thread can use the connection at a time, a Transport will synchronize access from multiple threads to maintain thread safety, but you'll really only want to use it from a single thread.
由于传输代表到邮件服务器的连接,并且一次只有一个线程可以使用该连接,传输将同步来自多个线程的访问以维护线程安全,但您实际上只想从单个线程使用它线。
So if you plan to use single Transport
instance from multiple threads(which is usually the case nowadays), it's actually quite bad ideafrom performance point of view. You'll probably better off developing some sort of pool of Transport
instances using ThreadLocal
.
因此,如果您打算使用Transport
来自多个线程的单个实例(现在通常是这种情况),从性能的角度来看,这实际上是一个很糟糕的主意。你可能会更好发展某种池的使用情况。Transport
ThreadLocal