Monday, October 5, 2009

Schedule a reoccur Task(s) using Java

Going forward(from today!) I am planning to write some technical blogs related to JAVA Technology. First, we might aware of Job Scheduling. I am sure all the projects require some job scheduling. Here is the sample example on Job Scheduling using Java Program:JobSchedule.java has main method and act as a gateway for this Scheduler Task example which will call schedule method in Timer Class(Available from JDK1.3). schedule method will take 3 arguments. First will the scheduled task to be perform and second will be the scheduled task to be called and third will be the scheduled time interval (all are in milli seconds). The scheduled task should extend the TimerTask class and the same needs to be override run method will perform the task.
In the below task whenever JobSchedule.java gets called, after 1 sec PrintTask gets called and you will be getting the output called "Calling PrintTask usinf JAVA Scheduler..". You will be getting the same o/p every 5 secs.

JobSchedule.java
package com.blx.jobSchedule;
import java.io.*;
import java.util.Timer;

public class JobSchedule {
private Timer timer;public JobSchedule() {
timer = new Timer();
}public static void main(String[] args) throws IOException {
JobSchedule jobSchedule = new JobSchedule();
// Test to be performed on schedule
// Inital/start schedule time or when to start
// Fixed time interval or schedular time
jobSchedule.timer.schedule(new PrintTask(), 1000, 5000);}
}


PrintTask. java
package com.blx.jobSchedule;
import java.util.TimerTask;
public class PrintTask extends TimerTask {
public void run() {
System.out.println("Calling PrintTask usinf JAVA Scheduler..");
}
}

No comments:

Pega Decisioning Consultant - Mission Test Quiz & Answers

The Pega Certified Decisioning Consultant (PCDC) certification is for professionals participating in the design and development of a Pega ...