Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Email queue implementation with Java

I have a DB whose columns are:

| from_email | to_email | subject | body | processed(Y/N) |

Now I want to implement a system in Java that will access this DB for not processed, send the mail (I'm using fakeSMTP for testing) and set it as processed.

It works fine enough, although I have yet to add any threading.

But the problem is to speed up the procedure it might be deployed over more than one server, in that case how can I ensure no mail gets sent twice?

How can it be made faster?

like image 764
codezero Avatar asked Feb 28 '26 04:02

codezero


1 Answers

The following should be the solution to your problem:

  1. Each server should have a unique id

  2. Add the server_id with default value of null to your mails table

  3. When a server intends to send a mail, run a query, like:

    update mails set server_id = <your server_id> where (server_id is null) and (<your other criteria>)

  4. After you processed the update, select the row and if server_id has the expected value (no concurrency issue occurred), then process it

  5. If a record's server_id is not null, then its processing has been already started. If its server_id is not null and its processed is Y, then it has been processed.

like image 62
Lajos Arpad Avatar answered Mar 01 '26 19:03

Lajos Arpad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!