只有无参数方法可以用@Scheduled 注释

Only no-arg methods may be annotated with @Scheduled

@Component
public class SaveProviderStartupRunner implements ApplicationRunner {

    @Autowired
    private ProviderController providerController;

    @Autowired
    private AttachmentEmail attachmentEmail;

    String fileDate1 = new SimpleDateFormat("dd.MM.yyyy").format(new Date());

    LocalDate today = LocalDate.now();
    String fileDate = (today.minusDays(1)).format(DateTimeFormatter.ofPattern("dd MMM"));
    String fileDate2 = (today.minusMonths(1)).format(DateTimeFormatter.ofPattern("MMM"));

    @Override
    public void run(ApplicationArguments args) throws Exception {
        providerController.saveCards();
    }

    //@Override
    @Scheduled(cron = "26 17 * * * *")
    public void run1(ApplicationArguments args) throws Exception {
        attachmentEmail.sendMail1("SomeEmail@gmail.com", "SomeEmail2@gmail.com", "List for " + fileDate, " ", "Report " + fileDate1 + ".xlsx");
        attachmentEmail.sendMail2("SomeEmail@gmail.com", "SomeEmail3@gmail.com", "List for " + fileDate, " ", "Report1 " + fileDate1 + ".xlsx");
        attachmentEmail.sendMail3("SomeEmail@gmail.com", "SomeEmail4@gmail.com", "List for " + fileDate, " ", "Report2 " + fileDate1 + ".xlsx");
    }

    @Scheduled(cron = "27 17 * * * *")
    public void run2(ApplicationArguments args) throws Exception {
        attachmentEmail.sendMail4("SomeEmail@gmail.com", "SomeEmail5@gmail.com", "List for " + fileDate, " ", "Report3 " + fileDate1 + ".xlsx");
        attachmentEmail.sendMail5("SomeEmail@gmail.com", "SomeEmail6@gmail.com", "List for " + fileDate, " ", "Report4 " + fileDate1 + ".xlsx");
        attachmentEmail.sendMail6("SomeEmail@gmail.com", "SomeEmail7@gmail.com", "List for " + fileDate, " ", "Report5 " + fileDate1 + ".xlsx");
    }

    @Scheduled(cron = "28 17 * * * *")
    public void run3(ApplicationArguments args) throws Exception {
        attachmentEmail.sendMail7("SomeEmail@gmail.com", "SomeEmail8@gmail.com", "List for " + fileDate, " ", "Report6 " + fileDate1 + ".xlsx");
        attachmentEmail.sendMail8("SomeEmail@gmail.com", "SomeEmail9@gmail.com", "List for " + fileDate, " ", "Report7 " + fileDate1 + ".xlsx");
        attachmentEmail.sendMail9("SomeEmail@gmail.com", "SomeEmail10@gmail.com", "List for " + fileDate, " ", "Report8 " + fileDate1 + ".xlsx");
    }
}

我的项目有一个开始保存到 .xlsx 文件的方法。首先,我想将它们分开,以便一些文件一次保存,另一些文件在另一次保存。我试图通过 Scheduled

将此方法设置为 运行
@Override
@Scheduled(cron = "10 10 * * * *")
public void run(ApplicationArguments args) throws Exception {
    providerController.saveCards();
}

但是,我得到一个错误,因为在方法的参数中我有 (args),没有它这个方法就不能工作。如何设置调度程序以便按时调用我的方法?

run1()run2()run3() 上,您不需要 ApplicationArguments args,因为唯一的方法实际上覆盖了 ApplicationRunner.run(ApplicationArguments args) 方法,所以只需删除他们。此外,我会保持简单并简单地将方法分开(一个覆盖 ApplicationRunner.run(ApplicationArguments args) 方法和一个被安排的方法):

@Component
public class SaveProviderStartupRunner implements ApplicationRunner {

    @Autowired
    private ProviderController providerController;

    @Autowired
    private AttachmentEmail attachmentEmail;

    String fileDate1 = new SimpleDateFormat("dd.MM.yyyy").format(new Date());

    LocalDate today = LocalDate.now();
    String fileDate = (today.minusDays(1)).format(DateTimeFormatter.ofPattern("dd MMM"));
    String fileDate2 = (today.minusMonths(1)).format(DateTimeFormatter.ofPattern("MMM"));

    @Override
    public void run(ApplicationArguments args) throws Exception {
        providerController.saveCards();
    }

    @Scheduled(cron = "10 10 * * * *")
    public void run() throws Exception {
        providerController.saveCards();
    }

    @Scheduled(cron = "26 17 * * * *")
    public void run1() throws Exception {
        attachmentEmail.sendMail1("SomeEmail@gmail.com", "SomeEmail2@gmail.com", "List for " + fileDate, " ", "Report " + fileDate1 + ".xlsx");
        attachmentEmail.sendMail2("SomeEmail@gmail.com", "SomeEmail3@gmail.com", "List for " + fileDate, " ", "Report1 " + fileDate1 + ".xlsx");
        attachmentEmail.sendMail3("SomeEmail@gmail.com", "SomeEmail4@gmail.com", "List for " + fileDate, " ", "Report2 " + fileDate1 + ".xlsx");
    }

    @Scheduled(cron = "27 17 * * * *")
    public void run2() throws Exception {
        attachmentEmail.sendMail4("SomeEmail@gmail.com", "SomeEmail5@gmail.com", "List for " + fileDate, " ", "Report3 " + fileDate1 + ".xlsx");
        attachmentEmail.sendMail5("SomeEmail@gmail.com", "SomeEmail6@gmail.com", "List for " + fileDate, " ", "Report4 " + fileDate1 + ".xlsx");
        attachmentEmail.sendMail6("SomeEmail@gmail.com", "SomeEmail7@gmail.com", "List for " + fileDate, " ", "Report5 " + fileDate1 + ".xlsx");
    }

    @Scheduled(cron = "28 17 * * * *")
    public void run3() throws Exception {
        attachmentEmail.sendMail7("SomeEmail@gmail.com", "SomeEmail8@gmail.com", "List for " + fileDate, " ", "Report6 " + fileDate1 + ".xlsx");
        attachmentEmail.sendMail8("SomeEmail@gmail.com", "SomeEmail9@gmail.com", "List for " + fileDate, " ", "Report7 " + fileDate1 + ".xlsx");
        attachmentEmail.sendMail9("SomeEmail@gmail.com", "SomeEmail10@gmail.com", "List for " + fileDate, " ", "Report8 " + fileDate1 + ".xlsx");
    }
}