1

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?
rtruszk
  • 3,902
  • 13
  • 36
  • 53
Subha Chandra
  • 751
  • 9
  • 23

2 Answers2

1

Create and pass in an exception handler object.

Something that just has a setError(Exception e) and a getError() method.

Each thread can set it (you could make it addError(Exception e) and make a list of exceptions).

You will also need to add a callback to each of your handlers, to either commit or roll-back. Then in your higher-level function, when these all complete, ask if errors occured. If they did, send a rollback to all the handlers, otherwise send a commit to them.

Rob Conklin
  • 8,806
  • 1
  • 19
  • 23
  • Yes, We can pass the exception handler object to ConsoleHandler , if ConsoleHandler throws any exception there is no response object to send back to the user ? so how to get the exception object in Rest Service (dispatchEvent method) – Subha Chandra Apr 09 '15 at 05:10
  • Have your executor expose it. – Rob Conklin Apr 09 '15 at 13:10
1

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

Community
  • 1
  • 1
KevinO
  • 4,303
  • 4
  • 27
  • 36