SDKJava
Java SDK
The Iggy Java SDK is a library that allows you to interact with the Iggy API from your Java application. It communicates with the Iggy server over TCP using the binary protocol. The repository can be found here.
Installation
Maven
<dependency>
<groupId>org.apache.iggy</groupId>
<artifactId>iggy</artifactId>
<version>LATEST</version>
</dependency>Gradle
implementation 'org.apache.iggy:iggy:LATEST'Quick start
Producer
import org.apache.iggy.client.blocking.tcp.IggyTcpClient;
import org.apache.iggy.identifier.StreamId;
import org.apache.iggy.identifier.TopicId;
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;
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")
.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);
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)));
}
}
}
}Consumer
import org.apache.iggy.client.blocking.tcp.IggyTcpClient;
import org.apache.iggy.consumergroup.Consumer;
import org.apache.iggy.identifier.StreamId;
import org.apache.iggy.identifier.TopicId;
import org.apache.iggy.message.Message;
import org.apache.iggy.message.PolledMessages;
import org.apache.iggy.message.PollingStrategy;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.util.Optional;
public class SampleConsumer {
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")
.port(8090)
.credentials("iggy", "iggy")
.buildAndLogin()) {
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 msg : polledMessages.messages()) {
String payload = new String(msg.payload(), StandardCharsets.UTF_8);
System.out.printf("Offset: %d, Payload: %s%n", offset, payload);
}
offset = offset.add(BigInteger.valueOf(polledMessages.messages().size()));
}
}
}
}Examples
Working examples are available in the examples/java directory as a Gradle project. The following example sets are included:
- getting-started - basic producer and consumer
- async - asynchronous message processing
- message-envelope - working with message envelopes
- message-headers - custom message headers
- multi-tenant - multi-tenant streaming setup
- tcp-tls - TLS-encrypted TCP connections
- sink-data-producer - data sink producer pattern
- stream-builder - stream builder API usage