commit fd3c83f8de56278cc9885018fc1fe0b51e37c735 Author: caoshd Date: Wed Mar 6 07:58:21 2024 +0800 新增: 初始化提交 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5ff6309 --- /dev/null +++ b/.gitignore @@ -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 \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..e145b10 --- /dev/null +++ b/pom.xml @@ -0,0 +1,60 @@ + + + 4.0.0 + + + org.springframework.boot + spring-boot-starter-parent + 3.2.3 + + + + space.caoshd + state-machin + 1.0.0-SNAPSHOT + + state-machine + + + 17 + 17 + 17 + UTF-8 + + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.statemachine + spring-statemachine-starter + 4.0.0 + + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.projectlombok + lombok + provided + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/src/main/java/space/caoshd/statemachine/StateMachineApplication.java b/src/main/java/space/caoshd/statemachine/StateMachineApplication.java new file mode 100644 index 0000000..7317202 --- /dev/null +++ b/src/main/java/space/caoshd/statemachine/StateMachineApplication.java @@ -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); + } + +} diff --git a/src/main/java/space/caoshd/statemachine/config/Config.java b/src/main/java/space/caoshd/statemachine/config/Config.java new file mode 100644 index 0000000..11ef3ed --- /dev/null +++ b/src/main/java/space/caoshd/statemachine/config/Config.java @@ -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 { + + @Override + public void configure(StateMachineStateConfigurer state) throws Exception { + state.withStates() + .initial(Status.INIT) + .states(EnumSet.allOf(Status.class)); + } + + @Override + public void configure(StateMachineTransitionConfigurer transitions) throws Exception { + ExternalTransitionConfigurer 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); + } + + +} diff --git a/src/main/java/space/caoshd/statemachine/config/Event.java b/src/main/java/space/caoshd/statemachine/config/Event.java new file mode 100644 index 0000000..483b3b9 --- /dev/null +++ b/src/main/java/space/caoshd/statemachine/config/Event.java @@ -0,0 +1,5 @@ +package space.caoshd.statemachine.config; + +public enum Event { + FETCH, HANDLE, FETCH_ERROR, HANDLE_ERROR, CALLBACK +} diff --git a/src/main/java/space/caoshd/statemachine/config/Status.java b/src/main/java/space/caoshd/statemachine/config/Status.java new file mode 100644 index 0000000..d4e20e6 --- /dev/null +++ b/src/main/java/space/caoshd/statemachine/config/Status.java @@ -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; + } +} diff --git a/src/main/java/space/caoshd/statemachine/service/Processor.java b/src/main/java/space/caoshd/statemachine/service/Processor.java new file mode 100644 index 0000000..ce8a5a2 --- /dev/null +++ b/src/main/java/space/caoshd/statemachine/service/Processor.java @@ -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 stateMachine; + + public boolean process(Event e) { + Message message = MessageBuilder.withPayload(e).build(); + return stateMachine.sendEvent(message); + } + + +} diff --git a/src/main/java/space/caoshd/statemachine/service/StateListener.java b/src/main/java/space/caoshd/statemachine/service/StateListener.java new file mode 100644 index 0000000..178d0e0 --- /dev/null +++ b/src/main/java/space/caoshd/statemachine/service/StateListener.java @@ -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 message) { + MessageHeaders headers = message.getHeaders(); + log.info("fetch"); + return true; + } + + @OnTransition(source = "READY", target = "FINISHED") + public boolean handle(Message message) { + MessageHeaders headers = message.getHeaders(); + log.info("ready"); + return true; + } + + @OnTransition(source = "INIT", target = "ERROR") + public boolean fetchError(Message message) { + MessageHeaders headers = message.getHeaders(); + log.info("fetchError"); + return true; + } + + @OnTransition(source = "READY", target = "ERROR") + public boolean handleError(Message message) { + MessageHeaders headers = message.getHeaders(); + log.info("handleError"); + return true; + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/main/resources/application.properties @@ -0,0 +1 @@ + diff --git a/src/test/java/space/caoshd/statemachine/StateMachineApplicationTests.java b/src/test/java/space/caoshd/statemachine/StateMachineApplicationTests.java new file mode 100644 index 0000000..abf5173 --- /dev/null +++ b/src/test/java/space/caoshd/statemachine/StateMachineApplicationTests.java @@ -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); + + } + +}