关于定时任务

在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();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值