新增: 初始化提交

master
曹世达 7 months ago
commit fd3c83f8de

38
.gitignore vendored

@ -0,0 +1,38 @@
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store

@ -0,0 +1,60 @@
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.3</version>
<relativePath/>
</parent>
<groupId>space.caoshd</groupId>
<artifactId>state-machin</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>state-machine</name>
<properties>
<java.version>17</java.version>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.statemachine</groupId>
<artifactId>spring-statemachine-starter</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

@ -0,0 +1,13 @@
package space.caoshd.statemachine;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class StateMachineApplication {
public static void main(String[] args) {
SpringApplication.run(StateMachineApplication.class, args);
}
}

@ -0,0 +1,33 @@
package space.caoshd.statemachine.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.statemachine.config.EnableStateMachine;
import org.springframework.statemachine.config.StateMachineConfigurerAdapter;
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;
import org.springframework.statemachine.config.configurers.ExternalTransitionConfigurer;
import java.util.EnumSet;
@Configuration
@EnableStateMachine(name = "stateMachine")
public class Config extends StateMachineConfigurerAdapter<Status, Event> {
@Override
public void configure(StateMachineStateConfigurer<Status, Event> state) throws Exception {
state.withStates()
.initial(Status.INIT)
.states(EnumSet.allOf(Status.class));
}
@Override
public void configure(StateMachineTransitionConfigurer<Status, Event> transitions) throws Exception {
ExternalTransitionConfigurer<Status, Event> configurer = transitions.withExternal();
configurer.source(Status.INIT).target(Status.READY).event(Event.FETCH);
configurer.source(Status.INIT).target(Status.ERROR).event(Event.FETCH_ERROR);
configurer.source(Status.READY).target(Status.FINISHED).event(Event.HANDLE);
configurer.source(Status.INIT).target(Status.ERROR).event(Event.HANDLE_ERROR);
}
}

@ -0,0 +1,5 @@
package space.caoshd.statemachine.config;
public enum Event {
FETCH, HANDLE, FETCH_ERROR, HANDLE_ERROR, CALLBACK
}

@ -0,0 +1,22 @@
package space.caoshd.statemachine.config;
public enum Status {
INIT(0), READY(1), FINISHED(2), ERROR(-1);
private final Integer code;
private final String name;
Status(int code) {
this.code = code;
this.name = this.name();
}
public Integer getCode() {
return this.code;
}
public String getName() {
return this.name;
}
}

@ -0,0 +1,23 @@
package space.caoshd.statemachine.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.statemachine.StateMachine;
import org.springframework.stereotype.Component;
import space.caoshd.statemachine.config.Event;
import space.caoshd.statemachine.config.Status;
@Component
public class Processor {
@Autowired
private StateMachine<Status, Event> stateMachine;
public boolean process(Event e) {
Message<Event> message = MessageBuilder.withPayload(e).build();
return stateMachine.sendEvent(message);
}
}

@ -0,0 +1,41 @@
package space.caoshd.statemachine.service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.statemachine.annotation.OnTransition;
import org.springframework.stereotype.Component;
import space.caoshd.statemachine.config.Event;
@Slf4j
@Component
public class StateListener {
@OnTransition(source = "INIT", target = "READY")
public boolean fetch(Message<Event> message) {
MessageHeaders headers = message.getHeaders();
log.info("fetch");
return true;
}
@OnTransition(source = "READY", target = "FINISHED")
public boolean handle(Message<Event> message) {
MessageHeaders headers = message.getHeaders();
log.info("ready");
return true;
}
@OnTransition(source = "INIT", target = "ERROR")
public boolean fetchError(Message<Event> message) {
MessageHeaders headers = message.getHeaders();
log.info("fetchError");
return true;
}
@OnTransition(source = "READY", target = "ERROR")
public boolean handleError(Message<Event> message) {
MessageHeaders headers = message.getHeaders();
log.info("handleError");
return true;
}
}

@ -0,0 +1,20 @@
package space.caoshd.statemachine;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import space.caoshd.statemachine.config.Event;
import space.caoshd.statemachine.service.Processor;
@SpringBootTest
class StateMachineApplicationTests {
@Autowired
private Processor processor;
@Test
void contextLoads() {
processor.process(Event.FETCH);
}
}
Loading…
Cancel
Save