javaoopspring-bootdesign-patternsweb-application-design

Designing custom workflow in JAVA and Spring


I am working on an spring 2.0.1.RELEASE application.

Brief of Application:

1. I have separate Transformer beans that transforms my DTO to Domain
and vice versa.
2. I have separate Validator beans that validate my domain object being passed.
3. I have Service classes that takes care of the applying rules and calling persistence layer.

Now, i want to build a Workflow in my application: where i will just call the start of the workflow and below mentioned steps will be executed in order and exception handling will be done as per the step:

1.First-Transformtion - transformToDomain() method will be called for that object type.  
2.Second-Validator - class valid() method will be called for that object.  
3.Third-Service - class save() method will be called for that object.  
4.Fourth- Transformation - transformToDTO() method will be called for that object type.

after this my workflow ends and i will return the DTO object as response of my REST API.

Exception handling part is the one, i also want to take care of, like if particular exception handler exist for that step then call it, else call global exception handler.

I designed some prototype of same, but looking for some expert advice and how this can be achieved with a better design in java.


Explanation with example considering above use case is highly appreciable.


Solution

  • Brief of what i implemented in a way with not much hustle:

    This is my design classes

    This is how I created flow of handlers:

    Stream.<Supplier<RequestHandler>>of(
                TransformToDomainRequestHandler::new,
                ValidateRequestHandler::new,
                PersistenceHandler::new,
                TransformToDTORequestHandler::new)
                .sequential()
                .map(c -> c.get()) /* Create the handler instance */
                .reduce((processed, unProcessed) -> { /* chains all handlers together */
                    RequestHandler previous = processed;
                    RequestHandler target = previous.getNextRequestHandler();
                    while (target != null && previous != null) {
                        previous = target;
                        target = target.getNextRequestHandler();
                    }
                    previous.setNextRequestHandler(unProcessed);
                    return processed;
                }).get();
    

    This is my Workflow State container

    This is my Request Handler which all other handler extends


    enter image description here