transition enricher
This commit is contained in:
33
state_machines/complex_multi_module_sm/.gitignore
vendored
Normal file
33
state_machines/complex_multi_module_sm/.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/
|
||||
33
state_machines/complex_multi_module_sm/domain/.gitignore
vendored
Normal file
33
state_machines/complex_multi_module_sm/domain/.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/
|
||||
24
state_machines/complex_multi_module_sm/domain/pom.xml
Normal file
24
state_machines/complex_multi_module_sm/domain/pom.xml
Normal file
@@ -0,0 +1,24 @@
|
||||
<?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>domain</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.statemachine</groupId>
|
||||
<artifactId>spring-statemachine-core</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,4 @@
|
||||
package click.kamil.domain;
|
||||
|
||||
public interface IEvent {
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package click.kamil.domain;
|
||||
|
||||
public interface IState {
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package click.kamil.domain;
|
||||
|
||||
public enum OrderEvent implements IEvent {
|
||||
PROCESS, COMPLETE, CANCEL
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package click.kamil.domain;
|
||||
|
||||
public enum OrderState implements IState {
|
||||
NEW, PROCESSING, COMPLETED, CANCELLED
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package click.kamil.domain;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.statemachine.config.EnableStateMachine;
|
||||
import org.springframework.statemachine.config.EnumStateMachineConfigurerAdapter;
|
||||
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
|
||||
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;
|
||||
|
||||
import java.util.EnumSet;
|
||||
|
||||
@Configuration
|
||||
@EnableStateMachine
|
||||
public class StateMachineConfig extends EnumStateMachineConfigurerAdapter<OrderState, OrderEvent> {
|
||||
|
||||
@Override
|
||||
public void configure(StateMachineStateConfigurer<OrderState, OrderEvent> states) throws Exception {
|
||||
states
|
||||
.withStates()
|
||||
.initial(OrderState.NEW)
|
||||
.states(EnumSet.allOf(OrderState.class));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configure(StateMachineTransitionConfigurer<OrderState, OrderEvent> transitions) throws Exception {
|
||||
transitions
|
||||
.withExternal()
|
||||
.source(OrderState.NEW).target(OrderState.PROCESSING).event(OrderEvent.PROCESS)
|
||||
.and()
|
||||
.withExternal()
|
||||
.source(OrderState.PROCESSING).target(OrderState.COMPLETED).event(OrderEvent.COMPLETE)
|
||||
.and()
|
||||
.withExternal()
|
||||
.source(OrderState.NEW).target(OrderState.CANCELLED).event(OrderEvent.CANCEL)
|
||||
.and()
|
||||
.withExternal()
|
||||
.source(OrderState.PROCESSING).target(OrderState.CANCELLED).event(OrderEvent.CANCEL);
|
||||
}
|
||||
}
|
||||
51
state_machines/complex_multi_module_sm/pom.xml
Normal file
51
state_machines/complex_multi_module_sm/pom.xml
Normal file
@@ -0,0 +1,51 @@
|
||||
<?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>
|
||||
|
||||
<groupId>click.kamil.complex</groupId>
|
||||
<artifactId>complex-multi-module-sm</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<modules>
|
||||
<module>domain</module>
|
||||
<module>service</module>
|
||||
<module>web</module>
|
||||
</modules>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>21</maven.compiler.source>
|
||||
<maven.compiler.target>21</maven.compiler.target>
|
||||
<spring-boot.version>3.2.0</spring-boot.version>
|
||||
<spring-statemachine.version>3.2.0</spring-statemachine.version>
|
||||
</properties>
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-dependencies</artifactId>
|
||||
<version>${spring-boot.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.statemachine</groupId>
|
||||
<artifactId>spring-statemachine-core</artifactId>
|
||||
<version>${spring-statemachine.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>click.kamil.complex</groupId>
|
||||
<artifactId>domain</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>click.kamil.complex</groupId>
|
||||
<artifactId>service</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
</project>
|
||||
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
|
||||
}
|
||||
}
|
||||
33
state_machines/complex_multi_module_sm/web/.gitignore
vendored
Normal file
33
state_machines/complex_multi_module_sm/web/.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/
|
||||
28
state_machines/complex_multi_module_sm/web/pom.xml
Normal file
28
state_machines/complex_multi_module_sm/web/pom.xml
Normal file
@@ -0,0 +1,28 @@
|
||||
<?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>web</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>click.kamil.complex</groupId>
|
||||
<artifactId>service</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-activemq</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,14 @@
|
||||
package click.kamil.web;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.jms.annotation.EnableJms;
|
||||
|
||||
@SpringBootApplication(scanBasePackages = "click.kamil")
|
||||
@EnableJms
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package click.kamil.web;
|
||||
|
||||
import click.kamil.domain.OrderEvent;
|
||||
import click.kamil.service.StateMachineService;
|
||||
import org.springframework.jms.annotation.JmsListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class JmsOrderListener {
|
||||
|
||||
private final StateMachineService stateMachineService;
|
||||
|
||||
// Constructor injection
|
||||
public JmsOrderListener(StateMachineService stateMachineService) {
|
||||
this.stateMachineService = stateMachineService;
|
||||
}
|
||||
|
||||
@JmsListener(destination = "order.process.queue")
|
||||
public void receiveProcessCommand(String message) {
|
||||
System.out.println("Received JMS message: " + message);
|
||||
stateMachineService.sendMessageWithOutbox(OrderEvent.PROCESS, event -> {
|
||||
System.out.println("JMS Triggered Faulty Outbox Action executed post-commit for event: " + event);
|
||||
});
|
||||
}
|
||||
|
||||
@JmsListener(destination = "order.cancel.queue")
|
||||
public void receiveCancelCommand(String message) {
|
||||
System.out.println("Received JMS message: " + message);
|
||||
stateMachineService.sendMessageWithProvider(() -> OrderEvent.CANCEL);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package click.kamil.web;
|
||||
|
||||
import click.kamil.domain.OrderEvent;
|
||||
import click.kamil.service.StateMachineService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/orders")
|
||||
public class OrderController {
|
||||
|
||||
@Autowired
|
||||
private StateMachineService stateMachineService;
|
||||
|
||||
@PostMapping("/process")
|
||||
public String processOrder() {
|
||||
stateMachineService.sendMessageWithOutbox(OrderEvent.PROCESS, event -> {
|
||||
System.out.println("Faulty Outbox Action executed post-commit for event: " + event);
|
||||
});
|
||||
return "Order process event sent within transaction";
|
||||
}
|
||||
|
||||
@PostMapping("/complete")
|
||||
public String completeOrder() {
|
||||
stateMachineService.sendMessage(OrderEvent.COMPLETE);
|
||||
return "Order complete event sent";
|
||||
}
|
||||
|
||||
@PostMapping("/cancel")
|
||||
public String cancelOrder() {
|
||||
stateMachineService.sendMessage(OrderEvent.CANCEL);
|
||||
return "Order cancel event sent";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user