Add distibuted action
- New module spring-statemachine-cluster which is based on spring-cloud-cluster to provide leader election. - Ensemble now has a concept of a leader if implementation supports it. - New DistributedLeaderAction can use leader info to execute action only on a leader. - Tweak web sample with these new concepts. - Fixes #176
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2015 the original author or authors.
|
||||
* Copyright 2015-2016 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.
|
||||
@@ -22,10 +22,14 @@ import org.apache.commons.logging.LogFactory;
|
||||
import org.apache.curator.framework.CuratorFramework;
|
||||
import org.apache.curator.framework.CuratorFrameworkFactory;
|
||||
import org.apache.curator.retry.ExponentialBackoffRetry;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.messaging.simp.SimpMessagingTemplate;
|
||||
import org.springframework.statemachine.StateContext;
|
||||
import org.springframework.statemachine.action.Action;
|
||||
import org.springframework.statemachine.action.DistributedLeaderAction;
|
||||
import org.springframework.statemachine.cluster.LeaderZookeeperStateMachineEnsemble;
|
||||
import org.springframework.statemachine.config.EnableStateMachine;
|
||||
import org.springframework.statemachine.config.EnumStateMachineConfigurerAdapter;
|
||||
import org.springframework.statemachine.config.builders.StateMachineConfigurationConfigurer;
|
||||
@@ -33,7 +37,6 @@ import org.springframework.statemachine.config.builders.StateMachineStateConfigu
|
||||
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;
|
||||
import org.springframework.statemachine.ensemble.StateMachineEnsemble;
|
||||
import org.springframework.statemachine.guard.Guard;
|
||||
import org.springframework.statemachine.zookeeper.ZookeeperStateMachineEnsemble;
|
||||
|
||||
@Configuration
|
||||
public class StateMachineConfig {
|
||||
@@ -45,8 +48,12 @@ public class StateMachineConfig {
|
||||
static class Config
|
||||
extends EnumStateMachineConfigurerAdapter<States, Events> {
|
||||
|
||||
@Autowired
|
||||
private SimpMessagingTemplate simpMessagingTemplate;
|
||||
|
||||
@Override
|
||||
public void configure(StateMachineConfigurationConfigurer<States, Events> config) throws Exception {
|
||||
public void configure(StateMachineConfigurationConfigurer<States, Events> config)
|
||||
throws Exception {
|
||||
config
|
||||
.withDistributed()
|
||||
.ensemble(stateMachineEnsemble())
|
||||
@@ -72,7 +79,7 @@ public class StateMachineConfig {
|
||||
.parent(States.S1)
|
||||
.initial(States.S11)
|
||||
.state(States.S11)
|
||||
.state(States.S12)
|
||||
.state(States.S12, distAction(), null)
|
||||
.and()
|
||||
.withStates()
|
||||
.parent(States.S0)
|
||||
@@ -178,9 +185,14 @@ public class StateMachineConfig {
|
||||
return new SetVariableAction();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public DistributedLeaderAction<States, Events> distAction() throws Exception {
|
||||
return new DistributedLeaderAction<>(new HelloAction(simpMessagingTemplate), stateMachineEnsemble());
|
||||
}
|
||||
|
||||
@Bean
|
||||
public StateMachineEnsemble<States, Events> stateMachineEnsemble() throws Exception {
|
||||
return new ZookeeperStateMachineEnsemble<States, Events>(curatorClient(), "/foo");
|
||||
return new LeaderZookeeperStateMachineEnsemble<States, Events>(curatorClient(), "/foo");
|
||||
}
|
||||
|
||||
@Bean
|
||||
@@ -193,7 +205,6 @@ public class StateMachineConfig {
|
||||
client.start();
|
||||
return client;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public enum States {
|
||||
@@ -247,7 +258,22 @@ public class StateMachineConfig {
|
||||
context.getExtendedState().getVariables().put("testVariable", testVariable);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static class HelloAction implements Action<States, Events> {
|
||||
|
||||
SimpMessagingTemplate simpMessagingTemplate;
|
||||
|
||||
public HelloAction(SimpMessagingTemplate simpMessagingTemplate) {
|
||||
this.simpMessagingTemplate = simpMessagingTemplate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(StateContext<States, Events> context) {
|
||||
log.info("Hello");
|
||||
StateMachineMessage message = new StateMachineMessage();
|
||||
message.setMessage("Hello action");
|
||||
simpMessagingTemplate.convertAndSend("/topic/sm.message", message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2015 the original author or authors.
|
||||
* Copyright 2015-2016 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.
|
||||
@@ -34,6 +34,8 @@ 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.ensemble.EnsembleListenerAdapter;
|
||||
import org.springframework.statemachine.ensemble.StateMachineEnsemble;
|
||||
import org.springframework.statemachine.listener.StateMachineListenerAdapter;
|
||||
import org.springframework.statemachine.state.State;
|
||||
import org.springframework.statemachine.transition.Transition;
|
||||
@@ -59,6 +61,9 @@ public class StateMachineController {
|
||||
@Autowired
|
||||
private StateMachine<States, Events> stateMachine;
|
||||
|
||||
@Autowired
|
||||
private StateMachineEnsemble<States, Events> stateMachineEnsemble;
|
||||
|
||||
@PostConstruct
|
||||
public void setup() {
|
||||
|
||||
@@ -102,6 +107,28 @@ public class StateMachineController {
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
stateMachineEnsemble.addEnsembleListener(new EnsembleListenerAdapter<States, Events>() {
|
||||
|
||||
@Override
|
||||
public void ensembleLeaderGranted(StateMachine<States, Events> stateMachine) {
|
||||
StateMachineMessage message = new StateMachineMessage();
|
||||
message.setMessage("Leader granted " + stateMachine.getUuid().toString());
|
||||
simpMessagingTemplate.convertAndSend("/topic/sm.message", message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void ensembleLeaderRevoked(StateMachine<States, Events> stateMachine) {
|
||||
StateMachineMessage message = new StateMachineMessage();
|
||||
message.setMessage("Leader revoked " + stateMachine.getUuid().toString());
|
||||
simpMessagingTemplate.convertAndSend("/topic/sm.message", message);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@SubscribeMapping("/sm.uuid")
|
||||
public String retrieveUuid() {
|
||||
return stateMachine.getUuid().toString();
|
||||
}
|
||||
|
||||
@SubscribeMapping("/sm.states")
|
||||
@@ -123,12 +150,24 @@ public class StateMachineController {
|
||||
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<Events> message = MessageBuilder
|
||||
.withPayload(id)
|
||||
.setHeader("testVariable", testVariable)
|
||||
.build();
|
||||
stateMachine.sendEvent(message);
|
||||
}
|
||||
Message<Events> message = MessageBuilder
|
||||
.withPayload(id)
|
||||
.setHeader("testVariable", testVariable)
|
||||
.build();
|
||||
stateMachine.sendEvent(message);
|
||||
}
|
||||
|
||||
@RequestMapping("/join")
|
||||
@ResponseStatus(HttpStatus.OK)
|
||||
public void joinEnsemble() {
|
||||
stateMachineEnsemble.join(stateMachine);
|
||||
}
|
||||
|
||||
@RequestMapping("/leave")
|
||||
@ResponseStatus(HttpStatus.OK)
|
||||
public void leaveEnsemble() {
|
||||
stateMachineEnsemble.leave(stateMachine);
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/states", method = RequestMethod.GET, produces="application/json")
|
||||
@ResponseBody
|
||||
|
||||
@@ -1,44 +1,44 @@
|
||||
+----------------------------------------------------------------------------------------------+
|
||||
| S0 |
|
||||
+----------------------------------------------------------------------------------------------+
|
||||
| entry/ |
|
||||
| exit/ |
|
||||
| H/[foo.equals(0)]; |
|
||||
| |
|
||||
| +-------------------------+ +--------------------------------------------+ |
|
||||
| *-->| S1 | | S2 | |
|
||||
| +-------------------------+ +--------------------------------------------+ |
|
||||
| | entry/ | C | entry/ | |
|
||||
| D | exit/ |----->| exit/ | |
|
||||
|<-----------| H/ | | H/[foo.equals(1)]; | |
|
||||
| | | | | |
|
||||
| | +---------------+ | K | +------------------------------+ | |
|
||||
| | *-->| S11 | |<-----| *-->| S21 | | |
|
||||
| | +---------------+ | | +------------------------------+ | |
|
||||
| | | entry/ | | F | | entry/ | | |
|
||||
| | | exit/ |<---------| | exit/ | | |
|
||||
| | | | | | | +--------------+ | | |
|
||||
| | B | | | | | *-->| s211 | | | |
|
||||
| |---->| J | | | F | +--------------+ G | | |
|
||||
| | | +-------+ | |-------------------->| entry/ |----------------->|
|
||||
| | +--| | | | | | G | | exit/ | | | |
|
||||
| | | | | v |------------------------>| | | E | |
|
||||
| | | +---------------+ | | | B | |<-----------------|
|
||||
| | | | | |------>| | | | |
|
||||
| | | +---------------+ | | | | | D | | |
|
||||
| | I| | S12 | | | | +--| |------>| | |
|
||||
| | | +---------------+ | | | | +--------------+ | | |
|
||||
| | | | entry/ | | | | | | | |
|
||||
| | | | exit/ | | | | I| +--------------+ | | |
|
||||
| | | | | | | | | | s212 | | | |
|
||||
| | +->| | | | | | +--------------+ | | |
|
||||
| +--| | | | | | +->| entry/ | | | |
|
||||
| | | | | | I | | | exit/ | | | |
|
||||
| | | | |------------------------>| | | | |
|
||||
| A| | | | | | | +--------------+ | | |
|
||||
| | | | | | | | | | |
|
||||
| | | +---------------+ | | +------------------------------+ | |
|
||||
| +->| | | | |
|
||||
| +-------------------------+ +--------------------------------------------+ |
|
||||
| A[foo.equals(1)]; |
|
||||
+----------------------------------------------------------------------------------------------+
|
||||
+-----------------------------------------------------------------------------------------------+
|
||||
| S0 |
|
||||
+-----------------------------------------------------------------------------------------------+
|
||||
| entry/ |
|
||||
| exit/ |
|
||||
| H/[foo.equals(0)]; |
|
||||
| |
|
||||
| +--------------------------+ +--------------------------------------------+ |
|
||||
| *-->| S1 | | S2 | |
|
||||
| +--------------------------+ +--------------------------------------------+ |
|
||||
| | entry/ | C | entry/ | |
|
||||
| D | exit/ |----->| exit/ | |
|
||||
|<-----------| H/ | | H/[foo.equals(1)]; | |
|
||||
| | | | | |
|
||||
| | +----------------+ | K | +------------------------------+ | |
|
||||
| | *-->| S11 | |<-----| *-->| S21 | | |
|
||||
| | +----------------+ | | +------------------------------+ | |
|
||||
| | | entry/ | | F | | entry/ | | |
|
||||
| | | exit/ |<---------| | exit/ | | |
|
||||
| | | | | | | +--------------+ | | |
|
||||
| | B | | | | | *-->| s211 | | | |
|
||||
| |---->| J | | | F | +--------------+ G | | |
|
||||
| | | +-------+ | |-------------------->| entry/ |----------------->|
|
||||
| | +--| | | | | | G | | exit/ | | | |
|
||||
| | | | | v |------------------------>| | | E | |
|
||||
| | | +----------------+ | | | B | |<-----------------|
|
||||
| | | | | |------>| | | | |
|
||||
| | | +----------------+ | | | | | D | | |
|
||||
| | I| | S12 | | | | +--| |------>| | |
|
||||
| | | +----------------+ | | | | +--------------+ | | |
|
||||
| | | | entry/ | | | | | | | |
|
||||
| | | | distAction() | | | | I| +--------------+ | | |
|
||||
| | | | exit/ | | | | | | s212 | | | |
|
||||
| | +->| | | | | | +--------------+ | | |
|
||||
| +--| | | | | | +->| entry/ | | | |
|
||||
| | | | | | I | | | exit/ | | | |
|
||||
| | | | |------------------------>| | | | |
|
||||
| A| | | | | | | +--------------+ | | |
|
||||
| | | | | | | | | | |
|
||||
| | | +----------------+ | | +------------------------------+ | |
|
||||
| +->| | | | |
|
||||
| +--------------------------+ +--------------------------------------------+ |
|
||||
| A[foo.equals(1)]; |
|
||||
+-----------------------------------------------------------------------------------------------+
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en" ng-app="springChat">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>Spring Statemachine Zookeeper Demo</title>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<meta charset="utf-8" />
|
||||
<title>Spring Statemachine Zookeeper Demo</title>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
|
||||
<link href="lib/flat-ui/dist/css/vendor/bootstrap.min.css" rel="stylesheet">
|
||||
<link href="lib/flat-ui/dist/css/flat-ui.css" rel="stylesheet">
|
||||
<link href="lib/angularjs-toaster/toaster.css" rel="stylesheet">
|
||||
<link href="lib/flat-ui/dist/css/vendor/bootstrap.min.css" rel="stylesheet">
|
||||
<link href="lib/flat-ui/dist/css/flat-ui.css" rel="stylesheet">
|
||||
<link href="lib/angularjs-toaster/toaster.css" rel="stylesheet">
|
||||
|
||||
<link href="css/chat.css" rel="stylesheet">
|
||||
<link href="css/chat.css" rel="stylesheet">
|
||||
</head>
|
||||
<body>
|
||||
|
||||
@@ -23,18 +23,18 @@
|
||||
</nav>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div>
|
||||
<button ng-click="sendEvent('A')">event A</button>
|
||||
<button ng-click="sendEvent('B')">event B</button>
|
||||
<button ng-click="sendEvent('C')">event C</button>
|
||||
<button ng-click="sendEvent('D')">event D</button>
|
||||
<button ng-click="sendEvent('E')">event E</button>
|
||||
<button ng-click="sendEvent('F')">event F</button>
|
||||
<button ng-click="sendEvent('G')">event G</button>
|
||||
<button ng-click="sendEvent('H')">event H</button>
|
||||
<button ng-click="sendEvent('I')">event I</button>
|
||||
<button ng-click="sendEvent('K')">event K</button>
|
||||
</div>
|
||||
<div>
|
||||
<button ng-click="sendEvent('A')">event A</button>
|
||||
<button ng-click="sendEvent('B')">event B</button>
|
||||
<button ng-click="sendEvent('C')">event C</button>
|
||||
<button ng-click="sendEvent('D')">event D</button>
|
||||
<button ng-click="sendEvent('E')">event E</button>
|
||||
<button ng-click="sendEvent('F')">event F</button>
|
||||
<button ng-click="sendEvent('G')">event G</button>
|
||||
<button ng-click="sendEvent('H')">event H</button>
|
||||
<button ng-click="sendEvent('I')">event I</button>
|
||||
<button ng-click="sendEvent('K')">event K</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div>
|
||||
@@ -42,44 +42,53 @@
|
||||
<input id="newTestVariable" type="text" ng-model="testVariable" placeholder="New testVariable value..." />
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col-xs-3">
|
||||
<h4>States</h4>
|
||||
<div class="share">
|
||||
<ul ng-repeat="participant in participants">
|
||||
<li>
|
||||
<div>{{participant.message}}</div>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-xs-3">
|
||||
<h4>Variables</h4>
|
||||
<div class="share">
|
||||
<ul ng-repeat="variable in variables">
|
||||
<li>
|
||||
<div>{{variable}}</div>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-xs-8 chat-box">
|
||||
<h4>Messages</h4>
|
||||
<div ng-repeat="message in messages">
|
||||
<small print-message></small>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<!-- /.container -->
|
||||
<div class="row">
|
||||
<div>
|
||||
<button ng-click="joinEnsemble()">Join</button>
|
||||
<button ng-click="leaveEnsemble()">Leave</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div ng-if="uuid">UUID: {{uuid}}</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col-xs-3">
|
||||
<h4>States</h4>
|
||||
<div class="share">
|
||||
<ul ng-repeat="participant in participants">
|
||||
<li>
|
||||
<div>{{participant.message}}</div>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-xs-3">
|
||||
<h4>Variables</h4>
|
||||
<div class="share">
|
||||
<ul ng-repeat="variable in variables">
|
||||
<li>
|
||||
<div>{{variable}}</div>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-xs-8 chat-box">
|
||||
<h4>Messages</h4>
|
||||
<div ng-repeat="message in messages">
|
||||
<small print-message></small>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<!-- /.container -->
|
||||
|
||||
<!-- 3rd party -->
|
||||
<script src="lib/angular/angular.min.js"></script>
|
||||
<script src="lib/angular-animate/angular-animate.min.js"></script>
|
||||
<script src="lib/angularjs-toaster/toaster.js"></script>
|
||||
<script src="lib/angularjs-scroll-glue/src/scrollglue.js"></script>
|
||||
<script src="lib/sockjs/sockjs.min.js"></script>
|
||||
<script src="lib/stomp/lib/stomp.min.js"></script>
|
||||
<script src="lib/angular-animate/angular-animate.min.js"></script>
|
||||
<script src="lib/angularjs-toaster/toaster.js"></script>
|
||||
<script src="lib/angularjs-scroll-glue/src/scrollglue.js"></script>
|
||||
<script src="lib/sockjs/sockjs.min.js"></script>
|
||||
<script src="lib/stomp/lib/stomp.min.js"></script>
|
||||
|
||||
<!-- App -->
|
||||
<script src="js/app.js"></script>
|
||||
|
||||
@@ -7,10 +7,11 @@ angular.module('springChat.controllers', ['toaster'])
|
||||
|
||||
var typing = undefined;
|
||||
|
||||
$scope.uuid = '';
|
||||
$scope.participants = [];
|
||||
$scope.variables = [];
|
||||
$scope.messages = [];
|
||||
$scope.newMessage = '';
|
||||
$scope.messages = [];
|
||||
$scope.newMessage = '';
|
||||
|
||||
$scope.sendEvent = function(event) {
|
||||
$http.post('/event', null, {params:{"id": event}}).
|
||||
@@ -24,11 +25,27 @@ angular.module('springChat.controllers', ['toaster'])
|
||||
});
|
||||
};
|
||||
|
||||
$scope.joinEnsemble = function(event) {
|
||||
$http.post('/join', null, {}).
|
||||
success(function(data) {
|
||||
});
|
||||
};
|
||||
|
||||
$scope.leaveEnsemble = function(event) {
|
||||
$http.post('/leave', null, {}).
|
||||
success(function(data) {
|
||||
});
|
||||
};
|
||||
|
||||
var initStompClient = function() {
|
||||
chatSocket.init('/ws');
|
||||
|
||||
chatSocket.connect(function(frame) {
|
||||
|
||||
chatSocket.subscribe("/app/sm.uuid", function(message) {
|
||||
$scope.uuid = message.body;
|
||||
});
|
||||
|
||||
chatSocket.subscribe("/app/sm.states", function(message) {
|
||||
$scope.participants = JSON.parse(message.body);
|
||||
});
|
||||
@@ -55,16 +72,16 @@ angular.module('springChat.controllers', ['toaster'])
|
||||
|
||||
chatSocket.subscribe("/topic/sm.message", function(message) {
|
||||
$scope.messages.unshift(JSON.parse(message.body));
|
||||
});
|
||||
});
|
||||
|
||||
chatSocket.subscribe("/user/queue/errors", function(message) {
|
||||
toaster.pop('error', "Error", message.body);
|
||||
});
|
||||
});
|
||||
|
||||
}, function(error) {
|
||||
toaster.pop('error', 'Error', 'Connection error ' + error);
|
||||
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
initStompClient();
|
||||
|
||||
Reference in New Issue
Block a user