emportella
emportella

Reputation: 333

Spawn multiple threads from a single EJB @Asynchronous method

I'm building a Java EE application that one of its requirements is to send messages for registered e-mails (around 1000 to 2000). The access to that application is limited and in any time there will be less than 2 user logged in.

For sending e-mails I'm using JavaMail, a @Stateless bean and @Asynchronous method.

My problem is that it takes too long to send the 1000+ e-mails, around 1.2 secs for each e-mail in my development server. What should I do to reduce time ? Can I span multiple Stateless beans? Or in that case creating around 10 to 15 threads, with so low user access isn't a too bad?

Upvotes: 1

Views: 515

Answers (2)

Bill Shannon
Bill Shannon

Reputation: 29971

Your performance problem is probably due to creating a new connection to send each message, as described in the JavaMail FAQ. Your code needs to be able to cache and reuse the connection to the mail server. A better approach for sending the messages asynchronously might be to put the information necessary to construct the message in a JMS message and then use a (pool of) MDB to process the information, turn it into a mail message, and send it, while caching and reusing the Transport object that represents the connection to the server.

Upvotes: 3

durr
durr

Reputation: 1479

You need to configure the async thread pool size inside your container, default size is usually 15 parallel threads. There isn't one thread per bean instance but if the pool fills up there will be a queue and max 15 sending at a time.

Upvotes: 1

Related Questions