Chain of Responsibility with multithreading and exception handling in java -
Chain of Responsibility with multithreading and exception handling in java -
i have rest service phone call chain of responsibility, improve performance have added multi threading unable handle exception please find code sample: starting 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; (int = 0; < size; i++) { h1 = (handler) clienthandlers.get(i); if (first == null) { first = h1; } else { prev.setnext(h1); } prev = h1; } } homecoming 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; homecoming 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 consolehandler have filehandler,finalhandlers
so, questions these:
the run method returning void so, there way handle exceptions ifconsolehandler
throws exception? how roll if sec handler executed fails?
instead of implementing thread handling directly, investigate java concurrency classes. using executor framework plus callable allow homecoming of exception. can implement whatever exception handling/rollback wish.
one illustration of executor framework , callables here: what recommended way spawning threads servlet in tomcat
java java-ee design-patterns architecture chain-of-responsibility
Comments
Post a Comment