Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2007

Tip: Looking for answers? Try searching our database.

SMTPTransport chokes with >1 thread, >1 message

Thread view: 
OtisUsenet - 19 Sep 2007 14:01 GMT
Hi,

I've been using the following piece of code for years to send e-mails
from Java apps.  It spawns a new thread for SMTP delivery and works
nicely when I want to send a single message at a time.  Unfortunately,
it looks like this chokes when I want to send multiple messages, each
from its own thread, as something (classloader) tries to load the same
underlying SMTPTransport class more than once.

Here is the error:

attempted  duplicate class definition for name: "com/sun/mail/smtp/
SMTPTransport"
       at java.lang.ClassLoader.defineClass1(Native Method)
       at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
       at
java.security.SecureClassLoader.defineClass(SecureClassLoader.java:
124)
       at java.net.URLClassLoader.defineClass(URLClassLoader.java:
260)
       at java.net.URLClassLoader.access$000(URLClassLoader.java:56)
       at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
       at java.security.AccessController.doPrivileged(Native Method)
       at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
       at org.mortbay.http.ContextLoader.loadClass(ContextLoader.java:
225)
       at org.mortbay.http.ContextLoader.loadClass(ContextLoader.java:
193)
       at javax.mail.Session.getService(Session.java:737)
       at javax.mail.Session.getTransport(Session.java:689)
       ....
       at javax.mail.Transport.send0(Transport.java:154)
       at javax.mail.Transport.send(Transport.java:80)
       at com.foo.util.Mailer$1.run(Mailer.java:336)

And here is the relevant call:

           // Do this asynchronously, in a Thread as sending email is
sometimes too slow
           new Thread() {
               public void run() {
                   Transport.send(mesg);   //  <====== choking entry
point
               }
           }.start();

The obvious "fix" is to do everything in the current thread without
starting a new one, but I'd like to avoid that because of what's in
the comments above - SMTP delivery can be slow, and I'd hate the user
of this interactive app to wait on some slow SMPT server.

Is there any way to avoid this error and send multiple email messages
in parallel from multiple threads?

Thanks.
derek - 19 Sep 2007 14:14 GMT
well anything that could be slow like sending an email or getting data from a database should be done in a background thread. my suggestion would be to put it in a single background thread and just queue up all the emails that must be sent, the background thread can get to them one at a time. you could then also give the users a status of the background thread if needed.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.