1

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?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
emportella
  • 323
  • 4
  • 9
  • Are you using an ApplicationServer, e.g. WildFly, or Glassfish? – K.Nicholas Mar 16 '16 at 15:24
  • I'm using Glassfish. Nicholas. Gimby you are right it's a JEE question. And 1.2 secs is for each e-mail of the 1000+ to send in the end it takes half a hour to send all that I have now. I edited that. Thanks. – emportella Mar 16 '16 at 16:01
  • @Gimby: 1.2 seconds to **long**??? ;-) – Kukeltje Mar 16 '16 at 16:04
  • Kukeltje 1.2 secs times 1000 = 1200 secs to send 1000 emails Around 20 minutes. – emportella Mar 16 '16 at 16:13
  • I don't think that the performance problem is anything to do with code or tech in any case, that sounds more like an environment/network thing. I wouldn't spend a good amount of time trying to redesign code before you've got proof it is a code problem. – Gimby Mar 16 '16 at 16:19
  • Gimpy I don't think it's a code problem either. I just want to make the sending of emails faster. So what would be best? Use one Stateless bean spanning @Asynchronous methods? or What? – emportella Mar 16 '16 at 17:22

2 Answers2

3

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.

Bill Shannon
  • 29,579
  • 6
  • 38
  • 40
  • I'm already using this approach of Transport t = session.getTransport("smtp"); And it lowered the time for what I get now. – emportella Mar 16 '16 at 17:45
1

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.

highstakes
  • 1,499
  • 9
  • 14