Netty- 无法访问类 jdk.internal.misc.Unsafe

2022-09-01 00:06:16

当我将Java从8升级到11时,我从Netty那里得到了一个关于的错误,详细信息如下:"jdk.internal.misc.Unsafe"

我知道这是一个调试级别的消息,我可以更改日志级别以忽略它。但我不确定当我忽略它时,是否会有其他问题 - 例如性能。有谁知道最好的解决方案吗?

java.lang.IllegalAccessException: class io.netty.util.internal.PlatformDependent0$6 cannot access class jdk.internal.misc.Unsafe (in module java.base) because module java.base does not export jdk.internal.misc to unnamed module @84b8f0f
    at jdk.internal.reflect.Reflection.newIllegalAccessException(Reflection.java:361) ~[?:?]
    at java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:591) ~[?:?]
    at java.lang.reflect.Method.invoke(Method.java:558) ~[?:?]
    at io.netty.util.internal.PlatformDependent0$6.run(PlatformDependent0.java:334) ~[netty-all-4.1.36.Final.jar:4.1.36.Final]
    at java.security.AccessController.doPrivileged(Native Method) ~[?:?]
    at io.netty.util.internal.PlatformDependent0.<clinit>(PlatformDependent0.java:325) ~[netty-all-4.1.36.Final.jar:4.1.36.Final]
    at io.netty.util.internal.PlatformDependent.isAndroid(PlatformDependent.java:214) ~[netty-all-4.1.36.Final.jar:4.1.36.Final]
    at io.netty.util.internal.PlatformDependent.<clinit>(PlatformDependent.java:82) ~[netty-all-4.1.36.Final.jar:4.1.36.Final]
    at io.netty.buffer.UnpooledByteBufAllocator.<clinit>(UnpooledByteBufAllocator.java:37) ~[netty-all-4.1.36.Final.jar:4.1.36.Final]
    at io.netty.buffer.Unpooled.<clinit>(Unpooled.java:73) ~[netty-all-4.1.36.Final.jar:4.1.36.Final]
    at wedo.stream3.framework.base.connector.supplier.DelimiterDecoderSupplier.getDelimiter(DelimiterDecoderSupplier.java:41) ~[classes/:?]
    at wedo.stream3.framework.base.connector.supplier.DelimiterDecoderSupplier.<init>(DelimiterDecoderSupplier.java:26) ~[classes/:?]
    at wedo.stream3.framework.base.connector.supplier.DelimiterDecoderSupplier.<init>(DelimiterDecoderSupplier.java:20) ~[classes/:?]
    at wedo.stream3.framework.base.connector.supplier.CommonChannelHandlerSupplier.<init>(CommonChannelHandlerSupplier.java:37) ~[classes/:?]
    at wedo.stream3.framework.base.connector.supplier.CommonChannelHandlerSupplier.<init>(CommonChannelHandlerSupplier.java:25) ~[classes/:?]
    at wedo.stream3.framework.base.connector.TcpClientConnector.start(TcpClientConnector.java:39) ~[classes/:?]
    at wedo.stream3.framework.bootstrap.FrameworkLauncher.lambda$start$0(FrameworkLauncher.java:61) ~[classes/:?]
    at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183) [?:?]
    at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:177) [?:?]
    at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1654) [?:?]
    at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484) [?:?]
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474) [?:?]
    at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150) [?:?]
    at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173) [?:?]
    at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) [?:?]
    at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:497) [?:?]
    at wedo.stream3.framework.bootstrap.FrameworkLauncher.start(FrameworkLauncher.java:58) [classes/:?]
    at org.stream3.prototype.mfc.App.launchFramework(App.java:58) [classes/:?]
    at org.stream3.prototype.mfc.App.main(App.java:41) [classes/:?]

答案 1

要允许 netty 访问该类,请使用以下选项启动 java:

--add-opens java.base/jdk.internal.misc=ALL-UNNAMED

这会将模块中的包打开到无铰接模块。jdk.internal.miscjava.base

另请参阅 java 命令的文档,以及 Java 模块系统的一般介绍。

编辑:对于Netty使用其直接缓冲区优化,您还需要设置

-Dio.netty.tryReflectionSetAccessible=true

关于这个主题有许多Netty问题,例如参见netty/issues/7769


答案 2

在 Java 11 上运行 Netty 时,请添加以下 VM 选项以启用性能优化:

--add-opens java.base/jdk.internal.misc=ALL-UNNAMED
-Dio.netty.tryReflectionSetAccessible=true

然后,要知道何时使用深度反射,您可以添加:

--illegal-access=warn

推荐