From 672a41ecf13b95653d3ab053583401b004d0e0db Mon Sep 17 00:00:00 2001 From: Janne Valkealahti Date: Fri, 7 Aug 2015 17:22:28 +0100 Subject: [PATCH] Add internal transition to web sample - This is purely for jepsen tests, though also handy for UI for showing that variable can be changed. Essentially we want to have a way to set extended state variable via internal transition which takes an value from event headers and set that to extended state. This is then supposed to be used from jepsen to test concurrenty issues around extended state variables. --- .../java/demo/web/StateMachineConfig.java | 28 +++++++++++++++++-- .../java/demo/web/StateMachineController.java | 13 +++++++-- .../web/src/main/resources/static/index.html | 6 ++++ .../main/resources/static/js/controllers.js | 6 ++++ 4 files changed, 47 insertions(+), 6 deletions(-) diff --git a/spring-statemachine-samples/web/src/main/java/demo/web/StateMachineConfig.java b/spring-statemachine-samples/web/src/main/java/demo/web/StateMachineConfig.java index f8165467..2beac869 100644 --- a/spring-statemachine-samples/web/src/main/java/demo/web/StateMachineConfig.java +++ b/spring-statemachine-samples/web/src/main/java/demo/web/StateMachineConfig.java @@ -151,8 +151,11 @@ public class StateMachineConfig { .source(States.S211).target(States.S212).event(Events.I) .and() .withExternal() - .source(States.S12).target(States.S212).event(Events.I); - + .source(States.S12).target(States.S212).event(Events.I) + .and() + .withInternal() + .source(States.S11).event(Events.J) + .action(setVariableAction()); } @Bean @@ -170,6 +173,11 @@ public class StateMachineConfig { return new FooAction(); } + @Bean + public SetVariableAction setVariableAction() { + return new SetVariableAction(); + } + @Bean public StateMachineEnsemble stateMachineEnsemble() throws Exception { return new ZookeeperStateMachineEnsemble(curatorClient(), "/foo"); @@ -193,7 +201,7 @@ public class StateMachineConfig { } public static enum Events { - A, B, C, D, E, F, G, H, I + A, B, C, D, E, F, G, H, I, J } private static class FooAction implements Action { @@ -230,4 +238,18 @@ public class StateMachineConfig { } } + private static class SetVariableAction implements Action { + + @Override + public void execute(StateContext context) { + System.out.println("XXXXXXXXXXX execute"); + String testVariable = context.getMessageHeaders().get("testVariable", String.class); + System.out.println("XXXXXXXXXXX execute testVariable=" + testVariable); + if (testVariable != null) { + context.getExtendedState().getVariables().put("testVariable", testVariable); + } + } + + } + } diff --git a/spring-statemachine-samples/web/src/main/java/demo/web/StateMachineController.java b/spring-statemachine-samples/web/src/main/java/demo/web/StateMachineController.java index db5de175..fa34c512 100644 --- a/spring-statemachine-samples/web/src/main/java/demo/web/StateMachineController.java +++ b/spring-statemachine-samples/web/src/main/java/demo/web/StateMachineController.java @@ -26,10 +26,12 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; +import org.springframework.messaging.Message; import org.springframework.messaging.handler.annotation.MessageExceptionHandler; import org.springframework.messaging.simp.SimpMessagingTemplate; import org.springframework.messaging.simp.annotation.SendToUser; import org.springframework.messaging.simp.annotation.SubscribeMapping; +import org.springframework.messaging.support.MessageBuilder; import org.springframework.statemachine.StateMachine; import org.springframework.statemachine.StateMachineException; import org.springframework.statemachine.listener.StateMachineListenerAdapter; @@ -118,9 +120,14 @@ public class StateMachineController { @RequestMapping("/event") @ResponseStatus(HttpStatus.OK) - public void sendEvent(@RequestParam(value="id") Events id) { - log.info("Got request to send event " + id); - stateMachine.sendEvent(id); + public void sendEvent(@RequestParam(value = "id") Events id, + @RequestParam(value = "testVariable", required = false) String testVariable) { + log.info("Got request to send event " + id + " testVariable " + testVariable); + Message message = MessageBuilder + .withPayload(id) + .setHeader("testVariable", testVariable) + .build(); + stateMachine.sendEvent(message); } @RequestMapping(value = "/states", method = RequestMethod.GET, produces="application/json") diff --git a/spring-statemachine-samples/web/src/main/resources/static/index.html b/spring-statemachine-samples/web/src/main/resources/static/index.html index ec208214..25806caf 100755 --- a/spring-statemachine-samples/web/src/main/resources/static/index.html +++ b/spring-statemachine-samples/web/src/main/resources/static/index.html @@ -35,6 +35,12 @@ +
+
+ + +
+

States

diff --git a/spring-statemachine-samples/web/src/main/resources/static/js/controllers.js b/spring-statemachine-samples/web/src/main/resources/static/js/controllers.js index 9b0c047f..c6e3d650 100644 --- a/spring-statemachine-samples/web/src/main/resources/static/js/controllers.js +++ b/spring-statemachine-samples/web/src/main/resources/static/js/controllers.js @@ -18,6 +18,12 @@ angular.module('springChat.controllers', ['toaster']) }); }; + $scope.sendEventAndVariable = function(event) { + $http.post('/event', null, {params:{"id": event, "testVariable": $scope.testVariable}}). + success(function(data) { + }); + }; + var initStompClient = function() { chatSocket.init('/ws');