|
1 | 1 | --- |
2 | 2 | title: Examples |
3 | 3 | --- |
| 4 | + |
| 5 | +Working examples are available in the [examples/java](https://github.com/apache/iggy/tree/master/examples/java) directory as a Gradle project. The following example sets are included: |
| 6 | + |
| 7 | +- **gettingstarted** - basic blocking TCP producer and consumer |
| 8 | +- **async** - asynchronous producer and consumer |
| 9 | +- **messageenvelope** - JSON message envelope pattern |
| 10 | +- **messageheaders** - custom message headers |
| 11 | +- **multitenant** - multi-tenant isolation with per-tenant streams |
| 12 | +- **tcptls** - TLS-encrypted TCP connections |
| 13 | +- **sinkdataproducer** - bulk random data generation |
| 14 | +- **streambuilder** - combined producer and consumer in one class |
| 15 | + |
| 16 | +## Producer |
| 17 | + |
| 18 | +```java |
| 19 | +import org.apache.iggy.client.blocking.tcp.IggyTcpClient; |
| 20 | +import org.apache.iggy.identifier.StreamId; |
| 21 | +import org.apache.iggy.identifier.TopicId; |
| 22 | +import org.apache.iggy.message.Message; |
| 23 | +import org.apache.iggy.message.Partitioning; |
| 24 | +import org.apache.iggy.topic.CompressionAlgorithm; |
| 25 | +import java.math.BigInteger; |
| 26 | +import java.util.List; |
| 27 | +import static java.util.Optional.empty; |
| 28 | + |
| 29 | +public class Producer { |
| 30 | + public static void main(String[] args) { |
| 31 | + try (var client = IggyTcpClient.builder() |
| 32 | + .host("localhost") |
| 33 | + .port(8090) |
| 34 | + .credentials("iggy", "iggy") |
| 35 | + .buildAndLogin()) { |
| 36 | + |
| 37 | + var streamId = StreamId.of("sample-stream"); |
| 38 | + var topicId = TopicId.of("sample-topic"); |
| 39 | + |
| 40 | + client.streams().createStream("sample-stream"); |
| 41 | + client.topics().createTopic( |
| 42 | + streamId, 1L, |
| 43 | + CompressionAlgorithm.None, |
| 44 | + BigInteger.ZERO, BigInteger.ZERO, |
| 45 | + empty(), "sample-topic"); |
| 46 | + |
| 47 | + var partitioning = Partitioning.partitionId(0L); |
| 48 | + var messages = List.of(Message.of("hello world")); |
| 49 | + client.messages().sendMessages( |
| 50 | + streamId, topicId, |
| 51 | + partitioning, messages); |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +## Consumer |
| 58 | + |
| 59 | +```java |
| 60 | +import org.apache.iggy.client.blocking.tcp.IggyTcpClient; |
| 61 | +import org.apache.iggy.consumergroup.Consumer; |
| 62 | +import org.apache.iggy.identifier.StreamId; |
| 63 | +import org.apache.iggy.identifier.TopicId; |
| 64 | +import org.apache.iggy.message.Message; |
| 65 | +import org.apache.iggy.message.PolledMessages; |
| 66 | +import org.apache.iggy.message.PollingStrategy; |
| 67 | +import java.math.BigInteger; |
| 68 | +import java.nio.charset.StandardCharsets; |
| 69 | +import java.util.Optional; |
| 70 | + |
| 71 | +public class ConsumerExample { |
| 72 | + public static void main(String[] args) { |
| 73 | + try (var client = IggyTcpClient.builder() |
| 74 | + .host("localhost") |
| 75 | + .port(8090) |
| 76 | + .credentials("iggy", "iggy") |
| 77 | + .buildAndLogin()) { |
| 78 | + |
| 79 | + var streamId = StreamId.of("sample-stream"); |
| 80 | + var topicId = TopicId.of("sample-topic"); |
| 81 | + var consumer = Consumer.of(0L); |
| 82 | + |
| 83 | + PolledMessages polled = client.messages() |
| 84 | + .pollMessages( |
| 85 | + streamId, topicId, |
| 86 | + Optional.of(0L), consumer, |
| 87 | + PollingStrategy.offset(BigInteger.ZERO), |
| 88 | + 10L, false); |
| 89 | + |
| 90 | + for (Message message : polled.messages()) { |
| 91 | + String payload = new String( |
| 92 | + message.payload(), StandardCharsets.UTF_8); |
| 93 | + System.out.println("Payload: " + payload); |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +For the full source code, see the [examples/java](https://github.com/apache/iggy/tree/master/examples/java) directory. |
0 commit comments