TIP

注意要加入EnableScheduling注解进行启动

package fun.gudu.task;

import fun.gudu.utils.DirectoryRecurse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.io.IOException;


@Slf4j
@Component
@Configuration
@EnableScheduling
public class CreateIndexTask {
    @Autowired
    private DirectoryRecurse directoryRecurse;

    @Value("${index-root}")
    private String indexRoot;

    @Scheduled(cron="0/30 * *  * * ? ")
    private void addIndex() {
        try {
            directoryRecurse.find(indexRoot);
        } catch (IOException e) {
            log.error("{}", e.getLocalizedMessage());
        }
    }
}

TIP

常用: 秒、分、时、日、月、年

0 0 10,14,16 * * ? 每天上午10点,下午2点,4点 0 0 12 * * ? 每天中午12点触发 0 0/5 0 * * ? 每5分钟执行一次

Last Updated: 11/9/2020, 4:20:27 PM