SagaState
In the previous chapter, we explored the concept of sagas in Evento, highlighting their role in coordinating long-running transactions. This chapter dives into SagaState
, a fundamental building block for managing the state of a saga throughout its lifecycle.
Understanding SagaState
SagaState
The SagaState
class serves as an abstract superclass for defining the state associated with a saga instance. It provides core functionalities for managing the saga's lifecycle and data.
Here's a breakdown of the key features of SagaState
:
State Management: Sagas are stateful, meaning they track relevant information during the workflow execution.
SagaState
provides a foundation for storing and manipulating this data.Ended Flag: The
ended
property indicates whether the saga has completed its execution. When a saga is set asended
(often triggered by an event signifying completion or cancellation), it's typically removed from the system. This ensures efficient resource management and prevents unnecessary processing of finished sagas.Associations: The
associations
map allows storing key-value pairs to associate data extracted from events with meaningful names. These associations become crucial for referencing specific event data within the saga logic.
Essential Methods of SagaState
SagaState
isEnded()
andsetEnded(boolean ended)
: These methods allow checking and setting theended
flag, which determines if the saga has finished processing its events. Setting the flag totrue
often initiates the saga's removal from the system.setAssociation(String eventFieldName, String value)
,getAssociation(String eventFieldName)
, andunsetAssociation(String eventFieldName)
: These methods manage theassociations
map. You can set key-value pairs to associate extracted data from event properties with meaningful names for easier retrieval later in the saga logic.
The DemoSagaState
Example
DemoSagaState
ExampleThe provided code snippet showcases a DemoSagaState
class:
It extends
SagaState
, inheriting its core functionalities.It defines a
longValue
property specific to theDemoSaga
workflow.It might be extended further to include additional state properties relevant to the saga's logic.
Using Associations in @SagaEventHandler
(Next Chapter)
@SagaEventHandler
(Next Chapter)The associations
map plays a vital role when working with @SagaEventHandler
methods (covered in the next chapter). These methods handle events and often leverage associations to retrieve data previously stored from event properties using setAssociation
. This establishes a connection between events and the saga's state, enabling informed decision-making within the saga logic.
Key Takeaways
SagaState
provides the foundation for managing the state of a saga instance in Evento.It offers functionalities to track the saga's lifecycle (ended flag) and store key-value associations for data extracted from events.
Understanding
SagaState
is essential for building effective sagas that can manage complex workflows and maintain their state across events.
The next chapter will explore @SagaEventHandler
, focusing on how sagas react to events and utilize SagaState
for informed decision-making within the workflow.
Last updated