现在有活动

问题描述 投票:0回答:1

例如,每天都会运行此函数。如果我能传递参数就好了。

java
1个回答
3
投票

最简单的方法是使用

@Scheduled

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.time.Instant;

@Component
@EnableScheduling
public class Scheduler {
    
    @Scheduled(cron = "0 30 8 * * ?", zone = "UTC")
    public void eightAMRun() {
        System.out.println("Breakfast time - " + Instant.now());
    }
}

"0 30 8 * * ?"
是一个 cron 表达式,表示“每天上午 08:30:00”

© www.soinside.com 2019 - 2024. All rights reserved.