Spring Boot : 定時任務(十)

目錄

單線程定時任務

SpringBoot提供的定時任務是單線程的。代碼很簡單。

package cn.milo.controllor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Configuration
@EnableScheduling
//@Component
public class Scheduling {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Scheduled(cron = "0/5 * * * * ?") // 每20秒執行一次
    public void scheduler() {
        logger.info("定時任務 1");
    }
}

    @Scheduled(cron = "0/5 * * * * ?") // 每20秒執行一次
    public void scheduler2() {
        logger.info("定時任務 2");
    }
}

大家可以打印一下線程號,會發現所有定時任務都是串行完成的。但很多時候我們需要好多個定時任務一起進行,且互不干擾。下邊介紹並行定時任務。

多線程定時任務

Scheduling.java

package cn.milo.controllor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

//@Configuration //這裏就不用springboot了
//@EnableScheduling //這裏就不用springboot了
@Component //spring直接加載這個類
public class Scheduling {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Scheduled(cron = "0/5 * * * * ?") // 每20秒執行一次
    public void scheduler() {
        logger.info("定時任務 1");
    }

    @Scheduled(cron = "0/5 * * * * ?") // 每20秒執行一次
    public void scheduler2() {
        logger.info("定時任務 2");
    }
}

通過SpringBoot配置Spring方式來指定Spring配置文件
SpringConfig.java

package cn.milo.controllor;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

/**
 * Created by mac on 2017/8/28.
 */
@Configuration
@ImportResource("/spring/applicationContext.xml")
public class SpringConfig {
}

在src/main/resources/spring下簡歷spring配置文件
applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:task="http://www.springframework.org/schema/task"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">

    <!-- Enables the Spring Task @Scheduled programming model -->
    <task:executor id="executor" pool-size="5" />
    <task:scheduler id="scheduler" pool-size="10" />
    <task:annotation-driven executor="executor" scheduler="scheduler" />

</beans>
發佈了41 篇原創文章 · 獲贊 35 · 訪問量 18萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章