Skip to content

Commit

Permalink
Add producer example
Browse files Browse the repository at this point in the history
  • Loading branch information
mmodzelewski committed Oct 20, 2024
1 parent 27e9a75 commit d66ee57
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
16 changes: 16 additions & 0 deletions examples/simple-producer/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
plugins {
id("java")
}

group = "rs.iggy"
version = "0.0.1-SNAPSHOT"

repositories {
mavenCentral()
}

dependencies {
implementation(project(":sdk"))
implementation("org.slf4j:slf4j-api:2.0.9")
runtimeOnly("ch.qos.logback:logback-classic:1.4.12")
}
72 changes: 72 additions & 0 deletions examples/simple-producer/src/main/java/rs/iggy/SimpleProducer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package rs.iggy;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import rs.iggy.clients.blocking.tcp.IggyTcpClient;
import rs.iggy.identifier.StreamId;
import rs.iggy.identifier.TopicId;
import rs.iggy.message.BigIntegerMessageId;
import rs.iggy.message.MessageToSend;
import rs.iggy.message.Partitioning;
import rs.iggy.message.PartitioningKind;
import rs.iggy.stream.StreamDetails;
import rs.iggy.topic.CompressionAlgorithm;
import rs.iggy.topic.TopicDetails;
import java.math.BigInteger;
import java.util.Optional;
import static java.util.Collections.singletonList;
import static java.util.Optional.empty;

public class SimpleProducer {
private static final String STREAM_NAME = "dev01";
private static final StreamId STREAM_ID = StreamId.of(STREAM_NAME);
private static final String TOPIC_NAME = "events";
private static final TopicId TOPIC_ID = TopicId.of(TOPIC_NAME);
private static final Logger log = LoggerFactory.getLogger(SimpleProducer.class);

public static void main(String[] args) {
IggyTcpClient client = new IggyTcpClient("localhost", 8090);
client.users().login("iggy", "iggy");

createStream(client);
createTopic(client);

int counter = 0;
while (counter++ < 1000) {
var text = "message from simple producer " + counter;
var message = new MessageToSend(new BigIntegerMessageId(BigInteger.ZERO), text.getBytes(), empty());
client.messages()
.sendMessages(STREAM_ID,
TOPIC_ID,
new Partitioning(PartitioningKind.Balanced, new byte[0]),
singletonList(message));
log.debug("Message {} sent", counter);
}

}

private static void createStream(IggyTcpClient client) {
Optional<StreamDetails> stream = client.streams().getStream(STREAM_ID);
if (stream.isPresent()) {
return;
}
client.streams().createStream(empty(), STREAM_NAME);
}

private static void createTopic(IggyTcpClient client) {
Optional<TopicDetails> topic = client.topics().getTopic(STREAM_ID, TOPIC_ID);
if (topic.isPresent()) {
return;
}
client.topics()
.createTopic(STREAM_ID,
empty(),
1L,
CompressionAlgorithm.none,
BigInteger.ZERO,
BigInteger.ZERO,
empty(),
TOPIC_NAME);
}

}
2 changes: 2 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ include("sdk")

include("simple-consumer-example")
project(":simple-consumer-example").projectDir = file("examples/simple-consumer")
include("simple-producer-example")
project(":simple-producer-example").projectDir = file("examples/simple-producer")

0 comments on commit d66ee57

Please sign in to comment.