Using JMS or ThreadPool to send email messages

前端 未结 4 762
醉酒成梦
醉酒成梦 2021-02-10 20:56

I will like to know:

I have a scenario. If a user adds a product to the system (I\'m developing), there\'s a listener that sends a notification to the user\'s client bas

4条回答
  •  甜味超标
    2021-02-10 21:09

    I had a similar requirement once, and we used JMS. Then main problem was how to deal with errors because SMTP is indeed not transactional:

    1. is it ok if some email are lost?
    2. is it ok if some email are sent twice?

    We decided it was better to send the message twice, and here is more or less the design we had:

    1. We relied on container managed transaction and if for some reason the email can not be sent, we decided to rollback the JMS transaction; the message would be redelivered later by JMS and an new attempt to send the message was done.

    2. If the JMS message delivery transaction failed after the email was sent (e.g. because of a problem with JMS), the transaction would be rolled back automatically and the message was redelivered later. In this case, the email was sent twice because STMP is not transactional.

    3. Even if the email can be sent (from point of view of code), the SMTP server can still have problem later. In this case, the JMS have been delivered and consumed, so we had no way to know which email had been processed and how to re-send them manually.

    But we were already using JMS. I would not introduce JMS just for that given that the main argument is that JMS is transactional, but SMTP isn't anyway.

    I would go for something lighter -- possibly with a ThreadPool -- and store the state in a database to know which email need to be sent or has been sent. If there are some problem, you can look at the database and take the ad-hoc decisions.

提交回复
热议问题