Skip to content

[ISSUE #1318] [Java] Support opt-in virtual threads - #1319

Merged
lizhimins merged 7 commits into
apache:masterfrom
qianye1001:codex/java-virtual-threads
Aug 6, 2026
Merged

lizhimins merged 7 commits into
apache:masterfrom
qianye1001:codex/java-virtual-threads

Conversation

@qianye1001

Copy link
Copy Markdown
Contributor

Which Issue(s) This PR Fixes

Fixes #1318

Brief Description

This change adds an opt-in enableVirtualThreads(boolean) client configuration. It is disabled by default.

When enabled on JDK 21 or later, the Java client uses virtual-thread-per-task executors for asynchronous RPC work, client callbacks, telemetry commands, and push-consumer message processing. The JDK 21 executor API is discovered through reflection so the project keeps its existing Java 8 compilation target and remains runnable on older JDKs. If virtual threads are unavailable, the client falls back to the existing platform-thread executors.

Push-consumer execution uses a fair Semaphore so setConsumptionThreadCount remains the maximum number of concurrently running consumption tasks. Waiting virtual threads park without occupying carrier threads. Scheduled executors remain platform-thread based.

How Did You Test This Change?

  • JDK 21 targeted tests:
    • mvn -pl client -am -Dspotbugs.skip=true -Djacoco.skip=true -Dtest=ClientConfigurationTest,ExecutorServicesTest -DfailIfNoTests=false -Dsurefire.failIfNoSpecifiedTests=false test
    • 5 tests passed.
  • JDK 11 clean full client test suite:
    • mvn -pl client -am clean -Dspotbugs.skip=true test
    • 296 tests run, 0 failures, 0 errors, 1 skipped.
  • Verified ClientConfiguration.class and ExecutorServices.class both have class-file major version 52.
  • Built the shaded client on a remote Alibaba AJDK 21 environment and ran a real producer/consumer test:
    • 64 asynchronous sends succeeded;
    • send callbacks and message listeners ran on virtual threads;
    • with setConsumptionThreadCount(1), the maximum observed concurrent listeners was exactly 1.

@codecov-commenter

codecov-commenter commented Aug 6, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 66.17647% with 23 lines in your changes missing coverage. Please review.
βœ… Project coverage is 54.59%. Comparing base (4076e2b) to head (91a84c6).
⚠️ Report is 157 commits behind head on master.

Files with missing lines Patch % Lines
...he/rocketmq/client/java/misc/ExecutorServices.java 58.49% 17 Missing and 5 partials ⚠️
...a/org/apache/rocketmq/client/java/impl/Client.java 0.00% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #1319      +/-   ##
============================================
+ Coverage     53.26%   54.59%   +1.32%     
- Complexity      651      822     +171     
============================================
  Files           208      222      +14     
  Lines         14303    14501     +198     
  Branches       5845     5600     -245     
============================================
+ Hits           7619     7917     +298     
+ Misses         6308     6175     -133     
- Partials        376      409      +33     
Flag Coverage Ξ”
java 62.80% <66.17%> (+1.11%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

β˜” View full report in Codecov by Harness.
πŸ“’ Have feedback on the report? Share it here.

πŸš€ New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • πŸ“¦ JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@qianye1001
qianye1001 marked this pull request as ready for review August 6, 2026 09:31

@RockteMQ-AI RockteMQ-AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary

This PR adds opt-in virtual thread support for the Java client (JDK 21+), using reflection to maintain Java 8 compilation compatibility. When enabled via enableVirtualThreads(true), async RPCs, callbacks, telemetry commands, and push-consumer message processing run on virtual threads. A fair Semaphore-based wrapper ensures setConsumptionThreadCount still bounds concurrency. Falls back gracefully to platform threads on older JDKs.

The design is clean: reflection-based discovery of Executors.newVirtualThreadPerTaskExecutor(), proper abstraction widening from ThreadPoolExecutor to ExecutorService, and comprehensive tests covering both JDK 21 and older environments. CI matrix is extended to include JDK 21. All checks pass.

LGTM β€” two minor informational notes inline.


Automated review by github-manager-bot

@RockteMQ-AI RockteMQ-AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary

This PR contains 726 lines of changes. Automated structural review completed.

Review Notes

  • Changes reviewed for structural correctness
  • No critical issues detected in automated scan
  • Recommend maintainer review for domain-specific logic

Automated review by github-manager-bot

@lizhimins
lizhimins merged commit 9abb7f3 into apache:master Aug 6, 2026
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support opt-in virtual threads in the Java client

4 participants