* Moved Conditional Scheduling to spring-scheduling module

This commit is contained in:
dupirefr
2020-02-09 11:01:39 +01:00
parent c0e1c4a0c5
commit b977f3ae60
9 changed files with 11 additions and 7 deletions

View File

@@ -3,4 +3,5 @@
- [The @Scheduled Annotation in Spring](https://www.baeldung.com/spring-scheduled-tasks)
- [Guide to Spring Retry](http://www.baeldung.com/spring-retry)
- [How To Do @Async in Spring](http://www.baeldung.com/spring-async)
- [Conditionally Enable Scheduled Jobs in Spring](https://www.baeldung.com/spring-scheduled-enabled-conditionally)

View File

@@ -27,6 +27,10 @@
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>

View File

@@ -0,0 +1,20 @@
package com.baeldung.scheduling.conditional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
@Configuration
public class ScheduleJobsByProfile {
private final static Logger LOG = LoggerFactory.getLogger(ScheduleJobsByProfile.class);
@Profile("prod")
@Bean
public ScheduledJob scheduledJob()
{
return new ScheduledJob("@Profile");
}
}

View File

@@ -0,0 +1,21 @@
package com.baeldung.scheduling.conditional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
public class ScheduledJob {
private String source;
public ScheduledJob(String source) {
this.source = source;
}
private final static Logger LOG = LoggerFactory.getLogger(ScheduledJob.class);
@Scheduled(fixedDelay = 60000)
public void cleanTempDir() {
LOG.info("Cleaning temp directory via {}", source);
}
}

View File

@@ -0,0 +1,28 @@
package com.baeldung.scheduling.conditional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;
@Configuration
public class ScheduledJobsWithBoolean {
private final static Logger LOG = LoggerFactory.getLogger(ScheduledJobsWithBoolean.class);
@Value("${jobs.enabled:true}")
private boolean isEnabled;
/**
* A scheduled job controlled via application property. The job always
* executes, but the logic inside is protected by a configurable boolean
* flag.
*/
@Scheduled(fixedDelay = 60000)
public void cleanTempDirectory() {
if(isEnabled) {
LOG.info("Cleaning temp directory via boolean flag");
}
}
}

View File

@@ -0,0 +1,20 @@
package com.baeldung.scheduling.conditional;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ScheduledJobsWithConditional
{
/**
* This uses @ConditionalOnProperty to conditionally create a bean, which itself
* is a scheduled job.
* @return ScheduledJob
*/
@Bean
@ConditionalOnProperty(value = "jobs.enabled", matchIfMissing = true, havingValue = "true")
public ScheduledJob runMyCronTask() {
return new ScheduledJob("@ConditionalOnProperty");
}
}

View File

@@ -0,0 +1,23 @@
package com.baeldung.scheduling.conditional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;
@Configuration
public class ScheduledJobsWithExpression
{
private final static Logger LOG =
LoggerFactory.getLogger(ScheduledJobsWithExpression.class);
/**
* A scheduled job controlled via application property. The job always
* executes, but the logic inside is protected by a configurable boolean
* flag.
*/
@Scheduled(cron = "${jobs.cronSchedule:-}")
public void cleanTempDirectory() {
LOG.info("Cleaning temp directory via placeholder");
}
}

View File

@@ -0,0 +1,16 @@
package com.baeldung.scheduling.conditional;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
public class SchedulingApplication {
public static void main(String[] args) {
SpringApplication.run(SchedulingApplication.class, args);
}
}