SMTPTransport chokes with >1 thread, >1 message

O

OtisUsenet

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.
 
D

derek

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top