12. State Machine ID

Various classes and interfaces use machineId either as a variable or parameter in a methods. This chapter takes a closer look how machineId relates to normal machine operation and instantiation.

During a runtime machineId really don’t have any big operational role except to distinguish machines from each other for example when following logs or doing deeper debugging. Having a lot of different machine instances quickly gets user lost in translation if there is no easy way to identify these instances and option to set this machineId was given to a user.

12.1 With @EnableStateMachine

Setting machineId via JavaConfig as mymachine then exposes that for logs as shown above. This same machineId is also available via method StateMachine.getId().

@Override
public void configure(StateMachineConfigurationConfigurer<String, String> config)
        throws Exception {
    config
        .withConfiguration()
            .machineId("mymachine");
}
11:23:54,509  INFO main support.LifecycleObjectSupport [main] -
started S2 S1  / S1 / uuid=8fe53d34-8c85-49fd-a6ba-773da15fcaf1 / id=mymachine
[Note]Note

Manual builder Section 13.2, “State Machine via Builder” uses same config interface meaning behaviour would be equivalent.

12.2 With @EnableStateMachineFactory

You’ll see same machineId getting configured if you use a StateMachineFactory and request a new machine using id.

StateMachineFactory<String, String> factory = context.getBean(StateMachineFactory.class);
StateMachine<String, String> machine = factory.getStateMachine("mymachine");

12.3 With StateMachineModelFactory

Behind a scenes all machine configurations are first translated into a StateMachineModel so that StateMachineFactory don’t need to know from where configuration originated as machine can be built from JavaConfig, UML or Repository. If user wants to go crazy a custom StateMachineModel can also be used which would be a lowest possible level to define configuration.

What all these has to do with a machineId? StateMachineModelFactory also have a method StateMachineModel<S, E> build(String machineId) which a StateMachineModelFactory implementation may choose to use.

RepositoryStateMachineModelFactory Chapter 34, Repository Support uses machineId to support different configurations in a persistent storage used via Spring Data Repository interfaces. For example both StateRepository and TransitionRepository have a method List<T> findByMachineId(String machineId) order to build different states and transitions by a machineId. With RepositoryStateMachineModelFactory if machineId is used as empty or NULL defaults to repository config(in a backing persistent model) without known machine id.

[Note]Note

UmlStateMachineModelFactory currently doesn’t distinguish between different machine id’s as uml source is always coming from a same file. Thought this may get changed in future releases.