Jagger
 All Classes Namespaces Files Functions Variables Enumerator 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 
22 public abstract class InvocationListener<Q, R, E> {
23 
26  public void onStart(InvocationInfo<Q, R, E> invocationInfo){
27  }
28 
31  public void onSuccess(InvocationInfo<Q, R, E> invocationInfo){
32  }
33 
37  public void onFail(InvocationInfo<Q, R, E> invocationInfo, InvocationException e){
38  }
39 
43  public void onError(InvocationInfo<Q, R, E> invocationInfo, Throwable error){
44  }
45 
48  public static class Composer<Q, R, E> extends InvocationListener<Q, R, E>{
49  private static Logger log = LoggerFactory.getLogger(Composer.class);
50 
51  private List<InvocationListener<Q, R, E>> listeners;
52 
53  public Composer(List<InvocationListener<Q, R, E>> listeners) {
54  this.listeners = listeners;
55  }
56 
57  public static <Q, R, E>InvocationListener compose(List<InvocationListener<Q, R, E>> listeners){
58  return new Composer<Q, R, E>(listeners);
59  }
60 
61  @Override
62  public void onStart(InvocationInfo<Q, R, E> invocationInfo) {
63  for (InvocationListener<Q, R, E> listener : listeners){
64  try{
65  listener.onStart(invocationInfo);
66  }catch (RuntimeException ex){
67  log.error("Failed to call onStart in {} listener-invocation", listener.toString(), ex);
68  }
69  }
70  }
71 
72  @Override
73  public void onSuccess(InvocationInfo<Q, R, E> invocationInfo) {
74  for (InvocationListener<Q, R, E> listener : listeners){
75  try{
76  listener.onSuccess(invocationInfo);
77  }catch (RuntimeException ex){
78  log.error("Failed to call onSuccess in {} listener-invocation", listener.toString(), ex);
79  }
80  }
81  }
82 
83  @Override
84  public void onFail(InvocationInfo<Q, R, E> invocationInfo, InvocationException e) {
85  for (InvocationListener<Q, R, E> listener : listeners){
86  try{
87  listener.onFail(invocationInfo, e);
88  }catch (RuntimeException ex){
89  log.error("Failed to call onFail in {} listener-invocation", listener.toString(), ex);
90  }
91  }
92  }
93 
94  @Override
95  public void onError(InvocationInfo<Q, R, E> invocationInfo, Throwable error) {
96  for (InvocationListener<Q, R, E> listener : listeners){
97  try{
98  listener.onError(invocationInfo, error);
99  }catch (RuntimeException ex){
100  log.error("Failed to call onError in {} listener-invocation", listener.toString(), ex);
101  }
102  }
103  }
104  }
105 }