Integrating AWS SWF in a Spring Boot microservice

A while ago, we launched a series of articles about microservices using Java, Spring Boot and Amazon Web Services.
Today we come with a new episode inspired by what is happening into practice: there are many cases when we need to execute from time to time a background process to complete a certain task: send a report at the end of the day, compute some final statistics, notify subscribers about something new, etc. And in some situations these processes have multiple steps, some of which can be executed in parallel, but all of them have different execution times and different retry policies in case of failures.
For this need, AWS comes with several solutions, but the one that we’ll present today is Simple Workflow Service. For sure it’s not simple as it name stats, but the rage of problems it solves makes it worthwhile to spend some time to understand it.
The workflow we built for this article is not the most relevant, but the purpose of this post is just to show how to integrate SWF in a Spring Boot microservice, how to start it and how to ensure the workflow starts properly, regardless of the number of hosts the service has.
Before going into code details, it’s very important to spend enough time to understand the SWF principles and to get familiar with the AWS Flow Framework. Being quite verbose, we are not going to paste the code snippets into this blog post, but we made it available for download here. Also, several details we want to describe here:
– the workflow (under service/workflow/swf) has 3 activities: one that generates a number, one that computes the square value of a value and a third step that computes the sum of that 2 values already determined. Basically, the first 2 steps could be done in parallel, whereas the third one has to wait until the former two are done.
– the workflow described above is managed from a class named WorkflowWatchDog, that checks from time to time if there is no workflow running and if there is not, it starts one. In that case, we ensure we don’t try to run the workflow more than once, even there is more than one host in the fleet.
– we implemented here the continuous workflow concept. Basically, the current workflow is closed and another one is started.
Again, the aim of this material is not to make you familiar with AWS SWF, since this requires some time, but more to offer a concrete implementation and an example about how to integrate it in your service.
If you have any idea about how to improve it, please leave a reply below. More than that, please share this blog with anyone interested!

One thought on “Integrating AWS SWF in a Spring Boot microservice

Comments are closed.