首页 文章

Jersey 客户:是否可以拦截每项绩效评估请求?

提问于
浏览
0

我目前正在为我们正在开发的Rest Service提供Rest Client . 我希望能够 measure the performance of all client calls . 人们很容易做到这样的事情:

WebTarget target ... // just assume a WebTarget is given
long before = System.currentTimeMillis();
Response response = target.request().get(); // execute the request
long after = System.currentTimeMillis();
long timeTaken = after - before;
// now log timeTaken or whatever one might wanna do

Is it possible to intercept each request, so that i can apply this code to each request? 我不想一遍又一遍地重复这段代码 . 我搜索了一段代码,我可以覆盖它以执行此代码,但我找不到合适的代码 .

1 回答

  • 2

    如果您使用的是Jersey 2.6或更高版本,则可以使用新添加的HK2 AOP feature . 基本上你要做的是接近应用程序初始化的地方,你会添加一个Interception Service的实现并提供你的AOP联盟方法拦截器 . 然后,您可以在拦截器中编写要测量性能的任何代码 .

相关问题