From 282d395ba0c1f6ceef698fc8684832602a18f24c Mon Sep 17 00:00:00 2001 From: james Date: Wed, 7 Feb 2018 16:37:29 +0100 Subject: [PATCH] Use the contextObj as is when saving the StateMachineContext - Polish and fix some merge issues - Backport #494 --- .../AbstractPersistingStateMachineInterceptor.java | 8 ++++++-- .../data/jpa/JpaRepositoryStateMachinePersist.java | 2 +- .../data/mongodb/MongoDbRepositoryStateMachine.java | 10 +++++++++- .../mongodb/MongoDbRepositoryStateMachinePersist.java | 5 +++-- .../data/redis/RedisRepositoryStateMachine.java | 10 +++++++++- .../data/redis/RedisRepositoryStateMachinePersist.java | 5 +++-- .../data/RepositoryStateMachinePersist.java | 5 +++-- 7 files changed, 34 insertions(+), 11 deletions(-) diff --git a/spring-statemachine-core/src/main/java/org/springframework/statemachine/persist/AbstractPersistingStateMachineInterceptor.java b/spring-statemachine-core/src/main/java/org/springframework/statemachine/persist/AbstractPersistingStateMachineInterceptor.java index e52a2e32..cd4d89de 100644 --- a/spring-statemachine-core/src/main/java/org/springframework/statemachine/persist/AbstractPersistingStateMachineInterceptor.java +++ b/spring-statemachine-core/src/main/java/org/springframework/statemachine/persist/AbstractPersistingStateMachineInterceptor.java @@ -58,18 +58,20 @@ public abstract class AbstractPersistingStateMachineInterceptor extends private Function, Map> extendedStateVariablesFunction = new AllVariablesFunction<>(); + @SuppressWarnings("unchecked") @Override public void preStateChange(State state, Message message, Transition transition, StateMachine stateMachine) { // try to persist context and in case of failure, interceptor // call chain aborts transition // TODO: should probably come up with a policy vs. not force feeding this functionality try { - write(buildStateMachineContext(stateMachine, state), null); + write(buildStateMachineContext(stateMachine, state), (T)stateMachine.getId()); } catch (Exception e) { throw new StateMachineException("Unable to persist stateMachineContext", e); } } + @SuppressWarnings("unchecked") @Override public void postStateChange(State state, Message message, Transition transition, StateMachine stateMachine) { // initial transitions are never intercepted as those cannot fail or get aborted. @@ -77,7 +79,7 @@ public abstract class AbstractPersistingStateMachineInterceptor extends // TODO: consider intercept initial transition, but not aborting if error is thrown? if (state != null && transition != null && transition.getKind() == TransitionKind.INITIAL) { try { - write(buildStateMachineContext(stateMachine, state), null); + write(buildStateMachineContext(stateMachine, state), (T)stateMachine.getId()); } catch (Exception e) { throw new StateMachineException("Unable to persist stateMachineContext", e); } @@ -90,6 +92,7 @@ public abstract class AbstractPersistingStateMachineInterceptor extends * @param context the state machine context * @param contextObj the context object */ + @Override public abstract void write(StateMachineContext context, T contextObj) throws Exception; /** @@ -98,6 +101,7 @@ public abstract class AbstractPersistingStateMachineInterceptor extends * @param contextObj the context object * @return the state machine context */ + @Override public abstract StateMachineContext read(T contextObj) throws Exception; /** diff --git a/spring-statemachine-data/jpa/src/main/java/org/springframework/statemachine/data/jpa/JpaRepositoryStateMachinePersist.java b/spring-statemachine-data/jpa/src/main/java/org/springframework/statemachine/data/jpa/JpaRepositoryStateMachinePersist.java index 30cb6cc5..6baa903f 100644 --- a/spring-statemachine-data/jpa/src/main/java/org/springframework/statemachine/data/jpa/JpaRepositoryStateMachinePersist.java +++ b/spring-statemachine-data/jpa/src/main/java/org/springframework/statemachine/data/jpa/JpaRepositoryStateMachinePersist.java @@ -60,7 +60,7 @@ public class JpaRepositoryStateMachinePersist extends RepositoryStateMachi } @Override - protected JpaRepositoryStateMachine build(StateMachineContext context, byte[] serialisedContext) { + protected JpaRepositoryStateMachine build(StateMachineContext context, Object contextObj, byte[] serialisedContext) { JpaRepositoryStateMachine jpaRepositoryStateMachine = new JpaRepositoryStateMachine(); jpaRepositoryStateMachine.setMachineId(context.getId()); jpaRepositoryStateMachine.setState(context.getState().toString()); diff --git a/spring-statemachine-data/mongodb/src/main/java/org/springframework/statemachine/data/mongodb/MongoDbRepositoryStateMachine.java b/spring-statemachine-data/mongodb/src/main/java/org/springframework/statemachine/data/mongodb/MongoDbRepositoryStateMachine.java index f90d479b..6c221abe 100644 --- a/spring-statemachine-data/mongodb/src/main/java/org/springframework/statemachine/data/mongodb/MongoDbRepositoryStateMachine.java +++ b/spring-statemachine-data/mongodb/src/main/java/org/springframework/statemachine/data/mongodb/MongoDbRepositoryStateMachine.java @@ -1,5 +1,5 @@ /* - * Copyright 2017 the original author or authors. + * Copyright 2017-2018 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. @@ -35,6 +35,14 @@ public class MongoDbRepositoryStateMachine extends RepositoryStateMachine { private String state; private byte[] stateMachineContext; + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + @Override public String getMachineId() { return machineId; diff --git a/spring-statemachine-data/mongodb/src/main/java/org/springframework/statemachine/data/mongodb/MongoDbRepositoryStateMachinePersist.java b/spring-statemachine-data/mongodb/src/main/java/org/springframework/statemachine/data/mongodb/MongoDbRepositoryStateMachinePersist.java index f0c92f31..2106ffff 100644 --- a/spring-statemachine-data/mongodb/src/main/java/org/springframework/statemachine/data/mongodb/MongoDbRepositoryStateMachinePersist.java +++ b/spring-statemachine-data/mongodb/src/main/java/org/springframework/statemachine/data/mongodb/MongoDbRepositoryStateMachinePersist.java @@ -1,5 +1,5 @@ /* - * Copyright 2017 the original author or authors. + * Copyright 2017-2018 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. @@ -60,8 +60,9 @@ public class MongoDbRepositoryStateMachinePersist extends RepositoryStateM } @Override - protected MongoDbRepositoryStateMachine build(StateMachineContext context, byte[] serialisedContext) { + protected MongoDbRepositoryStateMachine build(StateMachineContext context, Object contextObj, byte[] serialisedContext) { MongoDbRepositoryStateMachine mongodbRepositoryStateMachine = new MongoDbRepositoryStateMachine(); + mongodbRepositoryStateMachine.setId(contextObj.toString()); mongodbRepositoryStateMachine.setMachineId(context.getId()); mongodbRepositoryStateMachine.setState(context.getState().toString()); mongodbRepositoryStateMachine.setStateMachineContext(serialisedContext); diff --git a/spring-statemachine-data/redis/src/main/java/org/springframework/statemachine/data/redis/RedisRepositoryStateMachine.java b/spring-statemachine-data/redis/src/main/java/org/springframework/statemachine/data/redis/RedisRepositoryStateMachine.java index f0b33b30..31f9174e 100644 --- a/spring-statemachine-data/redis/src/main/java/org/springframework/statemachine/data/redis/RedisRepositoryStateMachine.java +++ b/spring-statemachine-data/redis/src/main/java/org/springframework/statemachine/data/redis/RedisRepositoryStateMachine.java @@ -1,5 +1,5 @@ /* - * Copyright 2017 the original author or authors. + * Copyright 2017-2018 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. @@ -35,6 +35,14 @@ public class RedisRepositoryStateMachine extends RepositoryStateMachine { private String state; private byte[] stateMachineContext; + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + @Override public String getMachineId() { return machineId; diff --git a/spring-statemachine-data/redis/src/main/java/org/springframework/statemachine/data/redis/RedisRepositoryStateMachinePersist.java b/spring-statemachine-data/redis/src/main/java/org/springframework/statemachine/data/redis/RedisRepositoryStateMachinePersist.java index 9e5336c1..424a796d 100644 --- a/spring-statemachine-data/redis/src/main/java/org/springframework/statemachine/data/redis/RedisRepositoryStateMachinePersist.java +++ b/spring-statemachine-data/redis/src/main/java/org/springframework/statemachine/data/redis/RedisRepositoryStateMachinePersist.java @@ -1,5 +1,5 @@ /* - * Copyright 2017 the original author or authors. + * Copyright 2017-2018 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. @@ -60,8 +60,9 @@ public class RedisRepositoryStateMachinePersist extends RepositoryStateMac } @Override - protected RedisRepositoryStateMachine build(StateMachineContext context, byte[] serialisedContext) { + protected RedisRepositoryStateMachine build(StateMachineContext context, Object contextObj, byte[] serialisedContext) { RedisRepositoryStateMachine redisRepositoryStateMachine = new RedisRepositoryStateMachine(); + redisRepositoryStateMachine.setId(contextObj.toString()); redisRepositoryStateMachine.setMachineId(context.getId()); redisRepositoryStateMachine.setState(context.getState().toString()); redisRepositoryStateMachine.setStateMachineContext(serialisedContext); diff --git a/spring-statemachine-data/src/main/java/org/springframework/statemachine/data/RepositoryStateMachinePersist.java b/spring-statemachine-data/src/main/java/org/springframework/statemachine/data/RepositoryStateMachinePersist.java index 34102ee9..10e0b2eb 100644 --- a/spring-statemachine-data/src/main/java/org/springframework/statemachine/data/RepositoryStateMachinePersist.java +++ b/spring-statemachine-data/src/main/java/org/springframework/statemachine/data/RepositoryStateMachinePersist.java @@ -59,7 +59,7 @@ public abstract class RepositoryStateMachinePersist context, byte[] serialisedContext); + protected abstract M build(StateMachineContext context, Object contextObj, byte[] serialisedContext); }