Skip to content

Commit f48002d

Browse files
authored
Transactional annotation support (#6)
* simpler class hierarchie and added test to find the Transactional annotation * added transaction behavior test * added detection for Transactional methods * added test for REQUIRES_NEW * @transactional Annotation support * PersistentTask instead of Task or SpringBeanTask
1 parent 9fb9775 commit f48002d

37 files changed

+532
-264
lines changed

CHANGELOG.md

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

3+
## v1.4.0 - (2025-01-02)
4+
5+
- @Transactional Annotation support
6+
- PersistentTask instead of Task or SpringBeanTask
7+
8+
9+
## v1.3.1 - (2025-01-02)
10+
11+
- Bugfixes
12+
- Sprign Transaction Template support
13+
314
## v1.3.0 - (2025-01-01)
415

516
- MariaDB support

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public class ExampleApplication {
5959
@Component(BuildVehicleTask.NAME)
6060
@RequiredArgsConstructor
6161
@Slf4j
62-
public class BuildVehicleTask implements SpringBeanTask<Vehicle> {
62+
public class BuildVehicleTask implements PersistentTask<Vehicle> {
6363

6464
private static final String NAME = "buildVehicleTask";
6565
public static final TaskId<Vehicle> ID = new TaskId<>(NAME);
@@ -109,11 +109,15 @@ Simple task will use defaults:
109109

110110
```java
111111
@Bean
112-
SpringBeanTask<Vehicle> task1(VehicleHttpConnector vehicleHttpConnector) {
112+
PersistentTask<Vehicle> task1(VehicleHttpConnector vehicleHttpConnector) {
113113
return v -> vehicleHttpConnector.send(v);
114114
}
115115
```
116116

117+
### Task Transaction Management
118+
119+
[Transaction-Management Task](https://github.com/sterlp/spring-persistent-tasks/wiki/Transaction-Management)
120+
117121
## Queue a task execution
118122

119123
### Direct usage of the `TriggerService` or `PersistentTaskService`.

RUN_AND_BUILD.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
mvn versions:display-dependency-updates
2-
mvn versions:set -DnewVersion=1.3.1 -DgenerateBackupPoms=false
3-
mvn versions:set -DnewVersion=1.3.2-SNAPSHOT -DgenerateBackupPoms=false
2+
mvn versions:set -DnewVersion=1.4.0-SNAPSHOT -DgenerateBackupPoms=false
3+
mvn versions:set -DnewVersion=1.4.0 -DgenerateBackupPoms=false
44

55
## postgres
66
docker run --name pg-container -e POSTGRES_USER=sa -e POSTGRES_PASSWORD=veryStrong123 -p 5432:5432 -d postgres

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.3.2-SNAPSHOT</version>
9+
<version>1.4.0-SNAPSHOT</version>
1010
<relativePath>../pom.xml</relativePath>
1111
</parent>
1212

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

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

66
/**
77
/**
8-
* For any registered task a task trigger represent one unit of work, executing this task once.
8+
* For any registered persistentTask a persistentTask trigger represent one unit of work, executing this persistentTask once.
99
* @param <T> state type which has to be of {@link Serializable}
1010
*/
1111
public record AddTriggerRequest<T extends Serializable>(
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.sterl.spring.persistent_tasks.api;
2+
3+
import java.io.Serializable;
4+
5+
/**
6+
* A Spring persistent task whose state is saved in a {@link Trigger}.
7+
*
8+
* <p>This interface defines a task that accepts a state of type <code>T</code> and
9+
* provides default implementations for retry strategies.
10+
*
11+
* @param <T> the type of the state, which must be {@link Serializable}
12+
*/
13+
@FunctionalInterface
14+
public interface PersistentTask<T extends Serializable> {
15+
void accept(T state);
16+
17+
default RetryStrategy retryStrategy() {
18+
return RetryStrategy.THREE_RETRIES;
19+
}
20+
21+
/**
22+
* Whether the persistentTask is transaction or not. If <code>true</code> the execution
23+
* is wrapped into the default transaction template together with the state update
24+
* and the following events:
25+
* <ol>
26+
* <li>org.sterl.spring.persistent_tasks.trigger.event.TriggerRunningEvent</li>
27+
* <li>org.sterl.spring.persistent_tasks.trigger.event.TriggerSuccessEvent</li>
28+
* </ol>
29+
* @return {@code true} if the persistentTask is transactional; {@code false} otherwise.
30+
*/
31+
default boolean isTransactional() {
32+
return false;
33+
}
34+
}
Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,12 @@
11
package org.sterl.spring.persistent_tasks.api;
22

33
import java.io.Serializable;
4-
import java.util.function.Consumer;
54

5+
/**
6+
* Same as {@link PersistentTask}
7+
*/
8+
@Deprecated
69
@FunctionalInterface
7-
public interface SpringBeanTask<T extends Serializable> extends Consumer<T> {
8-
@Override
9-
void accept(T state);
10-
11-
default RetryStrategy retryStrategy() {
12-
return RetryStrategy.THREE_RETRIES;
13-
}
10+
public interface SpringBeanTask<T extends Serializable> extends PersistentTask<T> {
1411

15-
/**
16-
* Whether the task is transaction or not. If <code>true</code> the execution
17-
* is wrapped into the default transaction template together with the state update
18-
* and the following events:
19-
* <ol>
20-
* <li>org.sterl.spring.persistent_tasks.trigger.event.TriggerRunningEvent</li>
21-
* <li>org.sterl.spring.persistent_tasks.trigger.event.TriggerSuccessEvent</li>
22-
* <li>org.sterl.spring.persistent_tasks.trigger.event.TriggerFailedEvent</li>
23-
* </ol>
24-
* @return {@code true} if the task is transactional; {@code false} otherwise.
25-
*/
26-
default boolean isTransactional() {
27-
return false;
28-
}
2912
}

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

Lines changed: 0 additions & 13 deletions
This file was deleted.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import lombok.RequiredArgsConstructor;
99

1010
/**
11-
* Represents the ID of a task, which is currently not running.
11+
* Represents the ID of a persistentTask, which is currently not running.
1212
*/
1313
public record TaskId<T extends Serializable>(String name) implements Serializable {
1414

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.sterl.spring.persistent_tasks.api;
2+
3+
import java.io.Serializable;
4+
5+
/**
6+
* Similar to {@link PersistentTask} but specifically for transactional workloads.
7+
* Use this interface when the task execution should be wrapped in a transaction.
8+
*
9+
* <p>This interface ensures that the task's execution is transactional, meaning that it will
10+
* be executed within a transaction context, along with the state update and the dispatching of
11+
* relevant events.
12+
*
13+
* @param <T> the type of the state, which must be {@link Serializable}
14+
*/
15+
@FunctionalInterface
16+
public interface TransactionalTask<T extends Serializable> extends PersistentTask<T> {
17+
/**
18+
* Whether the persistentTask is transaction or not. If <code>true</code> the execution
19+
* is wrapped into the default transaction template together with the state update
20+
* and the following events:
21+
* <ol>
22+
* <li>org.sterl.spring.persistent_tasks.trigger.event.TriggerRunningEvent</li>
23+
* <li>org.sterl.spring.persistent_tasks.trigger.event.TriggerSuccessEvent</li>
24+
* </ol>
25+
* @return {@code true} if the persistentTask is transactional; {@code false} otherwise.
26+
*/
27+
default boolean isTransactional() {
28+
return true;
29+
}
30+
}

0 commit comments

Comments
 (0)