Message Queue or Scheduler
I am currently using Quartz Scheduler for asynchronous tasks such as sending an email when an exception occurs, sending an email from the web interface, or periodically analyzing traffic.
Should I use a message queue for sending an email? Is it any more efficient or correct to do it that way? The scheduler approach works just fine.
If I use a queue and the email failed to send, is it possible for the queue to retry sending the email at a later time? The queue approach looks simpler than the scheduler for tasks that need to happen immediately, but for scheduler tasks, the scheduler still, unless there is more to the queue than I am aware of.
I have not yet used JMS, so this is what I have开发者_StackOverflow中文版 read.
Walter
A queue would be a more natural choice for sending things like email. Quartz can be shoe-horned into it but it is not a natural fit when you things like retry. A scheduler is most suited to exactly what the name suggests -- tasks that are supposed to occur periodically.
They are really different and it depends on the purpose and frequency you want to send an email. The scheduler generates an event that is time based and then runs some code to send an email. A queue has no way of triggering an event, it needs to have a message put on it from somewhere and then a MessageListener send an email.
To answer your question a queue is a good instrument to send an email if
- The message needs to be put back on the queue if the operation fails, even though SMTP does not know if the email reached it's destination.
- Some trigger can put a message on the queue.
The scheduler can run some java code at a certain interval and therefore generates temporal events. If you want to send periodic emails then the scheduler is the way to go.
If you go with the scheduler then you should have the scheduler put a message on a queue. If not then you need to have some other trigger put a message on the queue.
I agree, with Tom that such asynchronous communication is best done through queue. Which works like a publish-subscriber model following the observer pattern.
精彩评论