views:

2297

answers:

4

I am looking to use quartz to schedule emails, but I'm not sure which approach to take:

  1. Create a new job and trigger whenever an email is scheduled OR
  2. Create a single job, and create a new trigger each time an email is scheduled

I need to pass the message/recipient etc either way, and I'm not sure whether creating heaps of jobs will start adding considerable memory overheads, as there will quite possibly be thousands of emails scheduled.

Update: These emails will be scheduled by users, not by me - so I will be adding these programmatically at runtime, they aren't scheduled to go out at any particular time.

A: 

You might consider queuing or otherwise grouping a set of emails and have a single, or maybe a few, periodic (or scheduled) job(s) that then takes care of the 'batch'.

You could even have the Quartz job queue the emails for a collection of workers to consume and send.

I would not recommend thousands of Quartz jobs/triggers - it just isn't the intended use of the tool (IMHO).


EDIT: In response to the comment below:

I would not recommend thousands of Quartz jobs/triggers when used as part of a framework executing an application in the same JVM, The jobs/triggers will be competing for resources with the rest of the application.

Ken Gentle
"Quartz can be used to create simple or complex schedules for executing tens, hundreds, or even tens-of-thousands of jobs; jobs whose tasks are defined as standard Java components or EJBs." -- Quartz Home Page
erickson
The authors of Quartz do recommend using it for hundreds of thousands of jobs. Twisting tools into roles they weren't designed for is one of my pet peeves, as it always leads to trouble. But in this case, Quartz was expressly designed to scale in this way.
erickson
I think we're closer to agreement than it may appear. Quartz, running on its own can handle as many jobs/triggers as you want to throw at it. When used in an application framework, I don't want it competing with the Web application for resources, so I'd limit the number of jobs/triggers.
Ken Gentle
A: 

Are the triggers to be based on a time schedule? You can use a CronTrigger to set up a more complex time based schedule rather than individual triggers.

Feet
They will be on a time schedule, but these are different emails to different people, so having the trigger fire more than once isn't really going to accomplish much.
RodeoClown
Just read the update. Doesn't that mean that you will have to create multiple triggers anyway (at runtime)? As many users can make different schedules?
Feet
Yep - there will be lots of triggers, but I was wondering if it was better to try and and reduce the number of jobs, so lots of triggers for a single job.
RodeoClown
As long as the job that is being performed is always the same, and the variables can be collected dynamically, yeah, you should be able to.
Feet
+2  A: 
erickson
Hi Erickson, It will be configurable via the trigger's jobDataMap, (which I didn't realise existed), so I'm going to go with this way of doing things.I'll be setting the trigger to keep trying if it fails - can't miss out emails just because another one is being generated at the time...Thanks.
RodeoClown
A: 

I'd recommend one job and one trigger. Put the email requests in a database table and have the quartz job look for new emails to send.

Jon Strayer