• flink源码分析 - 获取调用位置信息


    flink版本: flink-1.11.2

    调用位置: org.apache.flink.streaming.api.datastream.DataStream#flatMap(org.apache.flink.api.common.functions.FlatMapFunction)

    代码核心位置: org.apache.flink.api.java.Utils#getCallLocationName()

    flink算子flatmap中调用了一个 Utils.getCallLocationName() 方法: 

    具体源码实现如下(为方便查看,该源码类中其他内容未加入): 

    1. /*
    2. * Licensed to the Apache Software Foundation (ASF) under one
    3. * or more contributor license agreements. See the NOTICE file
    4. * distributed with this work for additional information
    5. * regarding copyright ownership. The ASF licenses this file
    6. * to you under the Apache License, Version 2.0 (the
    7. * "License"); you may not use this file except in compliance
    8. * with the License. You may obtain a copy of the License at
    9. *
    10. * http://www.apache.org/licenses/LICENSE-2.0
    11. *
    12. * Unless required by applicable law or agreed to in writing, software
    13. * distributed under the License is distributed on an "AS IS" BASIS,
    14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15. * See the License for the specific language governing permissions and
    16. * limitations under the License.
    17. */
    18. package org.apache.flink.api.java;
    19. import org.apache.flink.annotation.Internal;
    20. import org.apache.flink.api.common.accumulators.Accumulator;
    21. import org.apache.flink.api.common.accumulators.SerializedListAccumulator;
    22. import org.apache.flink.api.common.accumulators.SimpleAccumulator;
    23. import org.apache.flink.api.common.io.RichOutputFormat;
    24. import org.apache.flink.api.common.typeinfo.TypeInformation;
    25. import org.apache.flink.api.common.typeutils.CompositeType;
    26. import org.apache.flink.api.common.typeutils.TypeSerializer;
    27. import org.apache.flink.api.java.typeutils.GenericTypeInfo;
    28. import org.apache.flink.configuration.Configuration;
    29. import org.apache.commons.lang3.StringUtils;
    30. import javax.annotation.Nullable;
    31. import java.io.IOException;
    32. import java.lang.reflect.Field;
    33. import java.lang.reflect.Modifier;
    34. import java.util.Arrays;
    35. import java.util.Optional;
    36. import java.util.Random;
    37. /** Utility class that contains helper methods to work with Java APIs. */
    38. @Internal
    39. public final class Utils {
    40. public static final Random RNG = new Random();
    41. public static String getCallLocationName() {
    42. return getCallLocationName(4);
    43. }
    44. public static String getCallLocationName(int depth) {
    45. StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
    46. if (stackTrace.length <= depth) {
    47. return "";
    48. }
    49. StackTraceElement elem = stackTrace[depth];
    50. return String.format(
    51. "%s(%s:%d)", elem.getMethodName(), elem.getFileName(), elem.getLineNumber());
    52. }
    53. /** Private constructor to prevent instantiation. */
    54. private Utils() {
    55. throw new RuntimeException();
    56. }
    57. }

    从中可以看出,该方法主要使用Thread.currentThread().getStackTrace();获取到调用栈信息,然后从调用栈信息中获取指定层级的调用栈元素作为调用位置信息。

    个人做了一个简单测试,将Utils.getCallLocationName()的关键代码拷贝出来执行:

    可以看到,这段代码运行在main方法中,它的调用栈信息是代码21行日志打印的结果,代码22行打印是它真正的调用位置。  

  • 相关阅读:
    第九站:Java黑——安全编码的坚固防线
    vue基础知识和原理(一)
    Docker安装Seata
    智能照明控制系统某大楼大厅照明的应用
    Ubuntu server 24 (Linux) 普通用户不能sudo 也不能使用root登录 忘记root密码 修复解决方案
    成绩分析(蓝桥杯)
    数据结构题目收录(十九)
    Selenium基础 — POM设计模式(一)
    无头单向非循环链表(C语言实现)
    SOLIDWORKS 2024新功能--Electrical篇
  • 原文地址:https://blog.csdn.net/u011250186/article/details/136269568