'Spring scheduler which is run after application is started and after midnight

How to describe Spring scheduler which is run after application is started and after 00:00 ?



Solution 1:[1]

A little about this subject, you can use @EventListener annotation.

Here is an example :

@Component
public class MyScheduler {

    @EventListener(ApplicationReadyEvent.class)
    public void onSchedule() {
        doWork();
    }

    public void doWork() {
        // your work required on startup
    }
}

Solution 2:[2]

At first you should add @EnableScheduling annotation for your application config.

The second add @Component or @Service annotation for your scheduler. And if you are using Scheduled annotation it runs automatically after initialization to change it you can use initialDelay parameter in annotation.

Here is complete example

@Component
public class MyScheduler {

    @Scheduled(cron="*/10 * * * * *")
    public void onSchedule() {
        doWork();
    }

    public void doWork() {
        // your work required on startup and at midnight
    }
}

Solution 3:[3]

We have multiple approaches to execute it, The suggested approach by the @nicholas.hauschild is correct and easier than any other approach, but with the @PostConstruct we must be careful not to use the method of the dependent bean. That bean may not have been initialized yet and cause a NullPointerException.

So we can implement CommandLineRunner or ApplicationRunner and implement the run method, So by that run method will be automatically invoked after all beans are initialized and the app has been started.

   @Component
   public class CommandLineAppStartupRunner implements CommandLineRunner {
        @Autowired
        private MyClass MyClass;

        @Override
        public void run(String...args) throws Exception {
           myService.doWork();

        }
    } 

Solution 4:[4]

okay, as I understand you want to run your schedule job once the app is started and at every midnight.

Spring Scheduler offers initialDelay but you have to use it with fixedDelay.

here is my answer:

// it runs after 30 seconds of startup, you will be dead before it runs again.

@Scheduled(initialDelay = 1000 * 30, fixedDelay=Long.MAX_VALUE)
public void runAtStartup(){
   doWork();
}

// runs every midgnight (12.00 AM)
@Scheduled(cron="0 0 0 * * ?")
public void onSchedule() {
   doWork();
}

Solution 5:[5]

Understanding more about it follow this link https://www.baeldung.com/cron-expressions

For the specific task to be done at midnight there is a predefined annotation that can help , try @midnight. It should work:

    @midnight
    public void midnightRun(){
       doTheWork();
    }

Solution 6:[6]

public class MyClass {
    @PostConstruct
    @Scheduled(cron="0 0 0 * * ?")
    public void doWork() {
        // your work 
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Philippe Gibert
Solution 2 Kovalsky Dmitryi
Solution 3 Vinit Solanki
Solution 4 nsv
Solution 5
Solution 6 iknow