springboot做定进任务,比较简单。
-
在applicatoin上加上注解 @EnableScheduling
-
在自己的方法上,加上 @Scheduled(fixedDelay = 10*1000 ) 就可以每次执行完后,隔10秒,执行下一次了。 但是,现在我希望这个时间可配置
-
我在application.proterties中增加了自定义的一项。mail.schedule=10000
-
在执行类中,增加 @ConfigurationProperties(prefix = "mail"),在执行方法上,改为 @Scheduled(fixedDelayString = "${mail.schedule}")
-
kotlin的写法,要在$前加,然后fixedDelay 要改为fixedDelayString
@Component
@ConfigurationProperties(prefix = "mail")
class ScheduledTask {
@Autowired
lateinit var mailInboxService: MailReceiveUtils
private val logger = LoggerFactory.getLogger(javaClass)
@Scheduled(fixedDelayString = "\${mail.schedule}")
fun receive() {
MailServerApplication.data.forEach({
mailInboxService.resceive(it)
}
)
}
}