Jslfl【软件开发技术笔记】

Quartz作业调度(一)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class TestJob implements Job{
    static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date exe = new Date();
    String exetime = dateFormat.format(exe);

    public void execute(JobExecutionContext arg0) throws JobExecutionException {  
        System.out.println(exetime + "作业调度操作...");  
    }  
   
    public static void main(String[] args) {
        Date taskStart = new Date();
    String taskStartTime = dateFormat.format(taskStart);       
    TestJob job = new TestJob();
    try {
        System.out.println(taskStartTime + "【调度启动...】");
        SchedulerFactory sf = new StdSchedulerFactory();
        Scheduler sched = sf.getScheduler();  
        JobDetail jobDetail = new JobDetail("job1", "group1", job.getClass());//任务名,任务组,任务执行类  
        //触发器  
        CronTrigger trigger = new CronTrigger("job1", "trigger1");//触发器名,触发器组  
        trigger.setCronExpression("0/3 * * * * ?");//触发器时间设定 每3秒
        sched.scheduleJob(jobDetail,trigger);  
        //启动  
        if(!sched.isShutdown())  
            sched.start();  
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}

, , ,

Comments are currently closed.