Spring boot 定時任務

2021-08-20 08:57:22 字數 1213 閱讀 1230

1.在啟動類上加@enablescheduling註解

package com.example.demo;

import org.springframework.scheduling.annotation.enablescheduling;

@enablescheduling

public static void main(string args)

}

2.在建立定時任務的類上加上@component,並在方法上加@scheduled(cron="")

package com.example.demo.com.example.demo.controller;

import org.springframework.scheduling.annotation.scheduled;

import org.springframework.stereotype.component;

@component

public class timejob

@scheduled(cron = "0/1 * * * * ?")

public void task2()

@scheduled(cron = "0/1 * * * * ?")

public void task3()

public void task4()

}

3.注意配乙個任務的執行緒池,不然所有的定時任務走同乙個執行緒;影響效率.定時任務的config如下:

package com.example.demo.com.example.demo.config;

import org.springframework.context.annotation.bean;

import org.springframework.context.annotation.configuration;

import org.springframework.scheduling.concurrent.threadpooltaskscheduler;

@configuration

public class taskschedulerconfig

}

4.執行結果如下

SpringBoot 定時任務

第一步 在啟動類中加入如下註解 enablescheduling public class public static void main string args 注意 enablescheduling必須加,否則無法開啟定時任務 第二步 建立定時任務 component public class ...

Spring boot定時任務

最近做專案,需要使用定時任務,半個小時去更新redis資料,於是便學習了一下經驗。希望可以幫到你們。定時任務可使用三種 created by fx on 2017 9 4.component allargsconstructor public class featuredatatask 實現乙個任務...

SpringBoot定時任務

在需要設定定時的位置加 scheduled 註解 scheduled cron 60 public void deal1 bean public resttemplate resttemplate cron的時間設定表示式 一般有6個,也可能有7個 時段可用值 號可用於所有字段,在分這個字段表示每分...