/[projects]/miscJava/CircuitBreaker/src/main/java/dk/thoerup/circuitbreaker/invocations/AnnotatedInvocation.java
ViewVC logotype

Annotation of /miscJava/CircuitBreaker/src/main/java/dk/thoerup/circuitbreaker/invocations/AnnotatedInvocation.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2572 - (hide annotations) (download)
Tue Jun 9 09:28:14 2015 UTC (8 years, 11 months ago) by torben
File size: 1591 byte(s)
FindBugs
1 torben 1290 package dk.thoerup.circuitbreaker.invocations;
2    
3     /* a annotation based invocation if it infeasible or you just don't like to implement yet another interface
4     *
5     * class MyInvocation {
6     * @CircuitBreakerMethod
7     * public void doSomething() {}
8     * }
9     *
10     * ..... snip......
11     *
12     * MyInvocation my = new MyInvocation();
13     * AnnotatedInvocation invok = new AnnotatedInvocation(my);
14     * Object out = CircuitBreakerManager.getManager().getCircuitBreaker("mycb").invoke(invok);
15     *
16     */
17    
18    
19     import java.lang.annotation.Annotation;
20     import java.lang.reflect.InvocationTargetException;
21     import java.lang.reflect.Method;
22    
23     import dk.thoerup.circuitbreaker.CircuitInvocation;
24    
25     public class AnnotatedInvocation implements CircuitInvocation {
26    
27 torben 2572 public static class CBAnnotationException extends Exception {
28 torben 1290 public CBAnnotationException() {
29     super("No method found annotated with @CircuitBreakerMethod");
30     }
31     private static final long serialVersionUID = 1L;
32    
33     }
34    
35     private Object obj;
36    
37     public AnnotatedInvocation(Object obj) {
38     this.obj = obj;
39     }
40    
41     public Object proceed() throws Exception {
42    
43     Method methods[] = obj.getClass().getMethods();
44     for(Method current : methods) {
45     Annotation anno = current.getAnnotation(CircuitBreakerMethod.class);
46    
47    
48     if (anno != null ) {
49     try {
50     return current.invoke(obj, (Object[])null); //invoke the first method annotated
51     } catch (InvocationTargetException e) {
52     throw (Exception) e.getCause(); //unwrap any exception from InvocationTargetException
53     }
54     }
55    
56     }
57    
58     throw new CBAnnotationException(); //if no method was found
59     }
60     }

  ViewVC Help
Powered by ViewVC 1.1.20