Creating Aggregator Class

The aggregatorref class does represent your business domain, and it is the container for carrying out the data for the executors. In side of the aggregator class, you should provide all the data regarding your entire transaction.

example: Just think you are will be to make a transaction for place and order by using StackSaga. then you have to create an Aggregator class to put the data that you want to while the whole process. you can create an Aggregator class called PlaceOrderAggregator and provide all the data that you want like username, orderId, transactionId etc…​ and also you might want to add complex data structure inside that aggregator class like list of items regarding the items that the customer’s order. then you have to create a java pojo as well. as a best practice, the framework suggests you to create all related pojo classes in the same package of the Aggregator class or as the inner classes of the Aggregator class.

To be an aggregator in StackSaga, Mainly, The aggregator class should be extended from org.stacksaga.Aggregator class and also the aggregator class should be annotated with org.stacksaga.core.annotation.SagaAggregator.

All the classes (main aggregator class and other classes that are used inside it) that are used for the SagaAggregator should be implemented by the java.io.Serializable class. By default, The main aggregator class is Serializable due to the fact that it is implemented from the Aggregator class. See complex object implementation

Let’s see how it is created in StackSaga project.

@SagaAggregator((1)
        version = @SagaAggregatorVersion(major = 1, minor = 0, patch = 1), (2)
        idPrefix = "po", (3)
        name = "PlaceOrderAggregator", (4)
        sagaSerializable = PlaceOrderAggregatorSample.class, (5)
        keyGen = PlaceOrderAggregatorKeyGenerator.class (6)
)
@Getter (7)
@Setter (8)
public class PlaceOrderAggregator extends Aggregator  { (9)

    (10)
    public PlaceOrderAggregator() {
        super(PlaceOrderAggregator.class);
    }

    private String orderId;
    private String username;
    private int isActive;
}
1 The aggregator class should be annotated with org.stacksaga.core.annotation.SagaAggregator.
2 version will be used for the identification of the aggregator versioning. it is helpful for the event-upper-casting and event-down-casting. org.stacksaga.core.annotation.SagaAggregatorVersion annotation help you to provide the version of the aggregator.
3 idPrefix will be used as the prefix of the transaction id. you can give a prefix according to the aggregator name. The maximum length of the prefix is 4 characters.
4 name The name of the aggregator. this is used for identification of the aggregator by the name.
5 keyGen is used for creating unique id for the aggregator. the keyGen has a default implementation. even though if it is not necessary to provide, providing a custom ID generator can beneficial for sometimes like make proper indexing. see the implementation
6 sagaSerializable is used for pre-serialization of the samples of the aggregator. see the implementation
7 Getter you can create getters as usual for the aggregator.
8 Setter you can create setters as usual for the aggregator.
9 The aggregator class should be implemented from the org.stacksaga.Aggregator class. It provides the shep of aggregator in the framework.
10 The aggregator class should have the default constructor and also the super() method should be called by passing the same aggregator class.
The name of the aggregator should be the same forever. Because, all the event-store data is mapped with the aggregator name.
Due to the fact that the aggregator name is configured by an attribute, you can change the package of the aggregator class anytime.
In StackSaga, The aggregator is not a spring bean at all. Therefore, it is not necessary to have inside the component scan area.