spring-state-machine持久化

简介: spring-state-machine持久化

文档如下:

https://docs.spring.io/spring-statemachine/docs/current/reference/#statemachine-examples-datapersist

代码示例如下:

https://github.com/spring-projects/spring-statemachine/tree/main/spring-statemachine-samples/persist

状态机持久化主要解决服务端重启后会话丢失问题

/*
 * Copyright 2015-2020 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package demo.persist;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.statemachine.TestUtils.doStartAndAssert;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.statemachine.StateMachine;
import org.springframework.statemachine.listener.StateMachineListenerAdapter;
import org.springframework.statemachine.state.State;
import org.springframework.statemachine.transition.Transition;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.annotation.DirtiesContext.ClassMode;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import demo.CommonConfiguration;

@ExtendWith(SpringExtension.class)
@DirtiesContext(classMode=ClassMode.AFTER_EACH_TEST_METHOD)
@SpringBootTest(classes = { CommonConfiguration.class, Application.class, StateMachineCommands.class })
public class PersistTests {

  @Autowired
  private StateMachineCommands commands;

  @Autowired
  private StateMachine<String, String> machine;

  @Autowired
  private Persist persist;

  @Test
  public void testNotStarted() throws Exception {
    assertThat(commands.state()).isEqualTo("No state");
  }

  @Test
  public void testInitialState() throws Exception {
    TestListener listener = new TestListener();
    machine.addStateListener(listener);
    doStartAndAssert(machine);
    assertThat(listener.stateChangedLatch.await(3, TimeUnit.SECONDS)).isTrue();
    assertThat(listener.stateEnteredLatch.await(3, TimeUnit.SECONDS)).isTrue();
    assertThat(machine.getState().getIds()).containsExactly("PLACED");
    assertThat(listener.statesEntered).hasSize(1);
    assertThat(listener.statesEntered.get(0).getId()).isEqualTo("PLACED");
    assertThat(listener.statesExited).isEmpty();
  }

  @Test
  public void testInitialDbList() {
    // dataOrder [id=1, state=PLACED]Order [id=2, state=PROCESSING]Order [id=3, state=SENT]Order [id=4, state=DELIVERED]
    assertThat(persist.listDbEntries()).contains("PLACED");
  }

  @Test
  public void testUpdate1() {
    persist.change(1, "PROCESS");
    assertThat(persist.listDbEntries()).contains("id=1, state=PROCESSING");
  }

  @Test
  public void testUpdate2() {
    persist.change(2, "SEND");
    assertThat(persist.listDbEntries()).contains("id=2, state=SENT");
  }

  private static class TestListener extends StateMachineListenerAdapter<String, String> {

    volatile CountDownLatch stateChangedLatch = new CountDownLatch(1);
    volatile CountDownLatch stateEnteredLatch = new CountDownLatch(1);
    volatile CountDownLatch stateExitedLatch = new CountDownLatch(0);
    volatile CountDownLatch transitionLatch = new CountDownLatch(0);
    volatile List<Transition<String, String>> transitions = new ArrayList<Transition<String, String>>();
    List<State<String, String>> statesEntered = new ArrayList<State<String, String>>();
    List<State<String, String>> statesExited = new ArrayList<State<String, String>>();

    @Override
    public void stateChanged(State<String, String> from, State<String, String> to) {
      stateChangedLatch.countDown();
    }

    @Override
    public void stateEntered(State<String, String> state) {
      statesEntered.add(state);
      stateEnteredLatch.countDown();
    }

    @Override
    public void stateExited(State<String, String> state) {
      statesExited.add(state);
      stateExitedLatch.countDown();
    }

    @Override
    public void transition(Transition<String, String> transition) {
      transitions.add(transition);
      transitionLatch.countDown();
    }

  }

}
相关文章
|
7月前
spring-state-machine持久化restore踩坑
spring-state-machine持久化restore踩坑
128 0
|
7月前
|
NoSQL Redis
spring-state-machine使用redis持久化
spring-state-machine使用redis持久化
199 0
|
负载均衡 Java 关系型数据库
|
XML 存储 JSON
Spring Cloud Alibaba Sentinel 整合 nacos 进行规则持久化
Spring Cloud Alibaba Sentinel 整合 nacos 进行规则持久化
2746 0
Spring Cloud Alibaba Sentinel 整合 nacos 进行规则持久化
|
存储 NoSQL Java
|
SQL 存储 druid
Spring Boot数据持久化之JdbcTemplate
Spring Boot数据持久化之JdbcTemplate
|
SQL Java 数据库连接
Java Web Spring核心之AOP的解析及实战(AOP的实现、切入点、Aspect Spring的持久化 Hibernate)
Java Web Spring核心之AOP的解析及实战(AOP的实现、切入点、Aspect Spring的持久化 Hibernate)
206 0
Java Web Spring核心之AOP的解析及实战(AOP的实现、切入点、Aspect Spring的持久化 Hibernate)
|
存储 算法 关系型数据库
手把手教你Spring Cloud Alibaba系列教程:Nacos数据持久化
手把手教你Spring Cloud Alibaba系列教程:Nacos数据持久化
194 0
|
存储 SQL 编解码
Spring Security系列教程16--基于持久化令牌方案实现自动登录
前言 在上一章节中,一一哥 带各位基于散列加密方案实现了自动登录,并且给各位介绍了散列加密算法,其实还有另一种自动登录的实现方案,也就是基于持久化令牌方案来进行实现。接下来请跟 一一哥 学习这种方案该怎么实现吧。 一. 持久化令牌方案简介 1. 持久化令牌方案 有的小伙伴会问,既然我们要基于持久化令牌来实现自动登录,那啥是持久化令牌啊?所以 一一哥 先给大家做个概念解释。 所谓的持久化令牌的实现方案,其实在交互上与我们前面章节讲的散列加密方案一致,只不过是在用户勾选Remember-me之后,将生成的token令牌发送到用户浏览器,并在用户下次访问系统时读取该令牌进行认证。不同的是,持久化令
534 0
|
SQL 安全 Java
肝!Spring JDBC持久化层框架“全家桶”教程!
肝!Spring JDBC持久化层框架“全家桶”教程!
220 0
肝!Spring JDBC持久化层框架“全家桶”教程!