transition enricher
This commit is contained in:
33
state_machines/complex_multi_module_sm/service/.gitignore
vendored
Normal file
33
state_machines/complex_multi_module_sm/service/.gitignore
vendored
Normal file
@@ -0,0 +1,33 @@
|
||||
HELP.md
|
||||
target/
|
||||
.mvn/wrapper/maven-wrapper.jar
|
||||
!**/src/main/**/target/
|
||||
!**/src/test/**/target/
|
||||
|
||||
### STS ###
|
||||
.apt_generated
|
||||
.classpath
|
||||
.factorypath
|
||||
.project
|
||||
.settings
|
||||
.springBeans
|
||||
.sts4-cache
|
||||
|
||||
### IntelliJ IDEA ###
|
||||
.idea
|
||||
*.iws
|
||||
*.iml
|
||||
*.ipr
|
||||
|
||||
### NetBeans ###
|
||||
/nbproject/private/
|
||||
/nbbuild/
|
||||
/dist/
|
||||
/nbdist/
|
||||
/.nb-gradle/
|
||||
build/
|
||||
!**/src/main/**/build/
|
||||
!**/src/test/**/build/
|
||||
|
||||
### VS Code ###
|
||||
.vscode/
|
||||
32
state_machines/complex_multi_module_sm/service/pom.xml
Normal file
32
state_machines/complex_multi_module_sm/service/pom.xml
Normal file
@@ -0,0 +1,32 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>click.kamil.complex</groupId>
|
||||
<artifactId>complex-multi-module-sm</artifactId>
|
||||
<version>1.0.0</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>service</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>click.kamil.complex</groupId>
|
||||
<artifactId>domain</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.statemachine</groupId>
|
||||
<artifactId>spring-statemachine-core</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-tx</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,7 @@
|
||||
package click.kamil.service;
|
||||
|
||||
import click.kamil.domain.IEvent;
|
||||
|
||||
public interface OutboxAction<T extends IEvent> {
|
||||
void onCommit(T event);
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package click.kamil.service;
|
||||
|
||||
import click.kamil.domain.IEvent;
|
||||
import click.kamil.domain.IState;
|
||||
|
||||
public interface StateMachineService {
|
||||
<S extends IState, T extends IEvent> void sendMessage(T event);
|
||||
|
||||
<S extends IState, T extends IEvent, A extends OutboxAction<T>> void sendMessageWithOutbox(T event, A action);
|
||||
|
||||
// Quirky pattern: Bounded generic supplier instead of reflection
|
||||
<T extends click.kamil.domain.OrderEvent> void sendMessageWithProvider(java.util.function.Supplier<T> eventProvider);
|
||||
|
||||
// Quirky pattern 2: Generic Varargs
|
||||
<T extends IEvent> void sendEventsVarargs(T... events);
|
||||
|
||||
// Quirky pattern 3: Multiple generic bounds (Intersection Types)
|
||||
<T extends IEvent & java.io.Serializable & Cloneable> T processQuirkyEvent(T event);
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package click.kamil.service;
|
||||
|
||||
import click.kamil.domain.IEvent;
|
||||
import click.kamil.domain.IState;
|
||||
import click.kamil.domain.OrderEvent;
|
||||
import click.kamil.domain.OrderState;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.messaging.support.MessageBuilder;
|
||||
import org.springframework.statemachine.StateMachine;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.transaction.support.TransactionSynchronization;
|
||||
import org.springframework.transaction.support.TransactionSynchronizationManager;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@Service
|
||||
public class StateMachineServiceImpl implements StateMachineService {
|
||||
|
||||
@Autowired
|
||||
private StateMachine<OrderState, OrderEvent> stateMachine;
|
||||
|
||||
@Override
|
||||
public <S extends IState, T extends IEvent> void sendMessage(T event) {
|
||||
if (event instanceof OrderEvent) {
|
||||
stateMachine.sendEvent(Mono.just(MessageBuilder.withPayload((OrderEvent) event).build())).subscribe();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public <S extends IState, T extends IEvent, A extends OutboxAction<T>> void sendMessageWithOutbox(T event, A action) {
|
||||
sendMessage(event);
|
||||
|
||||
if (TransactionSynchronizationManager.isSynchronizationActive()) {
|
||||
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
|
||||
@Override
|
||||
public void afterCommit() {
|
||||
action.onCommit(event);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
action.onCommit(event);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T extends OrderEvent> void sendMessageWithProvider(java.util.function.Supplier<T> eventProvider) {
|
||||
T event = eventProvider.get();
|
||||
if (event != null) {
|
||||
stateMachine.sendEvent(Mono.just(MessageBuilder.withPayload((OrderEvent) event).build())).subscribe();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@SafeVarargs
|
||||
public final <T extends IEvent> void sendEventsVarargs(T... events) {
|
||||
// Quirky pattern 2: Iterating over generic varargs
|
||||
for (T event : events) {
|
||||
sendMessage(event);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T extends IEvent & java.io.Serializable & Cloneable> T processQuirkyEvent(T event) {
|
||||
// Quirky pattern 3: Multiple bounds, returning the same generic type
|
||||
sendMessage(event);
|
||||
return event; // returns something that is guaranteed to be IEvent, Serializable, AND Cloneable
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user