问号是开启任何一门科学的钥匙。——巴甫洛夫

之前介绍过spring-state-machine持久化

今天使用redis实现

首先是需要一个RedisStateMachineRepository

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import com.alibaba.nacos.common.utils.JacksonUtils;
import jakarta.annotation.Nonnull;
import jakarta.annotation.Resource;
import org.dromara.streamquery.stream.core.stream.Steam;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.statemachine.data.redis.RedisRepositoryStateMachine;
import org.springframework.statemachine.data.redis.RedisStateMachineRepository;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
public class StateMachineRedisRepository implements RedisStateMachineRepository {


@Resource
private StringRedisTemplate stringRedisTemplate;

@Nonnull
@Override
public <S extends RedisRepositoryStateMachine> S save(@Nonnull S entity) {
stringRedisTemplate.opsForHash()
.put(SmCacheConst.STATE_MACHINE, entity.getId(), serialize(entity));
return entity;
}

@Nonnull
@Override
public <S extends RedisRepositoryStateMachine> Iterable<S> saveAll(@Nonnull Iterable<S> entities) {
var map = Steam.of(entities).toMap(RedisRepositoryStateMachine::getId, this::serialize);
stringRedisTemplate.opsForHash().putAll(SmCacheConst.STATE_MACHINE, map);
return Steam.of(entities).toList();
}

@Nonnull
@Override
public Optional<RedisRepositoryStateMachine> findById(@Nonnull String s) {
return Optional.ofNullable(stringRedisTemplate.opsForHash().get(SmCacheConst.STATE_MACHINE, s)).map(this::deserialize);
}

@Override
public boolean existsById(@Nonnull String s) {
return stringRedisTemplate.opsForHash().hasKey(SmCacheConst.STATE_MACHINE, s);
}

@Nonnull
@Override
public Iterable<RedisRepositoryStateMachine> findAll() {
return Steam.of(stringRedisTemplate.opsForHash().values(SmCacheConst.STATE_MACHINE)).map(this::deserialize).toList();
}

@Nonnull
@Override
public Iterable<RedisRepositoryStateMachine> findAllById(@Nonnull Iterable<String> strings) {
var ids = Steam.<Object>of(strings).toList();
var list = stringRedisTemplate.opsForHash().multiGet(SmCacheConst.STATE_MACHINE, ids);
return Steam.of(list).map(this::deserialize).toList();
}


@Override
public long count() {
return stringRedisTemplate.opsForHash().size(SmCacheConst.STATE_MACHINE);
}

@Override
public void deleteById(@Nonnull String s) {
stringRedisTemplate.opsForHash().delete(SmCacheConst.STATE_MACHINE, s);
}

@Override
public void delete(@Nonnull RedisRepositoryStateMachine entity) {
stringRedisTemplate.opsForHash().delete(SmCacheConst.STATE_MACHINE, entity.getId());
}

@Override
public void deleteAllById(@Nonnull Iterable<? extends String> strings) {
var array = Steam.of(strings).toArray();
stringRedisTemplate.opsForHash().delete(SmCacheConst.STATE_MACHINE, array);
}

@Override
public void deleteAll(@Nonnull Iterable<? extends RedisRepositoryStateMachine> entities) {
var ids = Steam.of(entities).map(RedisRepositoryStateMachine::getId).toArray();
stringRedisTemplate.opsForHash().delete(SmCacheConst.STATE_MACHINE, ids);
}

@Override
public void deleteAll() {
stringRedisTemplate.opsForHash().delete(SmCacheConst.STATE_MACHINE);
}

private String serialize(Object entity) {
return JacksonUtils.toJson(entity);
}

private RedisRepositoryStateMachine deserialize(Object obj) {
if (obj instanceof String json) {
return JacksonUtils.toObj(json, RedisRepositoryStateMachine.class);
}
return null;
}
}

然后是配置:

1
2
3
4
5
6
7
8
9
10
11
12
@Bean
public StateMachineRuntimePersister<WordChainStateEnum, WordChainEventEnum, String>
stateMachineRuntimePersister(StateMachineRedisRepository stateMachineRedisRepository) {
return new RedisPersistingStateMachineInterceptor<>(stateMachineRedisRepository);
}

@Bean
public StateMachineService<WordChainStateEnum, WordChainEventEnum>
stateMachineService(StateMachineFactory<WordChainStateEnum, WordChainEventEnum> stateMachineFactory,
StateMachineRuntimePersister<WordChainStateEnum, WordChainEventEnum, String> stateMachineRuntimePersister) {
return new DefaultStateMachineService<>(stateMachineFactory, stateMachineRuntimePersister);
}

接下来是状态机处使用

1
2
3
4
5
6
7
8
9
StateMachineBuilder.Builder<WordChainStateEnum, WordChainEventEnum> builder = StateMachineBuilder.builder();

builder.configureConfiguration().withConfiguration().machineId(room);

builder.configureStates().withStates()
.initial(STATE_WAITING_FOR_READY)
.states(Set.of(WordChainStateEnum.values()));

builder.configureConfiguration().withPersistence().runtimePersister(stateMachineRuntimePersister);