Skip to content

Commit fd941be

Browse files
committed
- FixedIntervalRetryStrategy
- Added SchedulerCustomizer
1 parent 7356cdd commit fd941be

File tree

11 files changed

+78
-15
lines changed

11 files changed

+78
-15
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## v1.5.2 - (2025-01-13)
4+
5+
- FixedIntervalRetryStrategy
6+
- Added SchedulerCustomizer
7+
38
## v1.5.1 - (2025-01-12)
49

510
- filter trigger by status

RUN_AND_BUILD.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
mvn versions:display-dependency-updates
2-
mvn versions:set -DnewVersion=1.5.1 -DgenerateBackupPoms=false
3-
git tag -a v1.5.1 -m "v1.5.1 release"
4-
mvn versions:set -DnewVersion=1.5.2-SNAPSHOT -DgenerateBackupPoms=false
2+
mvn versions:set -DnewVersion=1.5.2 -DgenerateBackupPoms=false
3+
git tag -a v1.5.2 -m "v1.5.2 release"
4+
mvn versions:set -DnewVersion=1.5.3-SNAPSHOT -DgenerateBackupPoms=false
55

66
## postgres
77

core/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>org.sterl.spring</groupId>
88
<artifactId>spring-persistent-tasks-root</artifactId>
9-
<version>1.5.2-SNAPSHOT</version>
9+
<version>1.5.2</version>
1010
<relativePath>../pom.xml</relativePath>
1111
</parent>
1212

core/src/main/java/org/sterl/spring/persistent_tasks/api/RetryStrategy.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,34 @@ public OffsetDateTime retryAt(int executionCount, Exception error) {
111111
return OffsetDateTime.now().plus(scalingFactor * executionCount, unit);
112112
}
113113
}
114+
115+
/**
116+
* A retry strategy that determines the next retry time by adding a fixed
117+
* interval to the current time in the specified temporal unit.
118+
*
119+
* <p>This strategy can be used to create retry intervals that remain constant
120+
* regardless of the number of attempts, providing a uniform delay between retries.</p>
121+
*
122+
* <p>Example:
123+
* If {@code interval = 5}, {@code unit = ChronoUnit.SECONDS}, each retry will
124+
* be scheduled after 5 seconds from the current time.</p>
125+
*
126+
* <p>Note: The retry attempts will stop once the maximum execution count
127+
* ({@code maxExecutionCount}) is reached.</p>
128+
*/
129+
@RequiredArgsConstructor
130+
class FixedIntervalRetryStrategy implements RetryStrategy {
131+
private final int maxExecutionCount;
132+
private final TemporalUnit unit;
133+
private final int interval;
134+
135+
@Override
136+
public boolean shouldRetry(int executionCount, Exception error) {
137+
return maxExecutionCount > executionCount;
138+
}
139+
@Override
140+
public OffsetDateTime retryAt(int executionCount, Exception error) {
141+
return OffsetDateTime.now().plus(interval, unit);
142+
}
143+
}
114144
}

core/src/main/java/org/sterl/spring/persistent_tasks/scheduler/config/SchedulerConfig.java

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.net.InetAddress;
44
import java.net.UnknownHostException;
5+
import java.util.Optional;
56

67
import org.springframework.beans.factory.annotation.Value;
78
import org.springframework.boot.sql.init.dependency.DependsOnDatabaseInitialization;
@@ -23,6 +24,22 @@
2324
@Slf4j
2425
public class SchedulerConfig {
2526

27+
public interface SchedulerCustomizer {
28+
default String name() {
29+
try {
30+
final var ip = InetAddress.getLocalHost();
31+
String name = ip.getHostName();
32+
33+
if (name == null) {
34+
name = ip.toString();
35+
}
36+
return name;
37+
} catch (Exception e) {
38+
throw new RuntimeException(e);
39+
}
40+
}
41+
}
42+
2643
@ConditionalSchedulerServiceByProperty
2744
@Primary
2845
@DependsOnDatabaseInitialization
@@ -31,15 +48,12 @@ SchedulerService schedulerService(
3148
TriggerService triggerService,
3249
@Value("${spring.persistent-tasks.max-threads:10}") int maxThreads,
3350
EditSchedulerStatusComponent editSchedulerStatus,
51+
Optional<SchedulerCustomizer> customizer,
3452
TransactionTemplate trx) throws UnknownHostException {
53+
54+
customizer = customizer.isEmpty() ? Optional.of(new SchedulerCustomizer() {}) : customizer;
3555

36-
final var ip = InetAddress.getLocalHost();
37-
String name = ip.getHostName();
38-
39-
if (name == null) {
40-
name = ip.toString();
41-
}
42-
return new SchedulerService(name, triggerService,
56+
return new SchedulerService(customizer.get().name(), triggerService,
4357
new TaskExecutorComponent(triggerService, maxThreads),
4458
editSchedulerStatus, trx);
4559
}

db/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>org.sterl.spring</groupId>
88
<artifactId>spring-persistent-tasks-root</artifactId>
9-
<version>1.5.2-SNAPSHOT</version>
9+
<version>1.5.2</version>
1010
<relativePath>../pom.xml</relativePath>
1111
</parent>
1212

example/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>org.sterl.spring</groupId>
88
<artifactId>spring-persistent-tasks-root</artifactId>
9-
<version>1.5.2-SNAPSHOT</version>
9+
<version>1.5.2</version>
1010
<relativePath>../pom.xml</relativePath>
1111
</parent>
1212

example/src/main/java/org/sterl/spring/example_app/ExampleApplication.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.sterl.spring.persistent_tasks.scheduler.SchedulerService;
2323
import org.sterl.spring.persistent_tasks.scheduler.component.EditSchedulerStatusComponent;
2424
import org.sterl.spring.persistent_tasks.scheduler.component.TaskExecutorComponent;
25+
import org.sterl.spring.persistent_tasks.scheduler.config.SchedulerConfig.SchedulerCustomizer;
2526
import org.sterl.spring.persistent_tasks.trigger.TriggerService;
2627
import org.sterl.spring.persistent_tasks_ui.EnableSpringPersistentTasksUI;
2728

@@ -36,6 +37,16 @@ public static void main(String[] args) {
3637
SpringApplication.run(ExampleApplication.class, args);
3738
}
3839

40+
@Bean
41+
SchedulerCustomizer SchedulerCustomizer() {
42+
return new SchedulerCustomizer() {
43+
@Override
44+
public String name() {
45+
return "Test-Scheduler";
46+
}
47+
};
48+
}
49+
3950
@Bean
4051
GroupedOpenApi exampleAppApi() {
4152
return GroupedOpenApi.builder()

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
<groupId>org.sterl.spring</groupId>
1313
<artifactId>spring-persistent-tasks-root</artifactId>
14-
<version>1.5.2-SNAPSHOT</version>
14+
<version>1.5.2</version>
1515
<packaging>pom</packaging>
1616

1717
<inceptionYear>2024</inceptionYear>

ui/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>org.sterl.spring</groupId>
88
<artifactId>spring-persistent-tasks-root</artifactId>
9-
<version>1.5.2-SNAPSHOT</version>
9+
<version>1.5.2</version>
1010
<relativePath>../pom.xml</relativePath>
1111
</parent>
1212

0 commit comments

Comments
 (0)