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.