fix: preload all protocol classes to avoid ClassNotFoundException in encode path#25
Open
LXingYun wants to merge 1 commit into
Open
fix: preload all protocol classes to avoid ClassNotFoundException in encode path#25LXingYun wants to merge 1 commit into
LXingYun wants to merge 1 commit into
Conversation
…encode path ProtocolClassLoader lazy loads inner classes which causes ClassNotFoundException when encoding commands (e.g. TopicMessageCodec.doEncode calling TopicPayload.of(...)). This static block scans all .class entries in the JAR and calls Class.forName() to preload them into the ProtocolClassLoader cache. Fixes: jetlinks/jetlinks-supports#38
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Fixes lazy loading bug in
ProtocolClassLoaderthat causesClassNotFoundExceptionwhen encoding commands (e.g.TopicMessageCodec.doEncodecalling
TopicPayload.of(...)).Fixes: jetlinks/jetlinks-supports#38
Approach
Add a
static {}initializer block toJetLinksProtocolSupportProviderthat walks all
.classentries within the running JAR and callsClass.forName(name, false, classLoader)on eachorg/jetlinks/protocol/official/*entry. This preloads all classes into the
ProtocolClassLoadercache,so subsequent runtime references hit the cache instead of triggering
the broken findClass path.
Trade-offs
Test results
TopicMessageCodec.encodefails withNoClassDefFoundError100% on first command dispatchAlternative considered
Fix at
ProtocolClassLoader(jetlinks-supports). Likely a better long-termfix but requires changes to multiple package boundaries. Submitting this
patch as a quick win; willing to follow up with a deeper fix in
jetlinks-supports if maintainers prefer.
Co-author
This patch was developed at SBOMP,
a smart building operations platform built on JetLinks.