Enterprise applications centre around workflows. Old news, I know. Yet, I have not seen that many projects which would tackle complex business logic with elegance and transparency much appreciated when it comes to product maintenance. It is not that developers fail to organize and document their code. The challenge lies in dealing with constantly changing requirements. Sooner or later, one has to resort to quick hacks. Exceptions breaking common sense keep growing until the application is no longer reliable or maintainable. This post suggests how to automate business processes with ease and as little effort as possible.
One of the issues I have with workflows is that they are hard to document. Honestly, one cannot expect a domain expert to come up with complete and fully thought-through state or activity diagrams. More often than not a feeble attempt in the early stage of the project is made and further deviations from the initial draft have to be figured out from the source code. BDD to the rescue - user stories over diagrams.
A set of user stories does not need to be exhaustive. A few most important ones will do. The point is that changing a few sentences or adding brand new stories is far easier than adding or redrawing diagrams. Even though messing up with text won't bring us much closer to the actual implementation it caters for effortless and up-to-date workflow definitions.
Next, the captured stories need to be attached to automated tests. This step takes some effort already. However, there are several frameworks that make it a bit easier. I have chosen JBehave since it is based on JUnit and plays well with Spring. Provided the unimplemented feature, custom workflow engine in this case, is in place we end up with a bunch of failing tests.
Finally, we are ready to deal with the actual workflow engine implementation. Typically, people get assigned to tasks and are supposed to close them off which in turn triggers other workflows etc. The Activiti framework supports several kinds of tasks, joins and other elements a workflow can consist of. It also comes shipped with user management and fairly good reporting options. One of the features I find highly useful is the email task.
There are only two things a developer needs to do - implement the business specific code and configure the workflows by using the BPMN 2.0 notation. BPMN strives for a commonly understood representation of business processes and defines graphical notations. The standard has become popular and is supported by a number of vendors.
That's all for now. I will follow up on this topic by providing a concrete example based on Activiti and JBehave.