【问题标题】:JAX-WS Monitor WebMethod usage and performanceJAX-WS 监控 WebMethod 的使用和性能
【发布时间】:2019-02-16 14:23:04
【问题描述】:

我正在构建一个基于令牌的身份验证的 WebService,令牌是从肥皂头中提取的。我想监控/分析每个 WebMethod,我想知道哪个用户调用了哪个 WebMethod,以及处理它需要多长时间。我无法使用外部监控工具,因为我仍然需要提取令牌,所以我知道哪个用户实际调用了该 WebMethod。

我现在使用一个简单的 WebFilter 类,它在每个 WebMethod 之前手动调用,如果用户没有调用该方法的权限,则会引发异常。但是在每个方法之前和计算时间之后添加类似long start = System.currentMillis(); 的东西并不是一个解决方案,因为我会有数百种方法。

@WebService (serviceName = "UserService")
public class UserService implements IUserService {

@Resource
WebServiceContext wsctx;

@WebMethod
@Override
public User getUser(@WebParam(name = "name") String name) throws ServiceException {
    WebFilter.filter(wsctx.getMessageContext());
    return userManager.getUser(name);
}

我正在寻找类似 Interceptors 的解决方案,我需要在每个 WebMethod 之前和之后调用一些东西,但是 Interceptors 并不能真正与 WebMethods 一起工作,所以我有点绝望。

【问题讨论】:

    标签: java web-services monitoring


    【解决方案1】:

    在每个被监控的类之前用注释解决:

    @Interceptors({PerformanceInterceptor.class})
    

    还有这个拦截器的实现:

    @AroundInvoke
    public Object methodInterceptor(InvocationContext ctx) throws Exception {
       long start = System.currentTimeMillis();
       try {
           return ctx.proceed();
       } finally {
           long end = System.currentTimeMillis();
           System.out.println(ctx.getMethod().getName() + " took: " + (end - start) + "ms");
       }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-16
      • 2012-04-03
      相关资源
      最近更新 更多