Add Quarkus/GraalVM Native Image support - Fix Random instance initialization issues #3733
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
问题描述 / Problem Description
当使用 Quarkus 3.3 构建 Native Image 时,编译失败并报错:
这是因为 GraalVM Native Image 不允许在构建时(image heap)创建 Random/SecureRandom 实例,因为这些实例包含缓存的种子值,在运行时无法正常工作。
解决方案 / Solution
1. 修复 Random 实例初始化方式
将所有静态 Random 实例改为延迟初始化,使用双重检查锁定模式(Double-Checked Locking):
static final Random
改为延迟初始化示例代码:
2. 添加 GraalVM Native Image 配置
在
weixin-java-common
模块中添加了 Native Image 配置文件:META-INF/native-image/com.github.binarywang/weixin-java-common/native-image.properties:
这告诉 GraalVM 在运行时而非构建时初始化 Apache HttpClient 的认证相关类,避免 SecureRandom 实例出现在 image heap 中。
META-INF/native-image/com.github.binarywang/weixin-java-common/reflect-config.json:
配置了必要的反射访问权限。
3. 完善文档
QUARKUS_SUPPORT.md
文档,详细说明 Quarkus/GraalVM 支持的使用方法README.md
,在重要信息中添加 Quarkus 支持说明兼容性 / Compatibility
测试验证 / Testing
mvn clean compile
mvn clean package -DskipTests=true
mvn checkstyle:check
使用方式 / Usage
在 Quarkus 项目中正常引入依赖即可:
构建 Native Image:
详细文档请参考:QUARKUS_SUPPORT.md
Fixes #3353
Original prompt
This section details on the original issue you should resolve
<issue_title>啥时候可以和quarkus 框架匹配下啊</issue_title>
<issue_description># 提问前,请确保阅读过项目首页说明以及wiki开发文档相关内容,完成内容后,请务必移除包括本句在内的无用内容,以免影响他人阅读,否则直接关闭,谢谢合作~
简要描述
我用quarkus3.3 + weixin-java-miniapp 4.7.8.B 编译本地代码时候报
Error: Unsupported features in 3 methods
Detailed message:
Error: Detected an instance of Random/SplittableRandom class in the image heap. Instances created during image generation have cached seed values and don't behave as expected. Try avoiding to initialize the class that caused initialization of the object.
The culprit object has been instantiated by the 'org.apache.http.impl.auth.NTLMEngineImpl' class initializer with the following trace:
at java.security.SecureRandom.(SecureRandom.java:331)
at java.security.SecureRandom.getInstance(SecureRandom.java:391)
at org.apache.http.impl.auth.NTLMEngineImpl.(NTLMEngineImpl.java:103)
The object was probably created by a class initializer and is reachable from a static field. You can request class initialization at image runtime by using the option --initialize-at-run-time=. Or you can write your own initialization methods and call them explicitly from your main entry point.
Trace: Object was reached by
trying to constant fold static field org.apache.http.impl.auth.NTLMEngineImpl.RND_GEN
at org.apache.http.impl.auth.NTLMEngineImpl.access$000(NTLMEngineImpl.java:51)
parsing method org.apache.http.impl.auth.NTLMEngineImpl.access$000(NTLMEngineImpl.java:51) reachable via the parsing context
at static root method.(Unknown Source)
Error: Discovered unresolved type during parsing: jodd.http.HttpConnectionProvider. This error is reported at image build time because class cn.binarywang.wx.miniapp.executor.JoddApiSignaturePostRequestExecutor is registered for linking at image build time by command line and command line.
Error encountered while parsing cn.binarywang.wx.miniapp.executor.JoddApiSignaturePostRequestExecutor.execute(JoddApiSignaturePostRequestExecutor.java:36)
Parsing context:
at cn.binarywang.wx.miniapp.api.impl.BaseWxMaServiceImpl.lambda$execute$1(BaseWxMaServiceImpl.java:373)
at cn.binarywang.wx.miniapp.api.impl.BaseWxMaServiceImpl$$Lambda/0x0000000805bd0ce0.execute(Unknown Source)
at cn.binarywang.wx.miniapp.api.impl.BaseWxMaServiceImpl.executeInternal(BaseWxMaServiceImpl.java:435)
at cn.binarywang.wx.miniapp.api.impl.BaseWxMaServiceImpl.executeWithRetry(BaseWxMaServiceImpl.java:387)
at cn.binarywang.wx.miniapp.api.impl.BaseWxMaServiceImpl.execute(BaseWxMaServiceImpl.java:358)
at cn.binarywang.wx.miniapp.api.impl.BaseWxMaServiceImpl.post(BaseWxMaServiceImpl.java:313)
at cn.binarywang.wx.miniapp.api.impl.WxMaOcrServiceImpl.idCard(WxMaOcrServiceImpl.java:36)
at com.cxp.application.ocr.strategy.IdCardStrategy.ocr(IdCardStrategy.java:23)
at com.cxp.api.IndexApi.OCR(IndexApi.java:140)
at root method.(Unknown Source)
Error: Discovered unresolved type during parsing: jodd.http.HttpConnectionProvider. This error is reported at image build time because class me.chanjar.weixin.common.util.http.jodd.JoddHttpSimplePostRequestExecutor is registered for linking at image build time by command line and command line.
Error encountered while parsing me.chanjar.weixin.common.util.http.jodd.JoddHttpSimplePostRequestExecutor.execute(JoddHttpSimplePostRequestExecutor.java:28)
Parsing context:
at me.chanjar.weixin.common.util.http.jodd.JoddHttpSimplePostRequestExecutor.execute(JoddHttpSimplePostRequestExecutor.java:21)
at cn.binarywang.wx.miniapp.api.impl.BaseWxMaServiceImpl.lambda$execute$0(BaseWxMaServiceImpl.java:359)
at cn.binarywang.wx.miniapp.api.impl.BaseWxMaServiceImpl$$Lambda/0x0000000805b29d08.execute(Unknown Source)
at cn.binarywang.wx.miniapp.api.impl.BaseWxMaServiceImpl.executeInternal(BaseWxMaServiceImpl.java:435)
at cn.binarywang.wx.miniapp.api.impl.BaseWxMaServiceImpl.executeWithRetry(BaseWxMaServiceImpl.java:387)
at cn.binarywang.wx.miniapp.api.impl.BaseWxMaServiceImpl.execute(BaseWxMaServiceImpl.java:358)
at cn.binarywang.wx.miniapp.api.impl.BaseWxMaServiceImpl.post(BaseWxMaServiceImpl.java:313)
at cn.binarywang.wx.miniapp.api.impl.WxMaOcrServiceImpl.idCard(WxMaOcrServiceImpl.java:36)
at com.cxp.application.ocr.strategy.IdCardStrategy.ocr(IdCardStrategy.java:23)
at com.cxp.api.IndexApi.OCR(IndexApi.java:140)
at root method.(Unknown Source)
官方文档地址
请提供所需功能对应的微信官方文档地址以便进行确认。
</issue_description>
Comments on the Issue (you are @copilot in this section)
@binarywang 欢迎楼主直接提交PR代码来完善支持Fixes #3732
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.