Home > Enterprise >  Programmatically trigger a @Scheduled method?
Programmatically trigger a @Scheduled method?

Time:11-11

I'm working on a Springboot app that includes a task that's executed on a schedule. It typically takes about two to three minutes to run.

@Scheduled(cron = "* */30 * * * *")
public void stageOfferUpdates() throws SQLException {
...

We have a requirement to be able to kick off the execution of that task at any time by calling a rest endpoint. Is there a way my @GET method can programmatically kick this off and immediately return an http 200 OK?

CodePudding user response:

So you just want to trigger an async task without waiting for results. Because you are using Spring, the @Async annotation is an easy way to achieve the goal.

@Async
public void asyncTask() {
    stageOfferUpdates();
}

CodePudding user response:

Couldn't you just run the method in another Thread:

executor.execute(() -> {
    stageOfferUpdates();
}

and then procede and return 200?

  • Related