java任務調度簡單示例


TimeTask

public class DemoTimeTask {
    public static void main(String[] args) {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            }
        }, 0, 1000 * 2);//延遲0秒,間隔2秒
    }
}

ScheduledExecutorService

public class DempScheduledExecutorService {
    public static void main(String[] args) {
        ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
        service.scheduleAtFixedRate(new Runnable() {
            public void run() {
                System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            }
        }, 0, 1000 * 2, TimeUnit.MILLISECONDS);//延遲0秒,間隔2秒
    }
}

springboot

啟動類添加@EnableScheduling

@Component
public class MyJob {
    @Scheduled(initialDelay = 0, fixedDelay = 1000)
    public void job1() {
        System.out.println("我是job1:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
    }

    @Scheduled(cron = "*/2 * * * * ?")
    public void job2() {
        System.out.println("我是job2:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
    }
}

quartz

1.pom依賴
        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
            <version>2.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz-jobs</artifactId>
            <version>2.2.1</version>
        </dependency>
2.任務類
public class MyJob implements Job {
    public void execute(JobExecutionContext context) throws JobExecutionException {
        System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
    }
}
3.啟動類
public class AppQuartz {
    public static void main(String[] args) throws SchedulerException {
        //1.創建Scheduler的工廠
        SchedulerFactory sf = new StdSchedulerFactory();
        //2.從工廠中獲取調度器實例
        Scheduler scheduler = sf.getScheduler();

        //3.創建JobDetail
        JobDetail jb = JobBuilder.newJob(MyJob.class)
                .withDescription("this is a ram job") //job的描述
                .withIdentity("ramJob", "ramGroup") //job 的name和group
                .build();

        //任務運行的時間,SimpleSchedle類型觸發器有效
        long time = System.currentTimeMillis() + 3 * 1000L; //3秒后啟動任務
        Date statTime = new Date(time);

        //4.創建Trigger
        //使用SimpleScheduleBuilder或者CronScheduleBuilder
        Trigger t = TriggerBuilder.newTrigger()
                .withDescription("")
                .withIdentity("ramTrigger", "ramTriggerGroup")
                //.withSchedule(SimpleScheduleBuilder.simpleSchedule())
                .startAt(statTime)  //默認當前時間啟動
                .withSchedule(CronScheduleBuilder.cronSchedule("0/2 * * * * ?")) //兩秒執行一次
                .build();

        //5.注冊任務和定時器
        scheduler.scheduleJob(jb, t);

        //6.啟動 調度器
        scheduler.start();
    }
}

分布式任務調度XXL-JOB。。。

待完善


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM