Added example code for usage and configuration of @Scheduled annotation.
This commit is contained in:
parent
fa4240311d
commit
fbe55d0584
|
@ -33,7 +33,7 @@
|
|||
</dependency>
|
||||
|
||||
<!-- persistence -->
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-core</artifactId>
|
||||
|
@ -118,7 +118,6 @@
|
|||
<version>${mockito.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
|
|
@ -0,0 +1,52 @@
|
|||
package org.baeldung.scheduling;
|
||||
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class ScheduledAnnotationExample {
|
||||
|
||||
@Scheduled(fixedDelay = 1000)
|
||||
public void scheduleFixedDelayTask() {
|
||||
System.out.println("Fixed delay task - " + System.currentTimeMillis() / 1000);
|
||||
}
|
||||
|
||||
@Scheduled(fixedDelayString = "${fixedDelay.in.milliseconds}")
|
||||
public void scheduleFixedDelayTaskUsingExpression() {
|
||||
System.out.println("Fixed delay task - " + System.currentTimeMillis() / 1000);
|
||||
}
|
||||
|
||||
@Scheduled(fixedDelay = 1000, initialDelay = 2000)
|
||||
public void scheduleFixedDelayWithInitialDelayTask() {
|
||||
System.out.println("Fixed delay task with one second initial delay - " + System.currentTimeMillis() / 1000);
|
||||
}
|
||||
|
||||
@Scheduled(fixedRate = 1000)
|
||||
public void scheduleFixedRateTask() {
|
||||
System.out.println("Fixed rate task - " + System.currentTimeMillis() / 1000);
|
||||
}
|
||||
|
||||
@Scheduled(fixedRateString = "${fixedRate.in.milliseconds}")
|
||||
public void scheduleFixedRateTaskUsingExpression() {
|
||||
System.out.println("Fixed rate task - " + System.currentTimeMillis() / 1000);
|
||||
}
|
||||
|
||||
@Scheduled(fixedDelay = 1000, initialDelay = 100)
|
||||
public void scheduleFixedRateWithInitialDelayTask() {
|
||||
System.out.println("Fixed delay task with one second initial delay - " + System.currentTimeMillis() / 1000);
|
||||
}
|
||||
|
||||
/**
|
||||
* Scheduled task is executed at 10:15 AM on the 15th day of every month
|
||||
*/
|
||||
@Scheduled(cron = "0 15 10 15 * ?")
|
||||
public void scheduleTaskUsingCronExpression() {
|
||||
System.out.println("schedule tasks using cron expressions - " + System.currentTimeMillis() / 1000);
|
||||
}
|
||||
|
||||
@Scheduled(cron = "${cron.expression}")
|
||||
public void scheduleTaskUsingExternalizedCronExpression() {
|
||||
System.out.println("schedule tasks using externalized cron expressions - " + System.currentTimeMillis() / 1000);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
package org.baeldung.scheduling;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
|
||||
@Configuration
|
||||
@EnableScheduling
|
||||
@ComponentScan("com.baeldung.spring.integration")
|
||||
@PropertySource("classpath:springIntegration.properties")
|
||||
public class SpringSchedulingConfig {
|
||||
|
||||
@Bean
|
||||
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
|
||||
return new PropertySourcesPlaceholderConfigurer();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:task="http://www.springframework.org/schema/task" xmlns:context="http://www.springframework.org/schema/context"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
|
||||
|
||||
<!-- Configure the scheduler -->
|
||||
<task:scheduler id="myScheduler" pool-size="10" />
|
||||
|
||||
<bean id="myscheduler" name="myscheduler"
|
||||
class="org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler">
|
||||
</bean>
|
||||
|
||||
<!-- Configure the fixedDealy, fixedRate or cron based schduled tasks -->
|
||||
<task:scheduled-tasks scheduler="myScheduler">
|
||||
<task:scheduled ref="beanA" method="methodA" fixed-delay="5000" initial-delay="1000" />
|
||||
<task:scheduled ref="beanB" method="methodB" fixed-rate="5000" />
|
||||
<task:scheduled ref="beanC" method="methodC" cron="*/5 * * * * MON-FRI" />
|
||||
</task:scheduled-tasks>
|
||||
</beans>
|
|
@ -0,0 +1,3 @@
|
|||
cron.expression=0 15 10 15 * ?
|
||||
fixedRate.in.millisecons=1000
|
||||
fixedDelay.in.millisecons=1000
|
|
@ -0,0 +1,18 @@
|
|||
package org.baeldung.scheduling;
|
||||
|
||||
import org.baeldung.scheduling.SpringSchedulingConfig;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { SpringSchedulingConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||
public class ScheduledAnnotationExampleTest {
|
||||
|
||||
@Test
|
||||
public void testScheduledAnnotation() throws InterruptedException {
|
||||
Thread.sleep(20000);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue