Designing custom workflow in JAVA and Spring

2k Views Asked by At

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.

2

There are 2 best solutions below

0
Sahil Verma On BEST ANSWER

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

6
Fana Sithole On

I'm not so sure if what you are describing is a workflow system in its true sense, perhaps a Chain of Responsibility is more of what you are talking about?

Following what you described as a sequence of execution, here is a simplified example of how I would implement the chain:

Transformer.java

public interface Transformer<IN, OUT> {

  OUT transformToDomain(IN dto);

  IN transformToDTO(OUT domainObject);
}

Validator.java

public interface Validator<T> {

  boolean isValid(T object);
}

Service.java

public interface Service {

  void save(Object object);
}

And the implementation that binds everything: ProcessChain.java

public class ProcessChain {

  private Transformer transformer;
  private Service service;
  private Validator validator;

  Object process(Object dto) throws MyValidationException {
    Object domainObject = transformer.transformToDomain(dto);
    boolean isValid = validator.isValid(domainObject);
    if(!isValid){
      throw new MyValidationException("Validation message here");
    }
    service.save(domainObject);
    return transformer.transformToDTO(domainObject);
  }
}

I haven't specified any Spring related things here because your question seems to be a design question rather than a technology questions.

Hope this helps