forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BAEL-4322 add sample codes for dynamic delay setting (eugenp#10531)
Co-authored-by: Yavuz Tas <[email protected]>
- Loading branch information
Showing
3 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
spring-scheduling/src/main/java/com/baeldung/scheduling/dynamic/DynamicSchedulingConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
); | ||
} | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
spring-scheduling/src/main/java/com/baeldung/scheduling/dynamic/TickService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
...ng-scheduling/src/test/java/com/baeldung/scheduling/DynamicSchedulingIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |