Quartz定时框架SimpleTrigger开发使用实例

定义工作任务Job

public class HelloJob implements Job {
    public void execute(JobExecutionContext context) throws JobExecutionException {
        System.out.println("Hello,Quartz!");
    }
}

编写定时任务代码

public QuartzDemo {
    public static void main(String[] args) throws SchedulerException {
        //定时器对象
        Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
        //定义一个工作对象
        JobDetail job = JobBuilder.newJob(HelloJob.class).withIdentity("job1","group1").build();
        //定义触发器
        Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger1","group1").startNow().withSchedule(SimpleScheduleBuilder.repeatSecondlyForever(5)).build();
        scheduler.scheduleJob(job,trigger);
        //开启定时任务
        scheduler.start();
    }
}

你可能感兴趣的:(经验分享,后台编程,学亮说JAVA)