/[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 3212 - (hide annotations) (download)
Thu Dec 28 09:34:47 2017 UTC (6 years, 5 months ago) by torben
File size: 1628 byte(s)
Use generics to encapsulate returned value
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 torben 3212 public class AnnotatedInvocation<T> implements CircuitInvocation<T> {
26 torben 1290
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 torben 3212 @SuppressWarnings("unchecked")
42     public T proceed() throws Exception {
43 torben 1290
44     Method methods[] = obj.getClass().getMethods();
45     for(Method current : methods) {
46     Annotation anno = current.getAnnotation(CircuitBreakerMethod.class);
47    
48    
49     if (anno != null ) {
50     try {
51 torben 3212 return (T) current.invoke(obj, (Object[])null); //invoke the first method annotated
52 torben 1290 } catch (InvocationTargetException e) {
53     throw (Exception) e.getCause(); //unwrap any exception from InvocationTargetException
54     }
55     }
56    
57     }
58    
59     throw new CBAnnotationException(); //if no method was found
60     }
61     }

  ViewVC Help
Powered by ViewVC 1.1.20