In some kind of situation we may need to
To do this kind of facility we normally use Thread and Synchronization concept .
But some additional plugin like Quartz offering us to implement this facility in very easy manner
Quartz
Quartz normally additional plugin to java, which is used to schedule a task.And this provides to implement the custom time configuration at which time the user program needs to execute.
I used this plugin which is very working like charm .
Configuration :
Step 1: Download the latest jar file from Quartz official site , and configure it to your application libraries
Step 2: Two types of Scheduler is available in Quartz
CronTrigger is used to define At which time the program need to execute, it is very reliable one to know about this more here
We will use SimpleTrigger here
- Execute a program continuesly in a schedule time
- Execute a program in prefer time
To do this kind of facility we normally use Thread and Synchronization concept .
But some additional plugin like Quartz offering us to implement this facility in very easy manner
Quartz
Quartz normally additional plugin to java, which is used to schedule a task.And this provides to implement the custom time configuration at which time the user program needs to execute.
I used this plugin which is very working like charm .
Configuration :
Step 1: Download the latest jar file from Quartz official site , and configure it to your application libraries
Step 2: Two types of Scheduler is available in Quartz
- SimpleTrigger
- CronTrigger
CronTrigger is used to define At which time the program need to execute, it is very reliable one to know about this more here
We will use SimpleTrigger here
- Create SimpleTriggerExample Java class
import java.util.Date;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SimpleTrigger;
import org.quartz.impl.StdSchedulerFactory;
public class SimpleTriggerExample
{
public static void main( String[] args ) throws Exception
{
JobDetail job = new JobDetail();
job.setName("dummyJobName");
job.setJobClass(HelloJob.class);
//configure the scheduler time
SimpleTrigger trigger = new SimpleTrigger();
trigger.setStartTime(new Date(System.currentTimeMillis() + 1000));
trigger.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY);
trigger.setRepeatInterval(30000);
//schedule it
Scheduler scheduler = new StdSchedulerFactory().getScheduler();
scheduler.start();
scheduler.scheduleJob(job, trigger);
}
}
2.Create HelloJob class
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class HelloJob implements Job
{
public void execute(JobExecutionContext context)
throws JobExecutionException
{
System.out.println("Hello Quartz!");
}
}
Run the SimpleTriggerExample file the Hello Quartz will be printed for every 30 seconds
Reference : www.Mkyong.com
Comments
Post a Comment