This commit is contained in:
weixiansheng 2023-12-27 09:47:46 +08:00
parent 72c31c5517
commit 7d6891851d
2 changed files with 36 additions and 4 deletions

View File

@ -0,0 +1,14 @@
package com.ybw.constant;
/**
* redis前缀常量
*
* @author weixiansheng
* @version V1.0
* @className RedisPreConstant
* @date 2023/12/27
**/
public interface RedisPreConstant {
String STATE_MACHINE = "STATE:MACHINE";
}

View File

@ -1,5 +1,6 @@
package com.ybw.state;
import com.ybw.constant.RedisPreConstant;
import com.ybw.constant.RegEventEnum;
import com.ybw.constant.RegStatusEnum;
import com.ybw.entity.Order;
@ -38,20 +39,37 @@ public class StateTest {
@Test
public void testState() {
try {
//1订单数据
Order order = Order.builder()
.id(1L)
.name("张三")
.status(RegStatusEnum.CONNECTED)
.build();
//2获取redis key
String redisKey = getRedisKey(order);
//3启动状态机
stateMachine.start();
//尝试恢复状态机状态(read)
persister.restore(stateMachine, order.getId().toString());
//4尝试恢复状态机状态(read)
persister.restore(stateMachine, redisKey);
Message<RegEventEnum> message = MessageBuilder.withPayload(RegEventEnum.REGISTER).setHeader("order", order).build();
stateMachine.sendEvent(message);
//持久化状态机状态(write)
persister.persist(stateMachine, order.getId().toString());
//5持久化状态机状态(write)
persister.persist(stateMachine, redisKey);
} catch (Exception e) {
log.error("testState error:", e);
}
}
/**
* 获取redis key
*
* @param order
* @methodName: getRedisKey
* @return: java.lang.String
* @author: weixiansheng
* @date: 2023/12/27
**/
private static String getRedisKey(Order order) {
return RedisPreConstant.STATE_MACHINE + order.getId();
}
}