Search code examples
javajakarta-eedesign-patternsarchitecturechain-of-responsibility

Chain of Responsibility with multithreading and exception handling in java


I have a Rest service call for chain of responsibility, for the better performance we have added multi threading but unable to handle the exception Please find the code sample: Starting with Rest Service method

 @POST
    @Path("/dispatch")
    public Response dispatchEvent(){
     Thread steps = new Thread(new HandlerExecutor());
            steps.start();
    }

HandlerExecutor.java:

public class HandlerExecutor implements Runnable {
    @Override
    public void run() {
        Handler first = HandlerLocator.getHandler();
        if (first != null) {
            first.process();
        }
    }
} 

HandlerLocator.java:

public class HandlerLocator {
    public static Map<String, List<Handler>> allHandlers = null;        
    public static Handler getHandler() {    
        Handler first = null;           
        List<Handler>> h = new HashMap<String, List<Handler>>();
            List<Handler> list = new ArrayList<Handler>();
            list.add(new ConsoleHandler());
            list.add(new FileHandler());
            list.add(new FinalHandler());
            h.put("1", list);
        List<Handler> clientHandlers = h.get("1");          
        if (clientHandlers != null) {
            int size = clientHandlers.size();
                Handler h1, prev = null;

                for (int i = 0; i < size; i++) {
                    h1 = (Handler) clientHandlers.get(i);

                    if (first == null) {
                        first = h1;
                    } else {
                        prev.setNext(h1);
                    }
                    prev = h1;
                }
        }
        return first;
    }
}

Handler.java:

public interface Handler extends Serializable {
    Handler setNext(Handler next);
    void process();
}

BasicHandler.java

public abstract class BasicHandler implements Handler {
    private Handler next;
    public BasicHandler() {
        this.next = null;
    }

    @Override
    public Handler setNext(Handler next) {
        this.next = next;
        return next;
    }

    @Override
    public void process()  {    
        doProcess();
        if (next != null) {
            next.process();
        } else {
            // done
        }
    }

    public abstract void doProcess() ;
}


public class ConsoleHandler extends BasicHandler {
    @Override
    public void doProcess()   {
        System.out.println("processed ConsoleHandler");
    }
}

same as ConsoleHandler we have FileHandler,FinalHandlers

So, the questions are these:

  1. The run method is returning void so, is there any way to handle the exceptions if ConsoleHandler throws the exception?
  2. How to roll back if the second handler executed fails?

Solution

  • Instead of implementing the Thread handling directly, you could investigate the Java concurrency classes. Using an Executor framework plus Callable would allow the return of an Exception. You can then implement whatever Exception handling/rollback you wish.

    One example of the Executor Framework and Callables is here: What is recommended way for spawning threads from a servlet in Tomcat