3
我正在用java做一些测试例子,我想出了一个使用@AroundInvoke的例子。问题是我不确切知道调用的方法在哪里。@AroundInvoke拦截器在哪里被调用?
该测试在调用post()方法的地方进行调用,但我真的不知道这是如何工作的(Using Interceptors explanation)。
@Test
public void crudtest() {
JsonObjectBuilder todoBuilder = Json.createObjectBuilder();
JsonObject todoToCreate = todoBuilder.
add("caption", "implement").
add("priority", 10).
build();
//The next post execute, invoke the method
Response postResponse = this.provider.target().request().
post(Entity.json(todoToCreate));
}
调用的顺序是BoundaryLogger然后MonitorSink
BoundaryLogger.java
...
public class BoundaryLogger {
@Inject
Event<CallEvent> monitoring;
@AroundInvoke
public Object logCall(InvocationContext ic) throws Exception {
long start = System.currentTimeMillis();
try {
return ic.proceed();
} finally {
long duration = System.currentTimeMillis() - start;
monitoring.fire(new CallEvent(ic.getMethod().getName(), duration));
}
}
}
MonitorSink
@Singleton
@ConcurrencyManagement(ConcurrencyManagementType.BEAN)
public class MonitorSink {
@Inject
LogSink LOG;
public void onCallEvent(@Observes CallEvent event){
LOG.log(event.toString());
}
}