有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在java计时器注释示例中编写自定义注释

基本上,我需要跟踪在使用自定义注释的方法中花费的时间。(我知道spring AOP可以用于此,但我们不能在我们的产品中使用它)

public class TimeCounterDemo {
    
    @TimeCounter
    public void trackMyTimeSpentUsingAnnotation()
    {
        //some heavy processing stuff
    }   
}

public @interface TimeCounter {
  //need help with this implementation.
}

所以,我的要求是完成TimeCounter注释。 要求很简单-

  1. 方法启动的日志时间
  2. 方法结束的日志时间
  3. 记录在方法中花费的总时间
  4. 已执行方法的名称

有人能帮我实现上述要求的注释吗

提前谢谢


共 (2) 个答案

  1. # 1 楼答案

    已经有很多库提供了这样的注释。 如果您想要自己的实现,其中一种方法是使用dynamic proxies

    下面是你的TimeCounterDemo的样子:

    计时器(注释)

    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.METHOD)
    public @interface TimeCounter {
    
    }
    

    ITimerCounterDemo(接口)

    public interface ITimerCounterDemo {
        @TimeCounter
        public void trackMyTimeSpentUsingAnnotation();
    
        public void someOtherMethod(int a);
    }
    

    TimerCounterDemo(上述接口的实现)

    
    public class TimerCounterDemo implements ITimerCounterDemo {
        
        public void trackMyTimeSpentUsingAnnotation() {
            System.out.println("TimerCounterDemo:: Going to sleep");
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
            }
            System.out.println("TimerCounterDemo:: Completed.");
        }
    
        public void someOtherMethod(int a) {
            System.out.println("In someothermethod with value:: " + a);
        }
    }
    

    TimerProxy

    import java.lang.reflect.InvocationHandler;
    import java.lang.reflect.Method;
    import java.lang.reflect.Proxy;
    import java.time.Duration;
    import java.time.LocalDateTime;
    import java.util.Objects;
    
    public class TimerProxy implements InvocationHandler {
    
        private Object targetObj;
    
        public static Object newInstance(Object targetObj) {
            Objects.requireNonNull(targetObj);
            return Proxy.newProxyInstance(
                          targetObj.getClass().getClassLoader(), 
                          targetObj.getClass().getInterfaces(),
                          new TimerProxy(targetObj)
                   );
        }
    
        private TimerProxy(Object targetObj) {
            this.targetObj = targetObj;
        }
    
        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            if (method.isAnnotationPresent(TimeCounter.class)) {
    
                LocalDateTime start = LocalDateTime.now();
                Object returnObj = method.invoke(targetObj, args);
    
                System.out.println(method.getName() + " executed in "
                        + Duration.between(start, LocalDateTime.now()).getSeconds() + " seconds");
                return returnObj;
            }
    
            return method.invoke(targetObj, args);
        }
    
    }
    

    测试计时器:

    public class TimerTest {
        public static void main(String[] args) throws InterruptedException {
            ITimerCounterDemo t = (ITimerCounterDemo) TimerProxy.newInstance(new TimerCounterDemo());
            t.someOtherMethod(10);
            t.trackMyTimeSpentUsingAnnotation();
        }
    }
    
    

    输出:

    In someothermethod with value:: 10
    TimerCounterDemo:: Going to sleep
    TimerCounterDemo:: Completed.
    trackMyTimeSpentUsingAnnotation executed in 2 seconds
    

    你可以阅读更多关于herehere

  2. # 2 楼答案

    您可以直接在方法中实现时间跟踪,而不是编写注释。 方法中的前两行必须是

    long startTime = System.currentTimeMillis();
    logger.info("Method started at = " + new Date(startTime);
    

    现在,您必须记录该方法花费的总时间。这可以通过将下面的语句写入相同的方法来实现

    logger.info("Total time taken by the method is = " + (System.currentTimeMillis() - startTime));