Win a copy of Pragmatic AI this week in the Artificial Intelligence forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

java mail - session cache?  RSS feed

 
Ranch Hand
Posts: 35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in general when we want to send a java mail we make a new java mail session

something like :-



This works fine. My question is for an enterprise project can we cache the mailSession object?

does it have a time out?

is it wise to cache it?

If cached - is there some function to call every time sending to see if it needs to re-initialized?
 
Bartender
Posts: 2292
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's slide this over to the Other JSE/JEE APIs forum!
 
Sheriff
Posts: 23647
49
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If by "an enterprise project" you are referring to a web application, then yes, that's the normal thing to do. All of them (Tomcat, Websphere, etc) have a way to configure a mail session which can be accessed via JNDI.
 
Tushar Kapila
Ranch Hand
Posts: 35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeap i mean a web app with a lot of tx that need emails to be sent out ...

do i need to put it in jndi?

i have a class that sends emails now - cant i just put in as a static member of this class?

and more importantly - once we get the session from JNDI/static member - do we ever need to re init if its the same host/port as before?
 
Paul Clapham
Sheriff
Posts: 23647
49
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't have to use any of the features of the container if you think you can reimplement them in your own code. Just be aware that the container code will have been written to take care of annoying details like having to deal with simultaneous access from multiple threads and reconnection after failures and all those other issues that you didn't acknowledge in your post.

Actually, sorry, you do acknowledge some of those issues:

Tushar Kapila wrote:and more importantly - once we get the session from JNDI/static member - do we ever need to re init if its the same host/port as before?



If you get a session from JNDI you don't need to deal with any of those issues. Unless you were proposing to keep a long-running reference to the session, of course. But don't do that. If you want to send an e-mail, then get a session from JNDI and use that to send the e-mail. Don't keep a reference to that session.

If you roll your own session, then I don't know.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!