Annotation Based Pipeline Job Executor [Java]
Have you ever lost within if-else statements when you are creating a complex logic? Most of the time in a sequential job we called it a pipeline, we do a lot of decision makings within the flow. There can be several alternatives that we need to tackle. So, are you going to use nested statements? No way!.
Recently, I have faced the same problem. I have asked to implement a pipeline job that has seven individual processes to perform in a sequence. But based on the state of each process, the flow has to be changed accordingly. This was my simple requirement and I ended up with multiple nested conditions deciding which way that the process needs to go. Boom! after few hours I have lost within my own code matching if-else blocks.
Finally, I narrowed down my requirement and identified that each job has just two decisions to make, success path and the failure path. So, I have implemented the decision making logic at one place and process it in another place. I have split each individual process as a method and used java annotations to contain the logic that each process needs to go through.
I am taking a simple example to explain this thing to you.
Above diagram shows you a simple flowchart of the example that we are going to discuss. There is a starting process “A” and an ending process “E”. If “A” fails, “D” and “E” processes need to be executed in order. The same process needs to be followed by “B” and “C” as well. This is somewhat similar to Java try-catch-finally statement. The process “D” includes the logic that needs to be executed within the “catch” block. “E” is simply the cleanup job that we need to include within the “finally” block.
This is like a binary tree traversal. We are selecting a branch based on the branching condition. We are using the same approach to traverse through this pipeline and execute each process one after another. Here I am including the entire pipeline job within a class and each job as a method of that class. Basically, we need two annotations to be defined at the class level and the method level.
Let’s start with implementation
So, our first annotation is @Pipeline which we are going to use at class level. It simply contains the starting job (root job) of the pipeline
@Job annotation is defined at method level to indicate the flow of execution.
Now we need to implement the logic to handle pipeline process.
Our final pipeline job is given below.
You can change the successful method and failure method according to your requirement. That simple change will determine the flow execution. Change the return statement of a job to Job.Status.FAILURE if and only if you want to switch execution flow in failure path.
Let’s run this code
Our output would be like this.
Play with the code :)