I’m using Spring Boot 2.7
I would like to execute a process 120 (or another value stablished in application.yml
) seconds after the microservice has started.
This is my code:
<code>import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
@Component
@Slf4j
public class OneTimeProcessRunner {
private final AtomicBoolean processLaunched = new AtomicBoolean(false);
@Async
@EventListener(ApplicationReadyEvent.class)
public void onApplicationReady() {
this.scheduleProcess();
}
@Scheduled(initialDelay = 120, fixedDelay = 120, timeUnit = TimeUnit.SECONDS)
public void scheduleProcess() {
if (processLaunched.compareAndSet(false, true)) {
this.runSingleProcess();
}
}
@Async
public void runSingleProcess() {
log.info("Running One time Single Process.");
}
}
</code>
<code>import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
@Component
@Slf4j
public class OneTimeProcessRunner {
private final AtomicBoolean processLaunched = new AtomicBoolean(false);
@Async
@EventListener(ApplicationReadyEvent.class)
public void onApplicationReady() {
this.scheduleProcess();
}
@Scheduled(initialDelay = 120, fixedDelay = 120, timeUnit = TimeUnit.SECONDS)
public void scheduleProcess() {
if (processLaunched.compareAndSet(false, true)) {
this.runSingleProcess();
}
}
@Async
public void runSingleProcess() {
log.info("Running One time Single Process.");
}
}
</code>
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
@Component
@Slf4j
public class OneTimeProcessRunner {
private final AtomicBoolean processLaunched = new AtomicBoolean(false);
@Async
@EventListener(ApplicationReadyEvent.class)
public void onApplicationReady() {
this.scheduleProcess();
}
@Scheduled(initialDelay = 120, fixedDelay = 120, timeUnit = TimeUnit.SECONDS)
public void scheduleProcess() {
if (processLaunched.compareAndSet(false, true)) {
this.runSingleProcess();
}
}
@Async
public void runSingleProcess() {
log.info("Running One time Single Process.");
}
}
The process should be executed only once!
I would like to set a solution without using cron.
Is this not possible using @Scheduled?
How I can programatically do it?