Please click on any Advertisement, if you like this blog.

Monday, February 28, 2011

Java job scheduling

Other Links: Job Scheduling with java.util.Timer
                    Job Scheduling with Quartz Framework and JSP - Servlet
                    Job Scheduling with Quartz Framework and Java Spring Framework


Java job scheduling:

Everybody, both bachelor and married, requires some reminding tools that may keep them safe from deadly mental torture from their girl friend or from their wives by reminding them worst dates like girl friend’s or wife’s birthday, wedding anniversary date and also several other things like when and what to do etc.
Today, we use mobile phone or several other things for this purpose but have we thought about the fact that how these devices remind us and what logic or program gives these information to the devices that present to us. As I used to bath in the ocean of Java, I am going to explain the above aspect in terms of Java.
Java provides 2 classes for handling the job scheduling task.  The term “Job” is used to explain what to be done and scheduling means defining the appropriate period or time at when this job will be fired and the task defined in this job will be performed. The corresponding java classes are:

1)      java.util.Timer
2)      java.util.TimerTask

Lets see how can we make ourselves safe from extreme mental torture from our girlfriends just by following java classes:

package net.nighttale.scheduling;

import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class ReportGenerator extends TimerTask {

  public void run() {
    System.out.println("Happy birthday");
    //put things here that we used to tell a lie to make our girlfriend happy. You can write here code for email and any other things.
  }

}


Please click on any advertisement if you like this blog.

class MainApplication {

  public static void main(String[] args) {
    Timer timer  new Timer();
    Calendar date = Calendar.getInstance();
    date.set(
      Calendar.DAY_OF_WEEK,
      Calendar.SUNDAY
    );
    date.set(Calendar.HOUR, 0);
    date.set(Calendar.MINUTE, 0);
    date.set(Calendar.SECOND, 0);
    date.set(Calendar.MILLISECOND, 0);
    // Schedule to run every Sunday in midnight
    timer.schedule(
      new ReportGenerator(),
      date.getTime(),
      1000 * 60 * 60 * 24 * 7
    );
  }
}

The above code will run the code written in the run() method on every Sunday at midnight. In the above example, the class ReportGenerator is the worker class as it does the user defined task and it must extends TimerTask which implements java.lang.Runnable. All that you need to do is to override run() method with the report generation code.
Then, we schedule this object's execution using one of the Timer's scheduling methods. In this case, we use the schedule() method that accepts the date of the first execution and the period of subsequent executions in milliseconds (since we repeat this report generation every week).

So gyes isn’t it very simple to make our financial-budget’s enemy( our girlfriend) happy by sending them birth day wish email on time. But gyes  don’t be fully dependent on the above methods, because this method has several limitations like:
1)      It can not resume its process again if it fails at scheduled time for any reason.
2)      It only shows at what intervals the job should be executed but it can not define the exact time of executing the job.

The above problems can be solved if we use certain 3rd party framework for java job scheduling. There are several 3rd party job scheduler are available in market but we will study the Quarts Framework in our next blog.

Please click on any advertisement if you like this blog.

No comments:

Post a Comment

Please provide your precious comments and suggestion