Skip to content

Commit

Permalink
Merge pull request #2531 from AxonFramework/enhancement/caching-saga-it
Browse files Browse the repository at this point in the history
Saga Caching Enhancements
  • Loading branch information
smcvb committed Dec 29, 2022
2 parents 856b880 + 9cf9d45 commit 1746fb7
Show file tree
Hide file tree
Showing 5 changed files with 373 additions and 119 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010-2018. Axon Framework
* Copyright (c) 2010-2022. Axon Framework
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -22,6 +22,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
* Test saga used by the {@link CachingIntegrationTestSuite}.
Expand All @@ -32,17 +33,33 @@ public class CachedSaga {

private String name;
private List<Object> state;
private int numberOfAssociations;
private Random random;
private Integer associationToRemove;

@StartSaga
@SagaEventHandler(associationProperty = "id")
public void on(SagaCreatedEvent event) {
this.name = event.name;
this.state = new ArrayList<>();
this.numberOfAssociations = event.numberOfAssociations;
this.random = new Random();

for (int i = 0; i < numberOfAssociations; i++) {
SagaLifecycle.associateWith(event.id + i, i);
}
}

@SagaEventHandler(associationProperty = "id")
public void on(VeryImportantEvent event) {
state.add(event.stateEntry);
if (associationToRemove == null) {
associationToRemove = random.nextInt(numberOfAssociations);
SagaLifecycle.removeAssociationWith(event.id + associationToRemove, associationToRemove);
} else {
SagaLifecycle.associateWith(event.id + associationToRemove, associationToRemove);
associationToRemove = null;
}
}

@SagaEventHandler(associationProperty = "id")
Expand All @@ -65,10 +82,12 @@ public static class SagaCreatedEvent {

private final String id;
private final String name;
private final int numberOfAssociations;

public SagaCreatedEvent(String id, String name) {
public SagaCreatedEvent(String id, String name, int numberOfAssociations) {
this.id = id;
this.name = name;
this.numberOfAssociations = numberOfAssociations;
}

public String getId() {
Expand Down

0 comments on commit 1746fb7

Please sign in to comment.