This is an automated email from the ASF dual-hosted git repository.

spetz pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/iggy-website.git


The following commit(s) were added to refs/heads/main by this push:
     new 8369e30f fix(docs): make Java SDK examples re-runnable and report the 
right offset (closes #56) (#57)
8369e30f is described below

commit 8369e30f3ff51b166aeed908e4860d85abab9600
Author: Justin Mclean <[email protected]>
AuthorDate: Wed Aug 5 17:47:24 2026 +1000

    fix(docs): make Java SDK examples re-runnable and report the right offset 
(closes #56) (#57)
---
 content/docs/sdk/java/examples.mdx | 90 ++++++++++++++++++++++++--------------
 content/docs/sdk/java/intro.mdx    | 35 ++++++++-------
 2 files changed, 78 insertions(+), 47 deletions(-)

diff --git a/content/docs/sdk/java/examples.mdx 
b/content/docs/sdk/java/examples.mdx
index 26469164..dcefe2d1 100644
--- a/content/docs/sdk/java/examples.mdx
+++ b/content/docs/sdk/java/examples.mdx
@@ -27,28 +27,44 @@ import java.util.List;
 import static java.util.Optional.empty;
 
 public class Producer {
+
+    static final String STREAM_NAME = "sample-stream";
+    static final StreamId STREAM_ID = StreamId.of(STREAM_NAME);
+    static final String TOPIC_NAME = "sample-topic";
+    static final TopicId TOPIC_ID = TopicId.of(TOPIC_NAME);
+
     public static void main(String[] args) {
         try (var client = IggyTcpClient.builder()
-                .host("localhost")
+                .host("127.0.0.1")
                 .port(8090)
                 .credentials("iggy", "iggy")
                 .buildAndLogin()) {
 
-            var streamId = StreamId.of("sample-stream");
-            var topicId = TopicId.of("sample-topic");
-
-            client.streams().createStream("sample-stream");
-            client.topics().createTopic(
-                    streamId, 1L,
-                    CompressionAlgorithm.None,
-                    BigInteger.ZERO, BigInteger.ZERO,
-                    empty(), "sample-topic");
-
-            var partitioning = Partitioning.partitionId(0L);
-            var messages = List.of(Message.of("hello world"));
-            client.messages().sendMessages(
-                    streamId, topicId,
-                    partitioning, messages);
+            // Re-running this example is fine: only create what is missing.
+            if (client.streams().getStream(STREAM_ID).isEmpty()) {
+                client.streams().createStream(STREAM_NAME);
+            }
+            if (client.topics().getTopic(STREAM_ID, TOPIC_ID).isEmpty()) {
+                client.topics().createTopic(
+                        STREAM_ID,
+                        1L,
+                        CompressionAlgorithm.None,
+                        BigInteger.ZERO,
+                        BigInteger.ZERO,
+                        empty(),
+                        TOPIC_NAME);
+            }
+
+            Partitioning partitioning = Partitioning.partitionId(0L);
+            for (int i = 0; i < 10; i++) {
+                String payload = "message-" + i;
+                client.messages().sendMessages(
+                        STREAM_ID,
+                        TOPIC_ID,
+                        partitioning,
+                        List.of(Message.of(payload)));
+                System.out.println("Sent: " + payload);
+            }
         }
     }
 }
@@ -69,28 +85,38 @@ import java.nio.charset.StandardCharsets;
 import java.util.Optional;
 
 public class ConsumerExample {
+
+    static final StreamId STREAM_ID = StreamId.of("sample-stream");
+    static final TopicId TOPIC_ID = TopicId.of("sample-topic");
+
     public static void main(String[] args) {
         try (var client = IggyTcpClient.builder()
-                .host("localhost")
+                .host("127.0.0.1")
                 .port(8090)
                 .credentials("iggy", "iggy")
                 .buildAndLogin()) {
 
-            var streamId = StreamId.of("sample-stream");
-            var topicId = TopicId.of("sample-topic");
-            var consumer = Consumer.of(0L);
-
-            PolledMessages polled = client.messages()
-                    .pollMessages(
-                            streamId, topicId,
-                            Optional.of(0L), consumer,
-                            PollingStrategy.offset(BigInteger.ZERO),
-                            10L, false);
-
-            for (Message message : polled.messages()) {
-                String payload = new String(
-                        message.payload(), StandardCharsets.UTF_8);
-                System.out.println("Payload: " + payload);
+            BigInteger offset = BigInteger.ZERO;
+            Consumer consumer = Consumer.of(0L);
+
+            while (true) {
+                PolledMessages polledMessages = client.messages().pollMessages(
+                        STREAM_ID,
+                        TOPIC_ID,
+                        Optional.of(0L),
+                        consumer,
+                        PollingStrategy.offset(offset),
+                        10L,
+                        false);
+
+                if (polledMessages.messages().isEmpty()) {
+                    break;
+                }
+                for (Message message : polledMessages.messages()) {
+                    String payload = new String(message.payload(), 
StandardCharsets.UTF_8);
+                    System.out.printf("Offset: %d, Payload: %s%n", 
message.header().offset(), payload);
+                }
+                offset = 
offset.add(BigInteger.valueOf(polledMessages.messages().size()));
             }
         }
     }
diff --git a/content/docs/sdk/java/intro.mdx b/content/docs/sdk/java/intro.mdx
index 163dcfdf..18c8c348 100644
--- a/content/docs/sdk/java/intro.mdx
+++ b/content/docs/sdk/java/intro.mdx
@@ -34,7 +34,6 @@ import org.apache.iggy.message.Message;
 import org.apache.iggy.message.Partitioning;
 import org.apache.iggy.topic.CompressionAlgorithm;
 import java.math.BigInteger;
-import java.util.ArrayList;
 import java.util.List;
 import static java.util.Optional.empty;
 
@@ -47,20 +46,25 @@ public class Producer {
 
     public static void main(String[] args) {
         try (var client = IggyTcpClient.builder()
-                .host("localhost")
+                .host("127.0.0.1")
                 .port(8090)
                 .credentials("iggy", "iggy")
                 .buildAndLogin()) {
 
-            client.streams().createStream(STREAM_NAME);
-            client.topics().createTopic(
-                    STREAM_ID,
-                    1L,
-                    CompressionAlgorithm.None,
-                    BigInteger.ZERO,
-                    BigInteger.ZERO,
-                    empty(),
-                    TOPIC_NAME);
+            // Re-running this example is fine: only create what is missing.
+            if (client.streams().getStream(STREAM_ID).isEmpty()) {
+                client.streams().createStream(STREAM_NAME);
+            }
+            if (client.topics().getTopic(STREAM_ID, TOPIC_ID).isEmpty()) {
+                client.topics().createTopic(
+                        STREAM_ID,
+                        1L,
+                        CompressionAlgorithm.None,
+                        BigInteger.ZERO,
+                        BigInteger.ZERO,
+                        empty(),
+                        TOPIC_NAME);
+            }
 
             Partitioning partitioning = Partitioning.partitionId(0L);
             for (int i = 0; i < 10; i++) {
@@ -70,6 +74,7 @@ public class Producer {
                         TOPIC_ID,
                         partitioning,
                         List.of(Message.of(payload)));
+                System.out.println("Sent: " + payload);
             }
         }
     }
@@ -97,7 +102,7 @@ public class SampleConsumer {
 
     public static void main(String[] args) {
         try (var client = IggyTcpClient.builder()
-                .host("localhost")
+                .host("127.0.0.1")
                 .port(8090)
                 .credentials("iggy", "iggy")
                 .buildAndLogin()) {
@@ -118,9 +123,9 @@ public class SampleConsumer {
                 if (polledMessages.messages().isEmpty()) {
                     break;
                 }
-                for (Message msg : polledMessages.messages()) {
-                    String payload = new String(msg.payload(), 
StandardCharsets.UTF_8);
-                    System.out.printf("Offset: %d, Payload: %s%n", offset, 
payload);
+                for (Message message : polledMessages.messages()) {
+                    String payload = new String(message.payload(), 
StandardCharsets.UTF_8);
+                    System.out.printf("Offset: %d, Payload: %s%n", 
message.header().offset(), payload);
                 }
                 offset = 
offset.add(BigInteger.valueOf(polledMessages.messages().size()));
             }

Reply via email to