Home > Blockchain >  run method at specific date and time using spring boot java like reminder or alarm
run method at specific date and time using spring boot java like reminder or alarm

Time:10-18

I need to run a function at a specific date using java spring boot like reminder or alarm

CodePudding user response:

Without knowing any of your constraints, the easiest solution would probably be using Spring Scheduling.

  1. Add the @EnableScheduling annotation to (one of) your config classes
  2. Add the @Scheduled annotation to the method you want to run

There's a few options for scheduling, using cron would probably make most sense for a one-time event.

Keep in mind this is probably not the best solution in many cases as the time this will run at is hard coded into your application, but based on your requirements, this will do.

CodePudding user response:

an easy approach create a while loop and check LocalDateTime.now() with your input

    String input = "";
    LocalDateTime now = LocalDateTime.now();
    while(true){

        if(now.equals(LocalDateTime.parse(input))){
            // trigger your action
            break;
        }
    }
  • Related