定时器注解 @Scheduled 使用
试列代码:
1 @Component 2 @Slf4j 3 //配置文件注入注解 4 @PropertySource("classpath:/**.properties") 5 public class TimerController { 6 @Value("${SEND_TEL}") 7 private String tel; 8 9 //* * 8,10,14,16 * * ? 10 /**每天8、10、14、16四个时间点执行任务**/ 11 //${CRON} CRON为配置文件中的key CRON=0 0 8,10,14,16 * * ? 12 @Scheduled(cron="${CRON}") 13 public void timerTask(){ 14 System.err.println("定时器!"+new Date().toString()); 15 16 try { 17 //如有多个值且需要一个一个取出再使用则需转换遍历 18 /**取出属性文件中的电话号并加入list**/ 19 String [] c=tel.split(","); 20 List<String> list = new ArrayList<>(); 21 for(int i=0;i<c.length;i++){ 22 list.add(c[i]); 23 // System.out.println(list.get(i)); 24 SendSmsResponse response = SmsUtils.sendSms(list.get(i),9999); 25 } 26 } catch (ClientException e) { 27 e.printStackTrace(); 28 } 29 } 30 31 }