编程知识 cdmana.com

Comment on Java

The interviewer tested me today Java annotation ...

public void send(String userName) {
  try {
    // qps  Report
    qps(params);
    long startTime = System.currentTimeMillis();

    //  Build context ( Simulate business code )
    ProcessContext processContext = new ProcessContext();
    UserModel userModel = new UserModel();
    userModel.setAge("22");
    userModel.setName(userName);
    //...

    // rt  Report
    long endTime = System.currentTimeMillis();
    rt(endTime - startTime);
  } catch (Exception e) {
    
    //  Error report
    error(params);
  }
}
@Around("@annotation(com.sanwai.service.openapi.monitor.Monitor)")
    public Object antispan(ProceedingJoinPoint pjp) throws Throwable {

        String functionName = pjp.getSignature().getName();
        Map<String, String> tags = new HashMap<>();

        logger.info(functionName);

        tags.put("functionName", functionName);
        tags.put("flag""done");

        monitor.sum(functionName, "start"1);

        // Method execution start time
        long startTime = System.currentTimeMillis();

        Object o = null;
        try {
            o = pjp.proceed();
        } catch (Exception e) {
            // Method execution end time
            long endTime = System.currentTimeMillis();

            tags.put("flag""fail");
            monitor.avg("rt", tags, endTime - startTime);

            monitor.sum(functionName, "fail"1);
            throw e;
        }

        // Method execution end time
        long endTime = System.currentTimeMillis();

        monitor.avg("rt", tags, endTime - startTime);

        if (null != o) {
            monitor.sum(functionName, "done"1);
        }
        return o;
    }

The article explains from the perspective of pure interview , So there are a lot of details that are unfounded .

For example, reflection 、.java file To jvm The process of 、AOP What is it? Wait a minute ... These in 【Java3y】 There are detailed basic tutorials and even e-books , I won't go into details .

Annotations can be used as a vehicle for configuration , Probably at run time 、 It's possible to parse annotations during compilation , Implement some easy-to-use functions .

Welcome to my WeChat official account. 【 Interview to build rocket 】 To have a chat Java interview

版权声明
本文为[Java3y]所创,转载请带上原文链接,感谢
https://cdmana.com/2020/12/20201224140210528b.html

Tags comment java
Scroll to Top