The @Scheduled Annotation in Spring
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
package com.javadevjournal;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.scheduling.annotation.SchedulingConfigurer;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
|
||||
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
|
||||
|
||||
@Configuration
|
||||
public class CustomThreadPoolConfig implements SchedulingConfigurer {
|
||||
|
||||
private final int CUSTOM_POOL_SIZE = 5;
|
||||
|
||||
@Override
|
||||
public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
|
||||
|
||||
ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
|
||||
threadPoolTaskScheduler.setPoolSize(CUSTOM_POOL_SIZE);
|
||||
threadPoolTaskScheduler.setThreadNamePrefix("javadevjournal");
|
||||
threadPoolTaskScheduler.initialize();
|
||||
|
||||
//let's register our custom thread pool scheduler
|
||||
scheduledTaskRegistrar.setTaskScheduler(threadPoolTaskScheduler);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.javadevjournal.schedule.task;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class CronExpressionTaskExample {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(CronExpressionTaskExample.class);
|
||||
|
||||
@Scheduled(cron = "0 * * * * ?")
|
||||
public void scheduleTaskWithCronExpression() {
|
||||
LOG.info("Example to show how cron expression can be used");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.javadevjournal.schedule.task;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@Component
|
||||
public class FixedDelayTaskExample {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(FixedDelayTaskExample.class);
|
||||
|
||||
@Scheduled(fixedDelay = 2000)
|
||||
public void fixedDelayExample() {
|
||||
LOG.info("Hello from our Fixed delay method");
|
||||
try {
|
||||
TimeUnit.SECONDS.sleep(3);
|
||||
} catch (InterruptedException ie) {
|
||||
LOG.error("Got Interrupted {}", ie);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.javadevjournal.schedule.task;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class FixedRateScheduledTask {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(FixedRateScheduledTask.class);
|
||||
|
||||
@Scheduled(fixedRate = 1000)
|
||||
public void sayHello() {
|
||||
LOG.info("Hello from our simple scheduled method");
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.javadevjournal.schedule.task;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class TasksWithInitialDelayExample {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(TasksWithInitialDelayExample.class);
|
||||
|
||||
@Scheduled(fixedRate = 2000, initialDelay = 5000)
|
||||
public void scheduleTaskWithInitialDelay() {
|
||||
LOG.info("Fixed Rate Task with Initial Delay");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user