parent
92f08c8a85
commit
f5429fd58c
@ -0,0 +1,92 @@
|
|||||||
|
package org.dromara.mes.config;
|
||||||
|
|
||||||
|
import org.dromara.mes.enums.PlanEventEnum;
|
||||||
|
import org.dromara.mes.enums.PlanStatusEnum;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.statemachine.config.EnableStateMachineFactory;
|
||||||
|
import org.springframework.statemachine.config.StateMachineConfigurerAdapter;
|
||||||
|
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
|
||||||
|
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;
|
||||||
|
|
||||||
|
import org.springframework.statemachine.guard.Guard;
|
||||||
|
import org.springframework.statemachine.action.Action;
|
||||||
|
|
||||||
|
import java.util.EnumSet;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@EnableStateMachineFactory
|
||||||
|
public class PlanStatusMachineConfig extends StateMachineConfigurerAdapter<PlanStatusEnum, PlanEventEnum> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void configure(StateMachineStateConfigurer<PlanStatusEnum, PlanEventEnum> states) throws Exception {
|
||||||
|
states
|
||||||
|
.withStates()
|
||||||
|
.initial(PlanStatusEnum.CREATED)
|
||||||
|
.states(EnumSet.allOf(PlanStatusEnum.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void configure(StateMachineTransitionConfigurer<PlanStatusEnum, PlanEventEnum> transitions) throws Exception {
|
||||||
|
transitions
|
||||||
|
.withExternal()
|
||||||
|
.source(PlanStatusEnum.CREATED)
|
||||||
|
.target(PlanStatusEnum.PLANNED)
|
||||||
|
.event(PlanEventEnum.PLAN)
|
||||||
|
.and()
|
||||||
|
.withExternal()
|
||||||
|
.source(PlanStatusEnum.PLANNED)
|
||||||
|
.target(PlanStatusEnum.IN_PROGRESS)
|
||||||
|
.event(PlanEventEnum.START)
|
||||||
|
.action(startAction())
|
||||||
|
.and()
|
||||||
|
.withExternal()
|
||||||
|
.source(PlanStatusEnum.IN_PROGRESS)
|
||||||
|
.target(PlanStatusEnum.PAUSED)
|
||||||
|
.event(PlanEventEnum.PAUSE)
|
||||||
|
.and()
|
||||||
|
.withExternal()
|
||||||
|
.source(PlanStatusEnum.PAUSED)
|
||||||
|
.target(PlanStatusEnum.IN_PROGRESS)
|
||||||
|
.event(PlanEventEnum.RESUME)
|
||||||
|
.and()
|
||||||
|
.withExternal()
|
||||||
|
.source(PlanStatusEnum.IN_PROGRESS)
|
||||||
|
.target(PlanStatusEnum.COMPLETED)
|
||||||
|
.event(PlanEventEnum.COMPLETE)
|
||||||
|
.guard(completionGuard())
|
||||||
|
.and()
|
||||||
|
.withExternal()
|
||||||
|
.source(PlanStatusEnum.CREATED)
|
||||||
|
.target(PlanStatusEnum.CANCELLED)
|
||||||
|
.event(PlanEventEnum.CANCEL)
|
||||||
|
.and()
|
||||||
|
.withExternal()
|
||||||
|
.source(PlanStatusEnum.PLANNED)
|
||||||
|
.target(PlanStatusEnum.CANCELLED)
|
||||||
|
.event(PlanEventEnum.CANCEL)
|
||||||
|
.and()
|
||||||
|
.withExternal()
|
||||||
|
.source(PlanStatusEnum.IN_PROGRESS)
|
||||||
|
.target(PlanStatusEnum.CANCELLED)
|
||||||
|
.event(PlanEventEnum.CANCEL);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public Action<PlanStatusEnum, PlanEventEnum> startAction() {
|
||||||
|
return context -> {
|
||||||
|
System.out.println("工单启动,准备资源...");
|
||||||
|
// 可注入其他Service
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public Guard<PlanStatusEnum, PlanEventEnum> completionGuard() {
|
||||||
|
return context -> {
|
||||||
|
// 检查是否允许完成
|
||||||
|
System.out.println("检查是否允许完成");
|
||||||
|
return true; // 示例:始终允许
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,39 @@
|
|||||||
|
package org.dromara.mes.listeners;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.dromara.mes.enums.PlanEventEnum;
|
||||||
|
import org.dromara.mes.enums.PlanStatusEnum;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.context.ApplicationEventPublisher;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.statemachine.listener.StateMachineListener;
|
||||||
|
import org.springframework.statemachine.listener.StateMachineListenerAdapter;
|
||||||
|
import org.springframework.statemachine.state.State;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author xins
|
||||||
|
* @Date 2025/4/15 14:07
|
||||||
|
* @Description:
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@Component
|
||||||
|
public class StatusMachineListeners extends StateMachineListenerAdapter<String, String>{
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void stateChanged(State<String, String> from, State<String, String> to) {
|
||||||
|
System.out.println("State changed from " + (from != null ? from.getId() : "null")
|
||||||
|
+ " to " + to.getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void stateEntered(State<String, String> state) {
|
||||||
|
System.out.println("Entered state: " + state.getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void stateExited(State<String, String> state) {
|
||||||
|
System.out.println("Exited state: " + state.getId());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue