io.statusmachina.core.spi.StateMachineService Maven / Gradle / Ivy
/*
*
* Copyright 2019 <---> Present Status Machina Contributors (https://github.com/entzik/status-machina/graphs/contributors)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* This software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
*/
package io.statusmachina.core.spi;
import io.statusmachina.core.api.Machine;
import io.statusmachina.core.api.MachineDefinition;
import io.statusmachina.core.api.MachineSnapshot;
import java.util.List;
import java.util.Map;
/**
* An interface that provides state machine lifecycle services. it allows to create and find state machines.
* typical implementations will extract some sort of internal state and persist it in a database or distributed cache
*
* @param the java type that defines the states of the machine
* @param the java type that defines events the machine reacts to
*/
public interface StateMachineService {
/**
* Create a state machine of the specified type and with the specified initial context.
*
* Specific implementations may (should) persist the machine as well
*
* An ID should be automatically assigned to this machine
*
* The machine must automatically execute any STP transitions configured our of the initial state
*
* @param type the machine definition
* @param context the initial context
* @return an instance of the machine
* @throws Exception is thrown if anything goes wrong
*/
Machine newMachine(MachineDefinition type, Map context) throws Exception;
/**
* Create a state machine of the specified type and with the specified initial context.
*
* Specific implementations may (should) persist the machine as well
*
* The machine must automatically execute any STP transitions configured our of the initial state
*
* @param type the machine definition
* @param id an ID the user wants to be assigned to this machine
* @param context the initial context
* @return an instance of the machine
* @throws Exception is thrown if anything goes wrong
*/
Machine newMachine(MachineDefinition type, String id, Map context) throws Exception;
/**
* Locates, restores and returns the state machine of the specified type and with the specified ID
*
* The machine must automatically execute any STP transitions configured our of the initial state
*
* An exception must be thrown if the ID points to a machine that is not of the specified type
*
* @param def the machine definition
* @param id the id of the machine
* @return an instance of the machine
* @throws Exception if anything goes wrong
*/
Machine read(MachineDefinition def, String id) throws Exception;
/**
* Finds all state machines that have not executed any transitions during in the specified timeout
*
* @param seconds - the timeout
*
* @return a list of machine descriptions
*/
List findStale(long seconds);
/**
* finds all state machines stuck in an error state
* @return a list of machine descriptions
*/
List findFailed();
/**
* finds all state machines in terminal states
* @return a list of machine descriptions
*/
List findTerminated();
}