java - Is it acceptable to leave the javamail session Transport open? -


my application needs send emails on ad hoc basis. i'm using javamail's getdefaultsession , gettransport send message, , it's working expected.

however i've noticed send can take long time - 7 seconds per send. if break steps down, this:

transport transport = session.gettransport("smtp"); transport.connect(); transport.sendmessage( msg, addresses ) transport.close(); 

...i can see it's connect() call takes of time, each time.

all of examples i've found - transport, connect, send, disconnect. of course they're single-shot examples, or sending large batches of emails in single call.

i thinking leave connection open, this:

transport transport = session.gettransport("smtp"); if (!transport.isconnected())     transport.connect(); transport.sendmessage( msg, addresses ) 

(there's variation on that, here: java mail keeping transport object connected).

i'll have close eventually, in shutdown hook of kind. , might have have fallback (if connection has been lost transport doesn't realise). there reason not leave open during application lifetime?

thanks, alastair

i don't see problem in keeping single smtp connection open, , use of transport object recommended connection reuse (see javamail tutorial).

additionally, recommend keep 1 smpt connection (through transport) open @ application, keeping @ single manager instance (i.e using singleton pattern), avoiding eventual cost of keeping different connection open every component needs send message.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -