Glassfish - JEE6 - 使用拦截器测量性能 [英] Glassfish - JEE6 - Use of Interceptor to measure performance

查看:157
本文介绍了Glassfish - JEE6 - 使用拦截器测量性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了测量方法的执行时间,我已经看到了使用

  public class PerformanceInterceptor {
@ AroundInvoke
Object measureTime(InvocationContext ctx)抛出异常{
long beforeTime = System.currentTimeMillis();
Object obj = null;
尝试{
obj = ctx.proceed();
return obj;
}
finally {
time = System.currentTimeMillis() - beforeTime;
//记录时间
}
}

然后把

  @Interceptors(PerformanceInterceptor.class)

之前,无论你想测量的任何方法。

无论如何,我试过这个,它似乎工作正常。



我还添加了一个

  public static long countCalls = 0; 

添加到PerformanceInterceptor类和a

  countCalls ++; 

加入到measureTime()中,这似乎也可以正常工作。

随着我的newby帽子,我会问,如果我使用countCalls是好的即
Glassfish / JEE6是o.k.与我一起使用Java类中的静态变量
作为拦截器......特别是关于线程安全性。我知道
通常你应该用Java来同步类变量的设置,但是我b $ b不知道JEE6 / Glassfish的情况。有什么想法?在这种情况下,容器没有提供任何额外的线程安全性。每个bean实例都有自己的拦截器实例。因此,多线程可以同时访问静态countCalls。



这就是为什么你必须像往常一样保护读写。其他可能性是使用 AtomicLong

  private static final AtomicLong callCount = new AtomicLong(); 

private long getCallCount(){
return callCount.get();
}

private void increaseCountCall(){
callCount.getAndIncrement();
}

正如预期的那样,只要所有实例都是在同一个JVM中,需要集群共享存储。


For measuring execution time of methods, I've seen suggestions to use

public class PerformanceInterceptor {
   @AroundInvoke
   Object measureTime(InvocationContext ctx) throws Exception {
   long beforeTime = System.currentTimeMillis();
   Object obj = null;
   try {
      obj = ctx.proceed();
      return obj;
   }
   finally {
      time = System.currentTimeMillis() - beforeTime;
      // Log time
   }
}

Then put

@Interceptors(PerformanceInterceptor.class) 

before whatever method you want measured.

Anyway I tried this and it seems to work fine.

I also added a

public static long countCalls = 0;

to the PerformanceInterceptor class and a

countCalls++; 

to the measureTime() which also seems to work o.k.

With my newby hat on, I will ask if my use of the countCalls is o.k. i.e that Glassfish/JEE6 is o.k. with me using static variables in a Java class that is used as an Interceptor.... in particular with regard to thread safety. I know that normally you are supposed to synchronize setting of class variables in Java, but I don't know what the case is with JEE6/Glassfish. Any thoughts ?

解决方案

There is not any additional thread safety provided by container in this case. Each bean instance does have its own instance of interceptor. As a consequence multiple thread can access static countCalls same time.

That's why you have to guard both reads and writes to it as usual. Other possibility is to use AtomicLong:

private static final AtomicLong callCount = new AtomicLong();

private long getCallCount() {
   return callCount.get();
}

private void increaseCountCall() {
   callCount.getAndIncrement();
}

As expected, these solutions will work only as long as all of the instances are in same JVM, for cluster shared storage is needed.

这篇关于Glassfish - JEE6 - 使用拦截器测量性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆