在流启动之前访问Flink类加载器

8
在我的项目中,我希望在流执行之前访问Flink用户类加载器。在流执行之前,我一直在实例化自己的类加载器以反序列化类(尽力避免与多个类加载器相关的问题)。
然而,随着我进一步开发,我不得不编写更多的(糟糕的)代码来避免这个问题。
如果我可以访问Flink用户类加载器并使用它,这个问题就可以解决了,但是我没有看到除了 "RichFunctions" 之外的机制可以做到这一点(https://ci.apache.org/projects/flink/flink-docs-stable/api/java/org/apache/flink/api/common/functions/RichFunction.html),而这需要流正在运行。
任何指导都将不胜感激。

你想在哪里访问用户类加载器?在主方法中还是在你定义的用户函数中? - Robert Metzger
1个回答

1
你可以在flink中使用自己的类加载器。
构建图形并将类加载器与客户端一起提交。
代码如下:
final StandaloneClusterClient client;
try {
    client = new StandaloneClusterClient(configuration);
} catch (final Exception e) {
    throw new RuntimeException("Could not establish a connection to the job manager", e);
}

try {
    ClassLoader classLoader = JobWithJars.buildUserCodeClassLoader(
            Collections.<URL>singletonList(uploadedJarUrl),
            Collections.<URL>emptyList(),
            this.getClass().getClassLoader());
    client.runDetached(jobGraph, classLoader);
} catch (final ProgramInvocationException e) {
    throw new RuntimeException("Cannot execute job due to ProgramInvocationException", e);
}

但我仍然想知道为什么你想要自己的类加载器,可能可以通过其他方式实现。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接