jeudi 1 septembre 2016

How to create chain of dynamic proxies?

I create two InvocationHandler, one for logging purpose and the other one for measuring time. Each on works but I do not know how to create a chain of these two, so that both will be executed. I thought it would be enough that for example the LoggingInvocationHandler extends the TimerInvocationHandler

public class DynamicProxyMain {

public static void main(String[] args) {
    System.out.println("Starting dynamic proxy sample");

    SubjectInterface timerProxy = (SubjectInterface) Proxy.newProxyInstance(SubjectInterface.class.getClassLoader(),
            new Class<?>[]{SubjectInterface.class},
            new TimerInvocationHandler(new SubjectInterfaceImpl()));

    SubjectInterface logginProxy = (SubjectInterface) Proxy.newProxyInstance(SubjectInterface.class.getClassLoader(),
            new Class<?>[]{SubjectInterface.class},
            new LoggingInvocationHandler(new SubjectInterfaceImpl()));
    logginProxy.methodA("a");
    logginProxy.methodB("test b");
    logginProxy.methodC(1, "test c");

}

}

In this example the loggingProxy is active but not the other the timerProxy. Thanks

Aucun commentaire:

Enregistrer un commentaire