在springboot框架下写的,不必写定时相关的配置,只用到以下代码,即可完成 import com.genshuixue.coursetool.dao.JDBCUtil; import com.genshuixue.coursetool.service.ClassesInfoTest; import com.genshuixue.coursetool.service.ExcelTool; import com.genshuixue.coursetool.service.SendMailUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.sql.Connection; @Component public class TimingTask { // 具体分类的URL topChannelId=4784&secondChannelId=4784&cursor=1 String url = "https://m.genshuixue.com/sapi/channel/list?thirdChannelId=all&fourthChannelId=1&appType=5"; @Autowired ClassesInfoTest classesInfoTest; @Autowired ExcelTool excelTool; @Autowired SendMailUtil sendMailUtil; // @Scheduled(fixedRate=120000) // 每天01:00运行 // @Scheduled(cron = "0 0 1 * * ? ")//0 0,39,42,45,48 * * * ? // @Scheduled(cron = "0 5 10 * * ? ") 这些通过cron在线生成器就能产生 //@Scheduled是Spring的一个定时任务注解,通过注解配置就能够轻量级的定时任务,简单方便。
@Scheduled(cron = "0 5,7,9 12 * * ? ") public void pushDataScheduled() { try { Connection conn = JDBCUtil.getConn(); classesInfoTest.testGet(conn, url); String excelPath = excelTool.toExcel(); sendMailUtil.sendMailOperation(excelPath); JDBCUtil.closeConnection(conn); // System.out.println("统计完毕"); } catch (Exception e) { e.printStackTrace(); } } }
关于定时任务
最新推荐文章于 2022-10-07 00:08:02 发布