about 2 years ago

以前設定固定排程最常用就是 Quartz 但是難免還要配置一些東西,現在用 Spring-boot 只要一個註解就可以啟動排程工作了

build.gradle
buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.5.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'spring-boot'

jar {
    baseName = 'gs-scheduling-tasks'
    version =  '0.1.0'
}

repositories {
    mavenCentral()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    compile("org.springframework.boot:spring-boot-starter")
    testCompile("junit:junit")
}

啟動程式

Application.java
package com.sam.schedule;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
 
@SpringBootApplication
@EnableScheduling
@EnableAsync
public class Application {
 
    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class);
    }
}

排程設定

ScheduledTasks.java
package com.sam.schedule;

import java.util.concurrent.atomic.AtomicInteger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class ScheduledTasks {
    private final AtomicInteger counter = new AtomicInteger();

    @Autowired
    private AsyncWorker worker;

    @Scheduled(fixedRate = 3000, initialDelay = 1 * 1000)
    public void report1() {
        for (int i = 0; i < 10; i++) {
            worker.work("reportCurrentTime1 - " + counter.incrementAndGet());
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block

                e.printStackTrace();
            }
        }
    }

    @Scheduled(fixedRate = 3000, initialDelay = 1 * 1000)
    public void report2() {
        for (int i = 0; i < 10; i++) {
            worker.work("reportCurrentTime2 - " + counter.incrementAndGet());
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block

                e.printStackTrace();
            }
        }
    }
}
AsyncWorker.java
package com.sam.schedule;

import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
public class AsyncWorker{

    @Async
    public void work(String name) {
        String threadName = Thread.currentThread().getName(); 
        System.out.println("   " + threadName + " beginning work on " + name);
        try {
            Thread.sleep(5000); // simulates work

        }catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
        System.out.println("   " + threadName + " completed work on " + name);
    }
}

執行結果

   SimpleAsyncTaskExecutor-1 beginning work on reportCurrentTime1 - 1
   SimpleAsyncTaskExecutor-2 beginning work on reportCurrentTime1 - 2
   SimpleAsyncTaskExecutor-3 beginning work on reportCurrentTime1 - 3
   SimpleAsyncTaskExecutor-4 beginning work on reportCurrentTime1 - 4
   SimpleAsyncTaskExecutor-5 beginning work on reportCurrentTime1 - 5
   SimpleAsyncTaskExecutor-6 beginning work on reportCurrentTime1 - 6
   SimpleAsyncTaskExecutor-7 beginning work on reportCurrentTime1 - 7
   SimpleAsyncTaskExecutor-8 beginning work on reportCurrentTime1 - 8
   SimpleAsyncTaskExecutor-9 beginning work on reportCurrentTime1 - 9
   SimpleAsyncTaskExecutor-10 beginning work on reportCurrentTime1 - 10
   SimpleAsyncTaskExecutor-11 beginning work on reportCurrentTime2 - 11
   SimpleAsyncTaskExecutor-12 beginning work on reportCurrentTime2 - 12
   SimpleAsyncTaskExecutor-13 beginning work on reportCurrentTime2 - 13
   SimpleAsyncTaskExecutor-14 beginning work on reportCurrentTime2 - 14
   SimpleAsyncTaskExecutor-15 beginning work on reportCurrentTime2 - 15
   SimpleAsyncTaskExecutor-16 beginning work on reportCurrentTime2 - 16
   SimpleAsyncTaskExecutor-17 beginning work on reportCurrentTime2 - 17
   SimpleAsyncTaskExecutor-1 completed work on reportCurrentTime1 - 1
   SimpleAsyncTaskExecutor-18 beginning work on reportCurrentTime2 - 18
   SimpleAsyncTaskExecutor-2 completed work on reportCurrentTime1 - 2
   SimpleAsyncTaskExecutor-19 beginning work on reportCurrentTime2 - 19
   SimpleAsyncTaskExecutor-3 completed work on reportCurrentTime1 - 3
   SimpleAsyncTaskExecutor-20 beginning work on reportCurrentTime2 - 20
   SimpleAsyncTaskExecutor-4 completed work on reportCurrentTime1 - 4
   SimpleAsyncTaskExecutor-21 beginning work on reportCurrentTime1 - 21
   SimpleAsyncTaskExecutor-5 completed work on reportCurrentTime1 - 5
   SimpleAsyncTaskExecutor-22 beginning work on reportCurrentTime1 - 22
   SimpleAsyncTaskExecutor-6 completed work on reportCurrentTime1 - 6
   SimpleAsyncTaskExecutor-23 beginning work on reportCurrentTime1 - 23
   SimpleAsyncTaskExecutor-7 completed work on reportCurrentTime1 - 7
   SimpleAsyncTaskExecutor-24 beginning work on reportCurrentTime1 - 24
   SimpleAsyncTaskExecutor-8 completed work on reportCurrentTime1 - 8
   SimpleAsyncTaskExecutor-25 beginning work on reportCurrentTime1 - 25

這樣可以看的出來當在 Scheduled 主線程的時候還是要等他跑完 report1 後才會跑 report2,但是 AsyncWorker 部份是異步執行,不用等他跑完

參考
Scheduling Tasks
簡易介紹,使用spring @Scheduled 註解執行定時任務!

← use Quartz in Spring Boot Android內WebView除錯 →
 
comments powered by Disqus