September 16, 2018

cron job example in java

package com.steffi.samples;

import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

public class CurrencyConvertor
{
    public static void main( String[] args ) throws Exception
    {
       
    JobDetail job = JobBuilder.newJob(Datascientist.class)
.withIdentity("steffi", "nijesh").build();


   
    Trigger trigger = TriggerBuilder
.newTrigger()
.withIdentity("steffi", "nijesh")
.withSchedule(
CronScheduleBuilder.cronSchedule("0/5 * * * * ?"))
.build();
   
    //schedule it
    Scheduler scheduler = new StdSchedulerFactory().getScheduler();
    scheduler.start();
    scheduler.scheduleJob(job, trigger);
 
    }
}


import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class Datascientist implements Job
{
public void execute(JobExecutionContext context)
throws JobExecutionException {

System.out.println("Hello Quartz!");

}

}

<dependencies>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.1.5</version>
</dependency>
</dependencies>

No comments:

Post a Comment

Your feedback may help others !!!

Facebook comments