How to enable @Scheduled jobs by profile in spring?
You should have one bean per profile:
@Component
@Profile("prod")
public class ProdJob {
@Scheduled(cron = "${job.cron}")
public void runJob() {
}
}
@Component
@Profile("beta")
public class BetaJob {
@Scheduled(cron = "${job.cron}")
public void runJob() {
}
}
Is there a global flag in Spring Boot to disable all the scheduled jobs?
No. But I'd suggest you to define all job beans as conditional like following:
@ConditionalOnProperty(value="jobs.enabled", havingValue = "true", matchIfMissing = true)
@Component
@Log4j2
public class GreetJob {
...
}
In such case all job beans will be enabled by default, even if property jobs.enabled is not defined. Do disable all jobs at once you can define property in your application properties file
jobs.enabled=false
Or define this property in the command line:
-Djobs.enabled=false
How to have an asynchronous and non-concurrent scheduler in Spring?
I've faced a similar issue with concurrency while using spring scheduling. We had a lot of jobs all running on the same service and interfering with each other. We switched to using Quartz Scheduling - felt simpler than the Spring multithreaded scheduler with a bunch of other features that we wanted. This repo was really helpful.
https://gitlab.com/johnjvester/jpa-spec-with-quartz/-/blob/master/src/main/java/com/gitlab/johnjvester/jpaspec/config/QuartzConfig.java
Quartz scheduling also has the advantage of being persistent - when it starts up, it will trigger all the missed jobs. One can also alter cron statements programmatically. This might be an overkill for your use case but it is worth a look. :) Also, what Wasif said - use delays to define when the job should be run vs a cron expression and you're guaranteed a win even with Spring!
Time Scheduling in Spring Boot
You can achieve this with @Scheduled
annotation in one of your methods of the bean. In order to enable scheduling you need to put @EnableScheduling
annotation in one of your config classes, can be the main class:
@SpringBootApplication
@EnableScheduling
public class TestingApplication {
public static void main(String[] args) {
SpringApplication.run(TestingApplication.class, args);
}
}
Then you create a class, annotate it with @Component
and create a method with @Scheduled
annotation with a cron statement inside:
@Component
public class MyWorkerComponent {
@Autowired
private MyListChecker myListChecker;
@Scheduled(cron = "0 0 19 * * ?")
public void doTheListThingy() {
if (myListChecker.isTheListAvailable()) {
// your task logic
}
}
}
Disable @EnableScheduling on Spring Tests
If you don't want to use profiles, you can add flag that will enable/disable scheduling for the application
In your AppConfiguration
add this
@ConditionalOnProperty(
value = "app.scheduling.enable", havingValue = "true", matchIfMissing = true
)
@Configuration
@EnableScheduling
public static class SchedulingConfiguration {
}
and in your test just add this annotation to disable scheduling
@TestPropertySource(properties = "app.scheduling.enable=false")
Related Topics
Maven 3.8.0 Compiler - Fatal Error Compiling: Release Version 11 Not Supported
Mailconnectexception: Couldn't Connect to Host, Port: Smtp.Sendgrid.Net
I Want to Execute Code Every X Seconds, But Handler.Postdelayed Not Working
Parse a Uri String into Name-Value Collection
Efficient Way of Processing Large CSV File Using Java
Most Efficient Solution for Reading Clob to String, and String to Clob in Java
How to Check If the File Is Image
Android - Running a Background Task Every 15 Minutes, Even When Application Is Not Running
How to Get Response as Json With Responseentity in Java
Statefulbeantocsv With Column Headers
How to Remove All Special Character in a String Except Dot and Comma
Every Digits in Number of a Range Should Be Is Divisible by N
A Better Way to Convert Integer (May Be Null) to Int in Java
Hibernate Error - Querysyntaxexception: Users Is Not Mapped [From Users]
Regular Expression to Find Money Value