import org.jodah.sarge.Plan ; import org.jodah.sarge.Plans ; import org.jodah.sarge.Sarge ; import org.jodah.sarge.util.Duration ; Plan plan = Plans .retryOn (RuntimeException.class, 5, Duration.millis(10)) .escalateOn (InterruptedException.class) .rethrowOn (IllegalArgumentException.class) .make(); Sarge sarg = new Sarge () ; App app = sarg.supervise (App.class, plan) ; app.mayFail () ; app.fail () ;这里实现的诀窍大概就是为这些东西创建一个 proxy,这些 proxy 有我们定制过的 handler,handler 将 Plan 的细节应用到传递 Method 的过程中,比如如果 catch 到了某个 exception 就交给 Plan 分析如何处理。这也算是一种 decorator 么?
Showing posts with label Proxy. Show all posts
Showing posts with label Proxy. Show all posts
Thursday, January 24, 2013
retry 的一种实现
调用外部的 service 总会有失败的可能,我们总会不停的写 while 什么 try,然后 catch 到 exception 就 Thread.sleep 之后继续,直到 fail 次数足够多了或者如何我们才放手。为了减少这种重复的 code,一个叫 sarge 的 library 使得整个问题变得简单了很多。我们看看下面的例子:
Subscribe to:
Posts (Atom)