1

I am creating GUI interface in asp.net in which users will specify a time to schedule and a job which will run at sometime in the future.

The job in my application is an instance of a Person class (given below). At a specified time, I want to run the "submit()" method of the created person instance.

e.g.

public class Person
{
  string User {get;set;}
  public void Submit()
  {
     //create a text file with User entry

  }
}

Now in MS SQL, I have a Jobs table. Jobs table has ScheduledTime (DateTime), JobDetail (XML) columns (where JobDetail column stores serialized version of Person object e.g. <Person User="TestUser"></Person>, Scheduled time: 2/2/10 12:12:11

I am aware that Quartz.Net can run jobs at their scheduled time. But I don't know how to use Quartz.Net with Spring.Net to pick up the jobs from DB and run them at their scheduled time?

Is it possible? If so can you please guide?

InfoLearner
  • 14,952
  • 20
  • 76
  • 124

1 Answers1

2

There are two separate components you'll need to implement. The trigger and something that polls for jobs to run. Based on your table structure so far it sounds like you want jobs that only run once and if not you'll need to add a cron expression column and create CronTrigger objects.

For polling for jobs to run you can find details on how to use Spring timer here: http://static.springsource.org/spring/docs/1.2.9/reference/scheduling.html

An alternative to Spring is implementing your poller in the database. For example, creating an SQL Server schedule job, the approach we went with. The downside is that you need to design it to pickup jobs missed when the database server is down.

Database approach details

This design assumes only run once schedules (to add recurring you need to add a cron translation step) and it assumes that the work the schedule does can get kicked off by the database. Spring is a better solution if you can only do that programatically.

  1. add a column 'processed' to your jobs table
  2. create a stored proc that runs every minute (or whatever)
  3. that procedure looks for any jobs in the past not marked as processed, kicks them off and then marks them as done

Related:

How do I configure Quartz.NET host with Spring.NET

Running a Job only once Using Quartz

Community
  • 1
  • 1
Chip McCormick
  • 744
  • 4
  • 17
  • I still don't understand how I will make quartz.net poll a DB and trigger when scheduled time == current time? – InfoLearner Sep 05 '11 at 21:13
  • Updated main answer with more details. – Chip McCormick Sep 06 '11 at 11:15
  • Quartz doesn't do the polling, it is merely an API for creating schedules and triggers. – Chip McCormick Sep 06 '11 at 11:23
  • but how can stored proc call a method in my .net app with the parameters that users defined and i stored in my table? – InfoLearner Sep 07 '11 at 21:44
  • We have both web and backend code (an app that runs jobs offline) so we can use a trigger to poll for jobs and then call code. Since you need to do all of your work in a webapp, you should be able to use a similar approach: setup the Spring (or whatever) timer to periodically call getNextFireTime on each trigger, run the submit method you have above and then mark the schedule as run (with an extra bit in your table). – Chip McCormick Sep 07 '11 at 23:41
  • Rereading your question it sounds like you're using serialization because you don't have ORM, which would allow you to map that table to a Schedule and Trigger. Without ORM you'll need to write some database to object code to translate records into Quartz objects. At that point I'm not sure what you gain from using Quartz...a stored proc called periodically from code to decide when to run the job and then calling Person.Submit() may be good enough. Quartz is most useful when you have fancier scheduling needs than 'run this job once'. – Chip McCormick Sep 07 '11 at 23:49
  • thanks so far but i am still unsure what you're suggesting. The serialized form of person object is inserted into db using Spring's NHibernate. Think about it this way, i want to run certain PersonJobs on daily basis. Each person instance is different because it has different username. I store all of the scheduled person jobs in DB. I also now have a cron expression for each person instance job that i store in db, which I serialize and embed in person xml msg. my question is... in spring.net, how do i create a trigger for the person job that i have just inserted into db? how will i link them? – InfoLearner Sep 08 '11 at 23:20
  • should i make person class inherit a quartz.net interface? should i write some c# code as post-insert db step to manually create a cron trigger. how do i get the parameters for the person object when i execute its Submit() method. – InfoLearner Sep 08 '11 at 23:22
  • also when cron time is reached, who will trigger the run? jobs are stored in db? how will they be picked up. sorry but your answers are not helping at all... i want to use spring.net quartz.net and not stored proc or windows timer or any other utility. will be glad if u can help but only if you can geniuely help and clear the concepts. – InfoLearner Sep 08 '11 at 23:26
  • i am serializing the person object because the jobstable can contain scheduled job for any of the entities in my solution. its not just for person class. its for any class. but anyways, thats not even relevant. the point is, who will tell my .net code that "ok you inserted a job in db and its time is now. lets get the object, deserialize it, get the parameters which were defined in the serialized xml msg and run its submit method" – InfoLearner Sep 08 '11 at 23:30
  • A late answer: use Spring scheduling to trigger the method you want to run. Spring supports Quartz.NET: http://www.springframework.net/docs/1.2.0/reference/html/scheduling.html – Chip McCormick Jan 05 '12 at 20:13