Start a fixed delayed schedular when a method start and stop when finishes

I have a Spring Boot web application.

I want to start a fixedDelayed scheduled job when a method starts. And stop scheduled job when someMethod method finished. I read the documentation but I couldn’t understand how. I want to wirte someting to db every 30 second while someMethod is still working.

Here is the pseudo code. Anyone has an idea?

public void someMethod() {

    //Start scheduledLogger() to work every 30 seconds

    //...
    //Do something taking long time

    //Stop scheduledLogger() job
}

private void scheduledLogger() {
    //Log to database
}


Source: java

Leave a Reply