Jagger
 All Classes Namespaces Files Functions Variables Groups Pages
InvocationListener.java
Go to the documentation of this file.
1 package com.griddynamics.jagger.engine.e1.collector.invocation;
2 
3 import com.griddynamics.jagger.invoker.InvocationException;
4 import org.slf4j.Logger;
5 import org.slf4j.LoggerFactory;
6 
7 import java.util.List;
8 
21 public abstract class InvocationListener<Q, R, E> {
22 
25  public void onStart(InvocationInfo<Q, R, E> invocationInfo){
26  }
27 
30  public void onSuccess(InvocationInfo<Q, R, E> invocationInfo){
31  }
32 
36  public void onFail(InvocationInfo<Q, R, E> invocationInfo, InvocationException e){
37  }
38 
42  public void onError(InvocationInfo<Q, R, E> invocationInfo, Throwable error){
43  }
44 
47  public static class Composer<Q, R, E> extends InvocationListener<Q, R, E>{
48  private static Logger log = LoggerFactory.getLogger(Composer.class);
49 
50  private List<InvocationListener<Q, R, E>> listeners;
51 
52  public Composer(List<InvocationListener<Q, R, E>> listeners) {
53  this.listeners = listeners;
54  }
55 
56  public static <Q, R, E>InvocationListener compose(List<InvocationListener<Q, R, E>> listeners){
57  return new Composer<Q, R, E>(listeners);
58  }
59 
60  @Override
61  public void onStart(InvocationInfo<Q, R, E> invocationInfo) {
62  for (InvocationListener<Q, R, E> listener : listeners){
63  try{
64  listener.onStart(invocationInfo);
65  }catch (RuntimeException ex){
66  log.error("Failed to call onStart in {} listener-invocation", listener.toString(), ex);
67  }
68  }
69  }
70 
71  @Override
72  public void onSuccess(InvocationInfo<Q, R, E> invocationInfo) {
73  for (InvocationListener<Q, R, E> listener : listeners){
74  try{
75  listener.onSuccess(invocationInfo);
76  }catch (RuntimeException ex){
77  log.error("Failed to call onSuccess in {} listener-invocation", listener.toString(), ex);
78  }
79  }
80  }
81 
82  @Override
83  public void onFail(InvocationInfo<Q, R, E> invocationInfo, InvocationException e) {
84  for (InvocationListener<Q, R, E> listener : listeners){
85  try{
86  listener.onFail(invocationInfo, e);
87  }catch (RuntimeException ex){
88  log.error("Failed to call onFail in {} listener-invocation", listener.toString(), ex);
89  }
90  }
91  }
92 
93  @Override
94  public void onError(InvocationInfo<Q, R, E> invocationInfo, Throwable error) {
95  for (InvocationListener<Q, R, E> listener : listeners){
96  try{
97  listener.onError(invocationInfo, error);
98  }catch (RuntimeException ex){
99  log.error("Failed to call onError in {} listener-invocation", listener.toString(), ex);
100  }
101  }
102  }
103  }
104 }