44. Web

Web is a distributed state machine example using a zookeeper to handle distributed state. This example is meant to be run on a multiple browser sessions against a multiple different hosts.

This sample is using a modified state machine structure from a Chapter 38, Showcase to work with a distributed state machine. The state machine logic is shown above:

statechart11
[Note]Note

Due to nature of this sample an instance of a Zookeeper is expected to be available from a localhost for every individual sample instance.

Let’s go through a simple example where three different sample instances are started. If you are running different instances on a same host you need to distinguish used port by adding --server.port=<myport> to the command. Otherwise default port for each host will be 8080.

In this sample run we have three hosts, n1, n2 and n3 which all have a local zookeeper instance running and a state machine sample running on a port 8080.

@n1:~# java -jar spring-statemachine-samples-web-2.0.2.RELEASE.jar
@n2:~# java -jar spring-statemachine-samples-web-2.0.2.RELEASE.jar
@n3:~# java -jar spring-statemachine-samples-web-2.0.2.RELEASE.jar

When all instances are running you should see all showing similar information via a browser where states are S0, S1 and S11, and extended state variable foo=0. Main state is S11.

sm dist n1 1

When you press button Event C in any of a browser window, distributed state is changed to S211 which is the target state denoted by transition associated with an event C.

sm dist n2 2

Then let’s press button Event H and what is supposed to happen is that internal transition is executed on all state machines changing extended state variable foo from value 0 to 1. This change is first done on a state machine receiving the event and then propagated to other state machines. You should only see variable foo to change from 0 to 1.

sm dist n3 3

Last we simply send an event Event K which is supposed to take state machine state back to state S11 and you should see this happening in all browser sessions.

sm dist n1 4