Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Bulk Mailing in java  RSS feed

 
Mike Anna
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried to post this before, but it didnt, so posting again.

Hello,

I am maintaining an app here, part of which sends emails.
however, at some point of time when there are too many mails, it is giving exceptions.
Its a class which extends Thread and in its run method is where the sending functionality is called.The params are taken from the classes constructor. I am planning to have a mechanism to queue up the mails and at periodic intervals, send about, say 10, emails.
However, the constructor of this class is being called from a lot of places.
Would appreciate if someone could guide me in how I could go about implementing this.


Cheers..
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What kind of exception?
 
Mike Anna
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply Ulf.

This is the exception :
 
Rob Spoor
Sheriff
Posts: 21087
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems your mail server is not allowing that many emails in such a short time. Perhaps you can build in a small pause after a few hundred messages.
 
Mike Anna
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

It seems your mail server is not allowing that many emails in such a short time. Perhaps you can build in a small pause after a few hundred messages.

Spot on. As I had thought earlier.
So, I cant really have a Thread.sleep(1000), after a counter of say 100 mails.
and, hence posted my main question above.

 
Rob Spoor
Sheriff
Posts: 21087
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm thinking a combination of ScheduledExecutorService combined with a Queue (e.g. ConcurrentLinkedQueue for that wanted thread-safety):

This class will try to send blockSize mails, stopping if no emails are in the queue anymore. Afterwards, it will do nothing for a full second (you can of course change this if you want), then try again. It won't quit until you stop the mail sender; it will then finish its current run and terminate. Of course stopping the mail sender will leave mails in the queue if you stop it too early, but that's also solvable.


With this class, you just add mails, and the scheduler will make sure that the emails are sent in blocks instead of all at the same time. By using a ConcurrentLinkedQueue it should also be thread-safe.
 
Mike Anna
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey thanks Rob, it sure looks like the code-to-my-thoughts
Thanks. I am gonna work on this tomorow and post back what I did.

cheers..
 
Mike Anna
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Rob, thanks for your reply again.
What I am thinking is, instead of burdening it to run based on time frequency, I would like to the task to run at, say when I reach 100 mails in the queue.
I tried looking at the other methods in the ScheduledExecutorService interface, but didnt find anything that will help me schedule things based on a counter. Any ideas ?

cheers..
 
Rob Spoor
Sheriff
Posts: 21087
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Discard the ScheduledExecutorService completely, and let the add method trigger the execution:

I would still use a separate thread (or another Executor) to do the actual sending so the adding thread will not block. The sending code could retrieve everything until the queue is empty, or only retrieve the first 100 mails.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!