I am using akka stream to process my data. In which I have 1 Source which consists of element UUID.
The flow is as follows :
- is fetching the Element from some third party HTTP service which returns complete Element with its properties.
- Then I retrieve required data from that element and convert it to object that my application understands.
- Then i write the data from that object to DB.
- Finally, I update the DB with status of all the elements in the stream.
Now I want to add retry mechanism to this flow so that if any of the stages in flow fails it should retry the stage for some no of times say 3 and if after that it fails then the only failure of the stream should happen. For example, if there are some issues with third-party service like HTTP 504 error then most of the time after retrying this element success. So is there any way in akka to achieve this.
Currently, I am maintaining 1 list to store all the failed element ids like below.
Code :
List<UUID> failedId = new CopyOnWriteArrayList<>();
Source.from(elementIdToProcess).map(f -> {
failedId.add(f);
return f;
}).via(featureFlow()).via(filterNullFeaturesFlow())
.via(mapToFeatureFlow()).via(setFeaturesAdditionalInfo())
.runForeach(features -> {
features.parallelStream().forEach(feature -> {
try {
featureCommitter.save(feature);
failedId.remove(UUID.fromString(feature.getFeatureId()));
} catch (Exception e) {
throw new RuntimeException(e);
}
});
}, materializer);