在springboot项目中 添加 @EnableScheduling
package com.zetyun.choice;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
@MapperScan("com.zetyun.choice.dao")
public class ChangheApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(ChangheApplication.class, args);
}
}
在 项目的server.impl 包下的新建一个一个类MyJosbServerImpl
package com.zetyun.choice.service.impl;
import java.util.Date;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
@Service
public class MyJosbServerImpl {
public final static long ONE_Minute = 30 * 1000;
@Scheduled(fixedDelay = ONE_Minute)
public void fixedDelayJob() {
System.out.println(new Date() + " >>fixedDelay执行....");
}
@Scheduled(fixedRate = ONE_Minute)
public void fixedRateJob() {
System.out.println((new Date()) + " >>fixedRate执行....");
}
@Scheduled(cron = "0 15 3 * * ?")
public void cronJob() {
System.out.println((new Date()) + " >>cron执行....");
}
}
然后启动 springboot项目 就可以看到30秒定时调用打印输出。
>>fixedDelay执行....
>>fixedRate执行....
方法被定时调用。