Skip to content
Matija Mazi edited this page Jul 25, 2017 · 4 revisions

Rescu Interceptors are a simple way to apply the same cross-cutting concern to all HTTP method calls issued by rescu. Examples might include:

  • logging of each request,
  • timing of each request,
  • handling exceptions,
  • etc.

An Interceptor is a class that implements si.mazi.rescu.Interceptor. The implementation must call the invoke method of the passed InvocationHandler with the passed parameters. Here's an example that logs method call duration:

class LoggingInterceptor implements Interceptor {

    @Override
    public Object aroundInvoke(InvocationHandler h, Object proxy, Method m, Object[] args) 
            throws Throwable {
        long start = System.currentTimeMillis();
        Object result = h.invoke(proxy, m, args);
        System.out.printf("%s took %s ms.%n", m.getName(), System.currentTimeMillis() - start);
        return result;
    }
}

Register your interceptor when you create the proxy:

MyService proxy = RestProxyFactory.createProxy(MyService.class, "http://api.myservice.com/", null, new LoggingInterceptor());
Clone this wiki locally