14

【对线面试官】Java注解

 3 years ago
source link: https://zhuanlan.zhihu.com/p/339231602
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

【对线面试官】Java注解

公众号:Java3y
v2-9704addda85bf97141228238f423173b_720w.jpgv2-e513b28c7c1e36d1a38727a956436795_720w.jpgv2-2c7474ad784dc8fff1231cc1261ab5b5_720w.jpgv2-4db453cf84d2da165addf537cb7a29ee_720w.jpg
public void send(String userName) {
  try {
    // qps 上报
    qps(params);
    long startTime = System.currentTimeMillis();

    // 构建上下文(模拟业务代码)
    ProcessContext processContext = new ProcessContext();
    UserModel userModel = new UserModel();
    userModel.setAge("22");
    userModel.setName(userName);
    //...

    // rt 上报
    long endTime = System.currentTimeMillis();
    rt(endTime - startTime);
  } catch (Exception e) {
    
    // 出错上报
    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);

        //方法执行开始时间
        long startTime = System.currentTimeMillis();

        Object o = null;
        try {
            o = pjp.proceed();
        } catch (Exception e) {
            //方法执行结束时间
            long endTime = System.currentTimeMillis();

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

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

        //方法执行结束时间
        long endTime = System.currentTimeMillis();

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

        if (null != o) {
            monitor.sum(functionName, "done", 1);
        }
        return o;
    }
v2-780c33913c5431e1a381db1a44c59792_720w.jpgv2-f655b149ba1924fda9039d5fea184a29_720w.jpgv2-c8e58a212072f928ce29fef241d7bb95_720w.jpg

文章以纯面试的角度去讲解,所以有很多的细节是未铺垫的。

比如说反射、.java文件到jvm的过程、AOP是什么等等等...这些在【Java3y】都有过详细的基本教程甚至电子书,我就不再详述了。

欢迎关注微信搜【面试造火箭】来聊聊Java面试

发布于 2020-12-24

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK