You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@pekko.apache.org by "He-Pin (via GitHub)" <gi...@apache.org> on 2026/06/22 20:31:39 UTC
[I] Modernize codebase with JDK 9-17 APIs [pekko]
He-Pin opened a new issue, #3136:
URL: https://github.com/apache/pekko/issues/3136
### Motivation
Apache Pekko 2.0 raised the minimum JDK version to 17. However, many parts of the codebase still use pre-JDK 9 patterns that can now be replaced with cleaner, safer, and sometimes more performant JDK 9-17 APIs. This meta-issue tracks the identification and migration of these patterns.
### Scope
Each migration should be a separate PR for reviewability and bisectability.
---
#### 1. Replace manual `CompletableFuture` timeout with `orTimeout()` / `completeOnTimeout()` (JDK 9)
`FutureTimeoutSupport.timeoutCompletionStage` is already `@deprecated("Use CompletableFuture#orTimeout instead.", "2.0.0")` but still used internally by `Patterns.timeout()`. Rewrite the implementation to use `CompletableFuture.orTimeout()` directly, eliminating the `Scheduler`-based manual timeout.
**Files:** `actor/src/main/scala/org/apache/pekko/pattern/FutureTimeoutSupport.scala`, `actor/src/main/scala/org/apache/pekko/pattern/Patterns.scala`
#### 2. Replace `Collections.emptyList/emptySet/unmodifiableSet` with `List.of/Set.of` in production code (JDK 9)
Several production Scala files still use `java.util.Collections.emptyList()` / `emptySet()` / `unmodifiableSet(emptySet())`. Replace with `java.util.List.of()` / `Set.of()` for cleaner semantics and fewer allocations.
**Files:**
- `actor/src/main/scala/org/apache/pekko/dispatch/AbstractDispatcher.scala` (line 580)
- `actor/src/main/scala/org/apache/pekko/dispatch/affinity/AffinityPool.scala` (line 241)
- `actor-typed/src/main/scala/org/apache/pekko/actor/typed/javadsl/Behaviors.scala` (line 363)
- `actor-testkit-typed/src/main/scala/org/apache/pekko/actor/testkit/typed/internal/TestAppender.scala` (line 99)
- `persistence-typed/src/main/scala/org/apache/pekko/persistence/typed/javadsl/EventSourcedBehavior.scala` (line 186)
- `stream/src/main/scala/org/apache/pekko/stream/Attributes.scala` (line 281)
- `stream/src/main/scala/org/apache/pekko/stream/javadsl/StatefulMapConcatAccumulator.scala` (line 38)
- `stream/src/main/scala/org/apache/pekko/stream/impl/fusing/StreamOfStreams.scala` (line 402)
#### 3. Use ByteBuffer fluent API chains with JDK 9 covariant returns (JDK 9)
JDK 9 changed `ByteBuffer.clear()`, `position(int)`, `limit(int)`, `flip()`, `rewind()`, `mark()`, `reset()` to return `ByteBuffer` instead of `Buffer`. This enables fluent chaining. Many separate-statement patterns can be consolidated.
**Files:**
- `actor/src/main/scala/org/apache/pekko/io/TcpConnection.scala` (lines 267-274, 451-455, 481-484)
- `actor/src/main/scala/org/apache/pekko/io/UdpConnection.scala` (lines 132-138, 156-159)
- `actor/src/main/scala/org/apache/pekko/io/UdpListener.scala` (lines 102-110)
- `actor/src/main/scala/org/apache/pekko/io/WithUdpSend.scala` (lines 86-92)
- `stream/src/main/scala/org/apache/pekko/stream/impl/io/TlsEngineHelpers.scala` (lines 30-34)
- `stream/src/main/scala/org/apache/pekko/stream/impl/io/IOSources.scala` (lines 108-123)
- `stream/src/main/scala/org/apache/pekko/stream/impl/io/TLSActor.scala` (multiple locations)
- `stream/src/main/scala/org/apache/pekko/stream/impl/io/TlsGraphStage.scala` (multiple locations)
#### 4. Optimize EnvelopeBufferPool.copy() with ByteBuffer duplicate+slice (JDK 9)
`EnvelopeBufferPool.copy()` currently allocates an intermediate `Array[Byte]`, does `rewind()` + `get(bytes)` + manual position save/restore. Using `byteBuffer.duplicate()` + JDK 9 covariant returns + `slice()` eliminates the intermediate allocation.
**Files:** `remote/src/main/scala/org/apache/pekko/remote/artery/EnvelopeBufferPool.scala` (lines 571-580)
#### 5. Update Scaladoc examples from `Arrays.asList` to `List.of` (JDK 9)
Scaladoc examples in the Stream Java DSL use `Arrays.asList(...)` which creates a mutable list. Update to `List.of(...)` for immutable semantics consistent with modern Java style.
**Files:**
- `stream/src/main/scala/org/apache/pekko/stream/javadsl/Source.scala` (lines 131, 1558, 3548, 3581)
- `stream/src/main/scala/org/apache/pekko/stream/javadsl/Flow.scala` (lines 1624, 1658, 3434-3435)
- `stream/src/main/scala/org/apache/pekko/stream/javadsl/SubSource.scala` (lines 1002, 1036, 2381)
- `stream/src/main/scala/org/apache/pekko/stream/javadsl/SubFlow.scala` (lines 1016, 1050)
#### 6. Use StackWalker for stack trace inspection (JDK 9)
`TestKitUtils.testNameFromCallStack` uses `Thread.currentThread.getStackTrace` which eagerly constructs the full stack trace array. `StackWalker` lazily walks the stack and is more efficient.
**Files:** `testkit/src/main/scala/org/apache/pekko/testkit/TestKitUtils.scala` (line 39)
### Out of scope
- **Protobuf generated files** (`*Messages.java`, `*Formats.java`, `ReplicatedEventSourcing.java`): auto-generated, not hand-edited
- **`OptionalUtil.java`**: uses `OptionalLong.isPresent()` + `getAsLong()` correctly for Scala `Option` interop
- **`PekkoException.scala`**: copies cause stack trace between Throwables, `StackWalker` is not applicable
- **`ByteBufferCleaner.java`**: `Unsafe.invokeCleaner` is the standard way to clean direct ByteBuffers
- **`HexFormat` (JDK 17)**: only one minor error-message use, not worth changing
- **`Charset.forName`**: already fully migrated to `StandardCharsets` throughout
### References
None - proactive modernization.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org
Re: [I] Modernize codebase with JDK 9-17 APIs? [pekko]
Posted by "pjfanning (via GitHub)" <gi...@apache.org>.
pjfanning closed issue #3136: Modernize codebase with JDK 9-17 APIs?
URL: https://github.com/apache/pekko/issues/3136
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org
Re: [I] Modernize codebase with JDK 9-17 APIs [pekko]
Posted by "pjfanning (via GitHub)" <gi...@apache.org>.
pjfanning commented on issue #3136:
URL: https://github.com/apache/pekko/issues/3136#issuecomment-4773283889
@He-Pin I don't understand why we need to get rid of ju.Collections.emptyList(). This is a singleton list (one static instance per JVM) - so no allocations.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org