-
Notifications
You must be signed in to change notification settings - Fork 3
Add docs.nats.io examples to main #49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| // Copyright (c) 2025 Synadia Communications Inc. All Rights Reserved. | ||
| // See LICENSE and NOTICE file for details. | ||
|
|
||
| package io.synadia.examples; | ||
|
|
||
| import io.nats.client.Connection; | ||
| import io.nats.client.JetStreamApiException; | ||
| import io.nats.client.JetStreamManagement; | ||
| import io.nats.client.Nats; | ||
| import io.nats.client.api.PublishAck; | ||
| import io.nats.client.api.StreamConfiguration; | ||
| import io.synadia.bp.BatchPublisher; | ||
|
|
||
| public class AtomicBatchDocExample { | ||
|
Check warning on line 14 in batch-publish/src/examples/java/io/synadia/examples/AtomicBatchDocExample.java
|
||
| static final String NATS_URL = "nats://localhost:4222"; | ||
| static final String STREAM = "ORDERS"; | ||
| static final String SUBJECTS = "orders.>"; | ||
| static final String SUBJECT = "orders.created"; | ||
| static final String BATCH_ID = "order-4273"; | ||
|
|
||
| public static void main(String[] args) throws Exception { | ||
| try (Connection nc = Nats.connect(NATS_URL)) { | ||
| JetStreamManagement jsm = nc.jetStreamManagement(); | ||
|
|
||
| // Ensure an ORDERS stream exists with atomic batch publishing enabled. | ||
| try { jsm.deleteStream(STREAM); } catch (JetStreamApiException ignore) {} | ||
| StreamConfiguration config = StreamConfiguration.builder() | ||
| .name(STREAM) | ||
| .subjects(SUBJECTS) | ||
| .allowAtomicPublish() | ||
| .build(); | ||
| jsm.addStream(config); | ||
|
|
||
| // NATS-DOC-START | ||
| // One order, three line items, stored as a single atomic batch: | ||
| // either all three messages land in the stream, or none do. | ||
| BatchPublisher publisher = BatchPublisher.builder() | ||
| .connection(nc) | ||
| .batchId(BATCH_ID) | ||
| .build(); | ||
|
|
||
| publisher.add(SUBJECT, "{\"sku\":\"NATS-TEE\",\"qty\":2}".getBytes()); | ||
| publisher.add(SUBJECT, "{\"sku\":\"NATS-MUG\",\"qty\":1}".getBytes()); | ||
| PublishAck ack = publisher.commit(SUBJECT, "{\"sku\":\"NATS-CAP\",\"qty\":1}".getBytes()); | ||
|
|
||
| System.out.println("Committed batch [" + publisher.getBatchId() + "]" | ||
| + " of " + ack.getBatchSize() + " line items" | ||
| + " at stream sequence " + ack.getSeqno() + "."); | ||
| // NATS-DOC-END | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,55 @@ | ||||||||
| // Copyright (c) 2025 Synadia Communications Inc. All Rights Reserved. | ||||||||
| // See LICENSE and NOTICE file for details. | ||||||||
|
|
||||||||
| package io.synadia.examples; | ||||||||
|
|
||||||||
| import io.nats.client.Connection; | ||||||||
| import io.nats.client.JetStream; | ||||||||
| import io.nats.client.JetStreamApiException; | ||||||||
| import io.nats.client.JetStreamManagement; | ||||||||
| import io.nats.client.Nats; | ||||||||
| import io.nats.client.api.MessageInfo; | ||||||||
| import io.nats.client.api.StreamConfiguration; | ||||||||
| import io.synadia.direct.DirectBatchContext; | ||||||||
| import io.synadia.direct.MessageBatchGetRequest; | ||||||||
|
|
||||||||
| import java.util.List; | ||||||||
|
|
||||||||
| public class LearnJetStreamGetDirectBatchGet { | ||||||||
| static final String NATS_URL = System.getenv("NATS_URL") != null | ||||||||
| ? System.getenv("NATS_URL") : "nats://localhost:4222"; | ||||||||
|
Comment on lines
+19
to
+20
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
or a static initializer with a single |
||||||||
| static final String STREAM = "ORDERS"; | ||||||||
| static final String SUBJECTS = "orders.>"; | ||||||||
|
|
||||||||
| public static void main(String[] args) throws Exception { | ||||||||
| try (Connection nc = Nats.connect(NATS_URL)) { | ||||||||
| JetStreamManagement jsm = nc.jetStreamManagement(); | ||||||||
| JetStream js = nc.jetStream(); | ||||||||
|
|
||||||||
| // Ensure an ORDERS stream exists with direct access enabled, | ||||||||
| // then seed a few orders so the batch get has something to read. | ||||||||
| try { jsm.deleteStream(STREAM); } catch (JetStreamApiException ignore) {} | ||||||||
| jsm.addStream(StreamConfiguration.builder() | ||||||||
| .name(STREAM) | ||||||||
| .subjects(SUBJECTS) | ||||||||
| .allowDirect(true) | ||||||||
| .build()); | ||||||||
|
|
||||||||
| js.publish("orders.created", "{\"id\":\"order-1\"}".getBytes()); | ||||||||
| js.publish("orders.created", "{\"id\":\"order-2\"}".getBytes()); | ||||||||
| js.publish("orders.created", "{\"id\":\"order-3\"}".getBytes()); | ||||||||
|
|
||||||||
| // NATS-DOC-START | ||||||||
| // Batch Direct Get: in one request, read up to 3 messages from the | ||||||||
| // ORDERS stream starting at stream sequence 1, then iterate in order. | ||||||||
| DirectBatchContext direct = new DirectBatchContext(nc, STREAM); | ||||||||
| MessageBatchGetRequest request = MessageBatchGetRequest.batch(">", 3, 1); | ||||||||
|
|
||||||||
| List<MessageInfo> messages = direct.fetchMessageBatch(request); | ||||||||
| for (MessageInfo mi : messages) { | ||||||||
| System.out.println("sequence " + mi.getSeq() + " | subject " + mi.getSubject()); | ||||||||
| } | ||||||||
| // NATS-DOC-END | ||||||||
| } | ||||||||
| } | ||||||||
| } | ||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ack.getBatchSize()is the only place this method is called in the entire examples codebase — existing examples usepa.getBatchId()andpa.getJv()but notgetBatchSize(). Please confirmgetBatchSize()exists onio.nats.client.api.PublishAckin jnats 2.25.1. If it's missing, the build will fail silently on this branch until CI runs thebp-*workflow. The rest of the line (getSeqno(),getBatchId()) are verified against other examples.