Home > Back-end >  Only no-arg methods may be annotated with @Scheduled
Only no-arg methods may be annotated with @Scheduled

Time:12-26

@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("[email protected]", "[email protected]", "List for "   fileDate, " ", "Report "   fileDate1   ".xlsx");
        attachmentEmail.sendMail2("[email protected]", "[email protected]", "List for "   fileDate, " ", "Report1 "   fileDate1   ".xlsx");
        attachmentEmail.sendMail3("[email protected]", "[email protected]", "List for "   fileDate, " ", "Report2 "   fileDate1   ".xlsx");
    }

    @Scheduled(cron = "27 17 * * * *")
    public void run2(ApplicationArguments args) throws Exception {
        attachmentEmail.sendMail4("[email protected]", "[email protected]", "List for "   fileDate, " ", "Report3 "   fileDate1   ".xlsx");
        attachmentEmail.sendMail5("[email protected]", "[email protected]", "List for "   fileDate, " ", "Report4 "   fileDate1   ".xlsx");
        attachmentEmail.sendMail6("[email protected]", "[email protected]", "List for "   fileDate, " ", "Report5 "   fileDate1   ".xlsx");
    }

    @Scheduled(cron = "28 17 * * * *")
    public void run3(ApplicationArguments args) throws Exception {
        attachmentEmail.sendMail7("[email protected]", "[email protected]", "List for "   fileDate, " ", "Report6 "   fileDate1   ".xlsx");
        attachmentEmail.sendMail8("[email protected]", "[email protected]", "List for "   fileDate, " ", "Report7 "   fileDate1   ".xlsx");
        attachmentEmail.sendMail9("[email protected]", "[email protected]", "List for "   fileDate, " ", "Report8 "   fileDate1   ".xlsx");
    }
}

My project has a method that starts saving to a .xlsx file. First, I want to separate them so that some files are saved at one time and others at another time. I tried to set up this method to run through Scheduled

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

However, I get an error, because in the parameters of the method I have (args), without which this method does not work. How do I set the Scheduler so that my method is called on time?

CodePudding user response:

On run1(), run2() and run3() you don't need the ApplicationArguments args because the only method actually overriding ApplicationRunner.run(ApplicationArguments args) method, so just drop them. Additionally, I would keep it simple and simply separate the methods (the one overriding ApplicationRunner.run(ApplicationArguments args) method and the one that is 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();
    }

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

    @Scheduled(cron = "26 17 * * * *")
    public void run1() throws Exception {
        attachmentEmail.sendMail1("[email protected]", "[email protected]", "List for "   fileDate, " ", "Report "   fileDate1   ".xlsx");
        attachmentEmail.sendMail2("[email protected]", "[email protected]", "List for "   fileDate, " ", "Report1 "   fileDate1   ".xlsx");
        attachmentEmail.sendMail3("[email protected]", "[email protected]", "List for "   fileDate, " ", "Report2 "   fileDate1   ".xlsx");
    }

    @Scheduled(cron = "27 17 * * * *")
    public void run2() throws Exception {
        attachmentEmail.sendMail4("[email protected]", "[email protected]", "List for "   fileDate, " ", "Report3 "   fileDate1   ".xlsx");
        attachmentEmail.sendMail5("[email protected]", "[email protected]", "List for "   fileDate, " ", "Report4 "   fileDate1   ".xlsx");
        attachmentEmail.sendMail6("[email protected]", "[email protected]", "List for "   fileDate, " ", "Report5 "   fileDate1   ".xlsx");
    }

    @Scheduled(cron = "28 17 * * * *")
    public void run3() throws Exception {
        attachmentEmail.sendMail7("[email protected]", "[email protected]", "List for "   fileDate, " ", "Report6 "   fileDate1   ".xlsx");
        attachmentEmail.sendMail8("[email protected]", "[email protected]", "List for "   fileDate, " ", "Report7 "   fileDate1   ".xlsx");
        attachmentEmail.sendMail9("[email protected]", "[email protected]", "List for "   fileDate, " ", "Report8 "   fileDate1   ".xlsx");
    }
}
  • Related