Spring Boot 定時任務單線程和多線程


1、創建定時任務:

@Component
public class AutoNotifyController {


    /**
     * 獲取RedisUtils注入的bean
     *
     * @return
     */
    private ThreadUtil getThreadUtil() {
        ThreadUtil threadUtil = SpringContextUtil.getBean("threadUtil");
        return threadUtil;
    }


    /**
     * @描述: 推送啟動充電結果的自動獲取和處理分發方法
     * @輸入值: void
     * @返回值: void
     */
    @Scheduled(cron = "*/5 * * * * ?")
    public void AutoNotifyStartChargeResult() {
        getThreadUtil().AutoNotifyStartChargeResult();
    }


    /**
     * @描述: 推送充電狀態的自動獲取和處理分發方法
     * @輸入值: void
     * @返回值: void
     */
    @Scheduled(cron = "*/50 * * * * ?")
    public void AutoNotifyChargeStatus() {
        getThreadUtil().AutoNotifyChargeStatus();
    }


    /**
     * @描述: 推送停止充電結果的自動獲取和處理分發方法
     * @輸入值: void
     * @返回值: void
     */
    @Scheduled(cron = "*/5 * * * * ?")
    public void AutoNotifyStopChargeResult() {
        getThreadUtil().AutoNotifyStopChargeResult();
    }


    /**
     * @描述: 推送訂單信息的自動獲取和處理分發方法
     * @輸入值: void
     * @返回值: void
     */
    @Scheduled(cron = "*/5 * * * * ?")
    public void AutoNotifyOrderInfo() {
        getThreadUtil().AutoNotifyOrderInfo();
    }


    /**
     * @描述: 公共信息部分的設備狀態變化推送接口的自動獲取和處理分發方法
     * @輸入值: void
     * @返回值: void
     */
    @Scheduled(fixedRate = 200)
    public void checkGunStatus() {
        getThreadUtil().checkGunStatus();
    }


    /**
     * @描述: 對於Redis中的活躍訂單增加和刪除的輪詢執行方法
     */
    @Scheduled(cron = "*/5 * * * * ?")
    public void ActiveOrderAddAndDelete() {
        getThreadUtil().ActiveOrderAddAndDelete();
    }


    /**
     * @描述: 對於Redis中的結束訂單訂單增加和刪除的輪詢執行方法
     */
    @Scheduled(cron = "*/5 * * * * ?")
    public void EndOrderAddAndDelete() {
        getThreadUtil().EndOrderAddAndDelete();
    }


}

使用 @Scheduled來創建定時任務 這個注解用來標注一個定時任務方法。 
通過看 @Scheduled源碼可以看出它支持多種參數:
    (1)cron:cron表達式,指定任務在特定時間執行;
    (2)fixedDelay:表示上一次任務執行完成后多久再次執行,參數類型為long,單位ms;
    (3)fixedDelayString:與fixedDelay含義一樣,只是參數類型變為String;
    (4)fixedRate:表示按一定的頻率執行任務,參數類型為long,單位ms;
    (5)fixedRateString: 與fixedRate的含義一樣,只是將參數類型變為String;
    (6)initialDelay:表示延遲多久再第一次執行任務,參數類型為long,單位ms;
    (7)initialDelayString:與initialDelay的含義一樣,只是將參數類型變為String;
    (8)zone:時區,默認為當前時區,一般沒有用到。

2、開啟定時任務:

@SpringBootApplication
@EnableScheduling
public class PositivebuttjointApplication extends SpringBootServletInitializer
{

    public static void main(String[] args)
    {
        SpringApplication.run(PositivebuttjointApplication.class, args);


    }

注:這里的 @EnableScheduling  注解,它的作用是發現注解 @Scheduled的任務並由后台執行。沒有它的話將無法執行定時任務。
引用官方文檔原文:
@EnableScheduling ensures that a background task executor is created. Without it, nothing gets scheduled.

3、執行結果(單線程)

就完成了一個簡單的定時任務模型,下面執行springBoot觀察執行結果:

從控制台輸入的結果中我們可以看出所有的定時任務都是在同一個線程池用同一個線程來處理的,那么我們如何來並發的處理各定時任務呢,請繼續向下看。

4、多線程處理定時任務:

      1.開啟多線程

  

@SpringBootApplication
@EnableScheduling
@EnableAsync
public class PositivebuttjointApplication extends SpringBootServletInitializer
{

    public static void main(String[] args)
    {
        SpringApplication.run(PositivebuttjointApplication.class, args);

    }

    加入@EnableAsync開啟多線程

   2.使用多線程

    

  @Async
    public void AutoNotifyStartChargeResult() {


    }

   調用的方法上加上@Async使用多線程

  3.配置連接池

  

@Configuration
public class ScheduleConfiguration implements SchedulingConfigurer {


    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        taskRegistrar.setScheduler(this.getTaskScheduler());
    }

    private ThreadPoolTaskScheduler getTaskScheduler() {
        ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
        taskScheduler.setPoolSize(20);
        taskScheduler.setThreadNamePrefix("schedule-pool-");
        taskScheduler.initialize();
        return taskScheduler;
    }
}

 


免責聲明!

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



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