Why JMS to send mail by Java Mail

2019-04-12 21:35发布

Scenario 1 :

  1. Setup a JMS Queue in your server
  2. Java code to send Messages to Producer Create a JMS Producer, which when invoked, should receive the email data (subject, body, to , cc etc) and post it to the Queue setup in step 1
  3. Create a JMS Consumer, which subscribes to the Queue created in Step 1, and its onMessage should call the JavaMail API to send the email.

Scenario 2 :

  • Directly call the JavaMail API to send the email.

I know about how to use and what JMS and Java Mail are doing.Thing is why we have to go from Scenario 2 to Scenario 1 for sending mails.Initially we did Scenario 2.Now we are using Scenario 1.From Different parts of the Big Application are sending mails so we use JMS Queue ,there will be Consumer of Queue from there sending mails.Please help me to understand.

3条回答
beautiful°
2楼-- · 2019-04-12 21:50

You would use this mechanism in a large application for 2 reasons:

1) You don't want your clients to have to wait for the mail to be sent.

2) You don't want to lose mails if you lose connectivity to your mail server for any reason.

查看更多
Melony?
3楼-- · 2019-04-12 22:06

Besides:

  1. the conversation with the mail provider (SMTP und POP3) is asynchronous and close to the JMS/MDB api. So why should i use a different API than JMS ?
  2. You can keep the mail handling in one transaction, together with some database changes other activities. I remember too many Spring .. sic' projects, where the customer demmands for a atomic operation, that included a state change in a db ;-)
  3. Image, the messages you send become more compulsory and you have to connect to a X400 service. Simply think of the slight code change (and the change of the RA) and you will discover to met the right architectual descision.
查看更多
祖国的老花朵
4楼-- · 2019-04-12 22:11

You would do this if you don't have a relyable MTA near your local machine but need to be sure your mail will be send. For example if there is a network outage but you rely on Java Mail to send your mail without additional logic, your mail will not be send at all.

Using JMS you can reschedule the mail for transfer as soon as the real MTA will become available again.

查看更多
登录 后发表回答