简体   繁体   English

使用Java实现电子邮件队列

[英]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. 现在我想用Java实现一个系统,它将访问这个数据库而不进行处理,发送邮件(我使用fakeSMTP进行测试)并将其设置为已处理。

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? 怎么能更快?

The following should be the solution to your problem: 以下应该是您的问题的解决方案:

  1. Each server should have a unique id 每个服务器都应具有唯一的ID

  2. Add the server_id with default value of null to your mails table 将server_id的默认值nullmails表中

  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 处理完update ,选择该行,如果server_id具有预期值(未发生并发问题),则进行处理

  5. If a record's server_id is not null , then its processing has been already started. 如果记录的server_id不为null ,则其处理已经开始。 If its server_id is not null and its processed is Y , then it has been processed. 如果其server_id不为null并且其processedY ,则表明它已被处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM