BAEL-4322 add sample codes for dynamic delay setting (#10531)
Co-authored-by: Yavuz Tas <ytas@vwd.com>
This commit is contained in:
parent
5ccfcb196f
commit
b5032bd3d6
|
@ -0,0 +1,45 @@
|
|||
package com.baeldung.scheduling.dynamic;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.Date;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.Executors;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
import org.springframework.scheduling.annotation.SchedulingConfigurer;
|
||||
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan("com.baeldung.scheduling.dynamic")
|
||||
@EnableScheduling
|
||||
public class DynamicSchedulingConfig implements SchedulingConfigurer {
|
||||
|
||||
@Autowired
|
||||
private TickService tickService;
|
||||
|
||||
@Bean
|
||||
public Executor taskExecutor() {
|
||||
return Executors.newSingleThreadScheduledExecutor();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
|
||||
taskRegistrar.setScheduler(taskExecutor());
|
||||
taskRegistrar.addTriggerTask(
|
||||
() -> tickService.tick(),
|
||||
context -> {
|
||||
Optional<Date> lastCompletionTime =
|
||||
Optional.ofNullable(context.lastCompletionTime());
|
||||
Instant nextExecutionTime =
|
||||
lastCompletionTime.orElseGet(Date::new).toInstant()
|
||||
.plusMillis(tickService.getDelay());
|
||||
return Date.from(nextExecutionTime);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package com.baeldung.scheduling.dynamic;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class TickService {
|
||||
|
||||
private long delay = 0;
|
||||
|
||||
public long getDelay() {
|
||||
this.delay += 1000;
|
||||
System.out.println("delaying " + this.delay + " milliseconds...");
|
||||
return this.delay;
|
||||
}
|
||||
|
||||
public void tick() {
|
||||
final long now = System.currentTimeMillis() / 1000;
|
||||
System.out
|
||||
.println("schedule tasks with dynamic delay - " + now);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
package com.baeldung.scheduling;
|
||||
|
||||
import com.baeldung.scheduling.dynamic.DynamicSchedulingConfig;
|
||||
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 = {
|
||||
DynamicSchedulingConfig.class}, loader = AnnotationConfigContextLoader.class)
|
||||
public class DynamicSchedulingIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void testTickServiceTick() throws InterruptedException {
|
||||
Thread.sleep(6000);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue