Add docs.nats.io examples to main - #1600
Conversation
|
Claude finished @Jarema's task in 3m 14s —— View job PR Review
The 52 examples are well-structured and consistent. All connections are properly closed via try-with-resources, JetStream Three issues worth addressing: 1. Missing null-check → NPE (medium) — 2. 3. Silent Two patterns that are fine but worth being aware of:
|
| System.out.println("Messages remaining in FULFILLMENT: " + count); | ||
| // NATS-DOC-END | ||
| } | ||
| catch (Exception e) { |
There was a problem hiding this comment.
Missing null-check before using task. fc.nextMessage() returns null when the fetch times out with no messages delivered. Every other nextMessage() call in this PR checks for null first — e.g. LearnJetStreamYourFirstConsumerPullAndAck.java:16. If there's a transient delay between the publish and the fetch (e.g. a slow CI worker), this will throw NullPointerException on task.getData().
| catch (Exception e) { | |
| try (FetchConsumer fc = shippers.fetchMessages(1)) { | |
| Message task = fc.nextMessage(); | |
| if (task == null) { | |
| System.out.println("No task available."); | |
| return; | |
| } | |
| System.out.println("Shipping: " + new String(task.getData(), StandardCharsets.UTF_8)); | |
| task.ack(); | |
| } |
| } | ||
| else { | ||
| System.out.println("request failed: " + e.getMessage()); | ||
| } |
There was a problem hiding this comment.
JetStreamStatusException is a JetStream-specific type but this is a Core NATS request. The "no responders" 503 status from reportNoResponders() is a Core NATS protocol feature. Please verify that the library does in fact complete the future with a JetStreamStatusException for this case (as opposed to a StatusException or similar base class). If readers copy this import and class name into a pure Core NATS project they may be confused by the JetStream dependency.
| } | ||
|
|
||
| try (FetchConsumer fc = cc.fetch( | ||
| FetchConsumeOptions.builder().maxMessages((int) pending).build())) { |
There was a problem hiding this comment.
Silent narrowing cast: getNumPending() returns long but maxMessages() takes int. Values above Integer.MAX_VALUE (~2.1B messages) wrap silently to a negative number, which would cause the fetch to fail. For a docs example this is extremely unlikely in practice, but since the code reads like it calculates a precise count it's worth clamping:
| FetchConsumeOptions.builder().maxMessages((int) pending).build())) { | |
| FetchConsumeOptions.builder().maxMessages((int) Math.min(pending, Integer.MAX_VALUE)).build())) { |
1c23fb0 to
b9deef3
Compare
Consolidates the core-docs and jetstream-docs branches' examples (examples/src/main/java/io/nats/examples/natsIoDoc/) onto main so the documentation examples are compiled by CI (:examples:compileJava) instead of living on unprotected side branches. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Signed-off-by: Tomasz Pietrek <tomasz@nats.io> Signed-off-by: Tomasz Pietrek <tomasz@synadia.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Signed-off-by: Tomasz Pietrek <tomasz@nats.io> Signed-off-by: Tomasz Pietrek <tomasz@synadia.com>
b9deef3 to
8cb8a91
Compare
Moves the documentation examples from the
core-docsandjetstream-docsbranches intoexamples/src/main/java/io/nats/examples/natsIoDoc/on main (52 newLearn*.javafiles joining the 19 already here; additive only).Why: the new docs.nats.io build fetches these snippets at build time. On side branches they get zero CI (no workflow triggers on them); on main the
examplessubproject'scompileJavacovers them on every PR/push automatically — no gradle or workflow changes needed.Note: please keep the docs branches until the docs repo's fetch config is switched to
main(follow-up PR there).🤖 Generated with Claude Code