commit
04b35f1b68
1
pom.xml
1
pom.xml
|
@ -716,7 +716,6 @@
|
|||
<module>spring-rest-simple</module>
|
||||
<module>spring-resttemplate</module>
|
||||
<module>spring-roo</module>
|
||||
|
||||
<module>spring-security-acl</module>
|
||||
<module>spring-security-angular/server</module>
|
||||
<module>spring-security-cache-control</module>
|
||||
|
|
|
@ -0,0 +1,20 @@
|
|||
package com.baeldung.scheduling;
|
||||
|
||||
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");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package com.baeldung.scheduling;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
package com.baeldung.scheduling;
|
||||
|
||||
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");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
package com.baeldung.scheduling;
|
||||
|
||||
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");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package com.baeldung.scheduling;
|
||||
|
||||
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");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package com.baeldung.scheduling;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue