检察院网站建设自查,网页制作与网站建设初学者必看教程,动漫设计与制作主修课程有哪些,网站访客统计代码使用 spring-statemachine 状态机持久化时#xff0c;可以通过内存、spring-statemachine-redis 或 spring-statemachine-data-jpa 现有方式持久化处理。
因项目审核操作记录频繁#xff0c;数据量大#xff0c;使用 内存 或 spring-statemachine-redis 模式不可取#xf…使用 spring-statemachine 状态机持久化时可以通过内存、spring-statemachine-redis 或 spring-statemachine-data-jpa 现有方式持久化处理。
因项目审核操作记录频繁数据量大使用 内存 或 spring-statemachine-redis 模式不可取而项目使用的是 MyBatis使用 spring-statemachine-data-jpa 也不合适需要自定义实现简述步骤如下
一、引入依赖 !--状态机--dependencygroupIdorg.springframework.statemachine/groupIdartifactIdspring-statemachine-starter/artifactIdversion2.2.3.RELEASE/version/dependencydependencygroupIdorg.springframework.statemachine/groupIdartifactIdspring-statemachine-kryo/artifactIdversion1.2.14.RELEASE/version/dependency
二、创建持久化记录存储表
CREATE TABLE state_machine_context (id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 自增主键,machine_type VARCHAR (32) DEFAULT COMMENT 状态机类型,machine_id VARCHAR (36) DEFAULT COMMENT 状态机ID,machine_data TINYBLOB COMMENT 状态机数据,machine_state VARCHAR (32) DEFAULT COMMENT 状态机状态,machine_event VARCHAR (36) DEFAULT COMMENT 状态机事件,create_date DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 创建时间,update_date DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 更新时间,PRIMARY KEY (id),KEY idx_machine_id (machine_id)
) ENGINE INNODB COMMENT 状态机上下文
关键字段说明
machine_type标记业务类型如订单业务、用户业务macheine_id业务数据ID如订单ID、用户IDmachine_data状态机二进制数据
其它字段可根据自己业务需求自定义
三、自定义持久化类即实现接口 StateMachinePersist
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.messaging.MessageHeaders;
import org.springframework.statemachine.StateMachineContext;
import org.springframework.statemachine.StateMachinePersist;
import org.springframework.statemachine.kryo.MessageHeadersSerializer;
import org.springframework.statemachine.kryo.StateMachineContextSerializer;
import org.springframework.statemachine.kryo.UUIDSerializer;import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.Date;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;/*** author songjianyong*/
public class CustomStateMachinePersistS, E implements StateMachinePersistS, E, PairString, String {private static final ThreadLocalKryo KRYO_THREAD_LOCAL ThreadLocal.withInitial(() - {Kryo kryo new Kryo();kryo.addDefaultSerializer(StateMachineContext.class, new StateMachineContextSerializer());kryo.addDefaultSerializer(MessageHeaders.class, new MessageHeadersSerializer());kryo.addDefaultSerializer(UUID.class, new UUIDSerializer());return kryo;});private byte[] serialize(StateMachineContextS, E context) {Kryo kryo KRYO_THREAD_LOCAL.get();ByteArrayOutputStream out new ByteArrayOutputStream();Output output new Output(out);kryo.writeObject(output, context);output.close();return out.toByteArray();}SuppressWarnings(unchecked)private StateMachineContextS, E deserialize(byte[] data) {if (data null || data.length 0) {return null;}Kryo kryo KRYO_THREAD_LOCAL.get();ByteArrayInputStream in new ByteArrayInputStream(data);Input input new Input(in);return kryo.readObject(input, StateMachineContext.class);}private final StateMachineContextDao stateMachineContextDao;public SongStateMachinePersist(StateMachineContextDao stateMachineContextDao) {this.stateMachineContextDao stateMachineContextDao;}Overridepublic void write(StateMachineContextS, E context, PairString, String pair) throws Exception {byte[] machineData serialize(context);String machineId pair.getKey();String machineType pair.getValue();StateMachineContextEntity stateMachineContexEntity stateMachinePersistDao.findByMachineIdAndMachineType(machineId, machineType);if (Objects.nonNull(stateMachineContexEntity)) {stateMachineContexEntity.setMachineData(machineData);stateMachineContexEntity.setMachineState(Optional.ofNullable(context.getState()).map(Objects::toString).orElse(stateMachineContexEntity.getMachineState()));stateMachineContexEntity.setMachineEvent(Optional.ofNullable(context.getEvent()).map(Objects::toString).orElse(stateMachineContexEntity.getMachineEvent()));stateMachineContexEntity.setUpdateDate(new Date());stateMachineContextDao.updateById(stateMachineContexEntity);return;}StateMachineContextEntity entity new StateMachineContextEntity();entity.setMachineId(machineId);entity.setMachineData(machineData);entity.setMachineType(machineType);entity.setMachineState(Optional.ofNullable(context.getState()).map(Objects::toString).orElse(null));entity.setMachineEvent(Optional.ofNullable(context.getEvent()).map(Objects::toString).orElse(null));stateMachineContextDao.save(entity);}Overridepublic StateMachineContextS, E read(PairString, String pair) throws Exception {String machineId pair.getKey();String machineType pair.getValue();StateMachineContextEntity stateMachineContexEntity stateMachineContextDao.findByMachineIdAndMachineType(machineId, machineType);if (Objects.isNull(stateMachineContexEntity)) {return null;}byte[] machineData stateMachineContexEntity.getMachineData();return deserialize(machineData);}
}四、自定义状态机即继承类 AbstractStateMachinePersister
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.statemachine.StateMachinePersist;
import org.springframework.statemachine.persist.AbstractStateMachinePersister;/*** author songjianyong*/
public class CustomStateMachinePersisterS, E extends AbstractStateMachinePersisterS, E, PairString, String {public CustomStateMachinePersister(StateMachinePersistS, E, PairString, String stateMachinePersist) {super(stateMachinePersist);}
}
五、使用自定义状态机 /*** 持久化到库中** return 数据库持久化状态机*/Bean(name customStateMachinePersister)public CustomStateMachinePersisterS, E customStateMachinePersister(StateMachineContextDao stateMachineContextDao) {CustomStateMachinePersistS, E customStateMachinePersist new CustomStateMachinePersist(stateMachineContextDao);return new CustomStateMachinePersister(customStateMachinePersist);} Resourceprivate StateMachinePersisterOrderStatus, OrderStatusChangeEvent, PairString, String pair customStateMachinePersister;