Quartz 之 Simple

Posted satisfysmy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Quartz 之 Simple相关的知识,希望对你有一定的参考价值。

package com.satisfy;


import java.text.SimpleDateFormat;
import java.util.Date;

import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobKey;
import org.quartz.Trigger;
import org.quartz.TriggerKey;

public class HelloJob implements Job {
 
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        Date date=new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("current times is"+sdf.format(date));
        System.out.println("Hello World!!!");
    }
    
    
    
         
}













package com.satisfy;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.SimpleTrigger;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

public class HelloScheduler {
       public static void main(String[] args) throws SchedulerException {
           Date date=new Date();
           SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
           System.out.println("current times is"+sdf.format(date));
         
        // 创建一个JobDetail实例,将该实例与HelloJob Class绑定
                    JobDetail jobDetail = JobBuilder.newJob(HelloJob.class)
                            .withIdentity("myJob").
                            build();
                    
            date.setTime(date.getTime()+4000);        
            //  距离当前时间4 秒后  执行且执行 一次任务
            //  距离当前时间4 秒后 首次执行任务,之后每次隔开2 秒执行一次任务
            SimpleTrigger trigger=(SimpleTrigger)TriggerBuilder.newTrigger().withIdentity("myTrigger", "group1").
                    startAt(date).
                    withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(2).withRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY))
                            .build();
            // 创建Scheduler实例
            SchedulerFactory sfact = new StdSchedulerFactory();
            Scheduler scheduler =sfact.getScheduler();
            scheduler.start();
            
            scheduler.scheduleJob(jobDetail, trigger);
    }
}

 

以上是关于Quartz 之 Simple的主要内容,如果未能解决你的问题,请参考以下文章

csharp C#代码片段 - 使类成为Singleton模式。 (C#4.0+)https://heiswayi.github.io/2016/simple-singleton-pattern-us

160405quartz持久化所需表结构

Quartz之入门实例

Quartz 之 HelloWorld

Quartz2之入门示例转

Quartz 之 处理Job 异常