码迷,mamicode.com
首页 > 编程语言 > 详细

Java的任务调度,使用Cron表达式实现

时间:2016-05-20 19:32:39      阅读:485      评论:0      收藏:0      [点我收藏+]

标签:

通过定时任务调度框架Quartz可以实现某些定时功能,定时执行某些方法等功能。下面提供一个简单的例子,实现Quartz框架的Hello World.

import org.quartz.CronExpression;
import org.quartz.CronTrigger;
import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;

public class DiaoduTest implements Job {

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        System.out.println(new Date() + "天佑最帅!!!");
        
    }
    public static void main(String[] args) {
        JobDetail detail = new JobDetail("job1", "group1", DiaoduTest.class);
        CronTrigger cronTrigger = new CronTrigger("job1", "group1");
        try {
            CronExpression cronExpression = new CronExpression("0/1 * * * * ?");
            cronTrigger.setCronExpression(cronExpression);
            SchedulerFactory factory = new StdSchedulerFactory();
            Scheduler scheduler;
            try {
                scheduler = factory.getScheduler();
                try {
                    scheduler.scheduleJob(detail, cronTrigger);
                    scheduler.start();
                } catch (SchedulerException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            } catch (SchedulerException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

 

Java的任务调度,使用Cron表达式实现

标签:

原文地址:http://www.cnblogs.com/qiutianyou/p/5513167.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!