第一步:定义两个类
Task3
package group.esperanto.util;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component //必须标记为一个Spring管理的JavaBean类
public class MyTask3 { //不需要继承任何子类
@Scheduled(fixedRate=2000) //设置为两秒一执行 间隔触发
public void excJob() {
System.out.println("【MyTask3】当前日期: " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
}
}
Task4
package group.esperanto.util;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component //必须标记为一个Spring管理的JavaBean类
public class MyTask4 {
@Scheduled(cron="* * * * * ?" ) // 一秒一执行 定时触发
public void excJob() {
try{
// 休息5秒再继续执行
TimeUnit.SECONDS.sleep(5);
}catch (Exception e) {
}
System.out.println("【MyTask4】当前日期: " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
}
}
第二步:修改applicationContext文件