Use the contextObj as is when saving the StateMachineContext

- Polish and fix some merge issues
- Backport #494
This commit is contained in:
james
2018-02-07 16:37:29 +01:00
committed by jvalkeal
parent bbc4e336e5
commit 282d395ba0
7 changed files with 34 additions and 11 deletions

View File

@@ -58,18 +58,20 @@ public abstract class AbstractPersistingStateMachineInterceptor<S, E, T> extends
private Function<StateMachine<S, E>, Map<Object, Object>> extendedStateVariablesFunction = new AllVariablesFunction<>();
@SuppressWarnings("unchecked")
@Override
public void preStateChange(State<S, E> state, Message<E> message, Transition<S, E> transition, StateMachine<S, E> 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<S, E> state, Message<E> message, Transition<S, E> transition, StateMachine<S, E> stateMachine) {
// initial transitions are never intercepted as those cannot fail or get aborted.
@@ -77,7 +79,7 @@ public abstract class AbstractPersistingStateMachineInterceptor<S, E, T> 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<S, E, T> extends
* @param context the state machine context
* @param contextObj the context object
*/
@Override
public abstract void write(StateMachineContext<S, E> context, T contextObj) throws Exception;
/**
@@ -98,6 +101,7 @@ public abstract class AbstractPersistingStateMachineInterceptor<S, E, T> extends
* @param contextObj the context object
* @return the state machine context
*/
@Override
public abstract StateMachineContext<S, E> read(T contextObj) throws Exception;
/**

View File

@@ -60,7 +60,7 @@ public class JpaRepositoryStateMachinePersist<S, E> extends RepositoryStateMachi
}
@Override
protected JpaRepositoryStateMachine build(StateMachineContext<S, E> context, byte[] serialisedContext) {
protected JpaRepositoryStateMachine build(StateMachineContext<S, E> context, Object contextObj, byte[] serialisedContext) {
JpaRepositoryStateMachine jpaRepositoryStateMachine = new JpaRepositoryStateMachine();
jpaRepositoryStateMachine.setMachineId(context.getId());
jpaRepositoryStateMachine.setState(context.getState().toString());

View File

@@ -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;

View File

@@ -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<S, E> extends RepositoryStateM
}
@Override
protected MongoDbRepositoryStateMachine build(StateMachineContext<S, E> context, byte[] serialisedContext) {
protected MongoDbRepositoryStateMachine build(StateMachineContext<S, E> 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);

View File

@@ -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;

View File

@@ -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<S, E> extends RepositoryStateMac
}
@Override
protected RedisRepositoryStateMachine build(StateMachineContext<S, E> context, byte[] serialisedContext) {
protected RedisRepositoryStateMachine build(StateMachineContext<S, E> 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);

View File

@@ -59,7 +59,7 @@ public abstract class RepositoryStateMachinePersist<M extends RepositoryStateMac
if (log.isDebugEnabled()) {
log.debug("Persisting context " + context + " using contextObj " + contextObj);
}
M build = build(context, serialisationService.serialiseStateMachineContext(context));
M build = build(context, contextObj, serialisationService.serialiseStateMachineContext(context));
getRepository().save(build);
}
@@ -83,8 +83,9 @@ public abstract class RepositoryStateMachinePersist<M extends RepositoryStateMac
* Builds the generic {@link RepositoryStateMachine} entity.
*
* @param context the context
* @param contextObj the context obj
* @param serialisedContext the serialised context
* @return the repository state machine entity
*/
protected abstract M build(StateMachineContext<S, E> context, byte[] serialisedContext);
protected abstract M build(StateMachineContext<S, E> context, Object contextObj, byte[] serialisedContext);
}