[Zookeeper] (Temp) stash

This commit is contained in:
2023-12-26 01:06:45 +07:00
parent c0e6ee20d4
commit 3cd77cb798
3 changed files with 60 additions and 0 deletions
+1
View File
@@ -12,6 +12,7 @@ repositories {
dependencies {
implementation("com.couchbase.client:java-client:3.5.1")
implementation("org.apache.commons:commons-lang3:3.12.0")
implementation("org.apache.kafka:kafka-clients:3.6.1")
implementation("org.apache.logging.log4j:log4j-core:2.14.1")
implementation("org.apache.logging.log4j:log4j-slf4j-impl:2.14.1")
implementation("redis.clients:jedis:3.7.0")
@@ -0,0 +1,34 @@
package com.zingplay.tiennm5;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import java.time.Duration;
import java.util.Collections;
import java.util.Properties;
public class KafkaMessageReceiver {
public static void main(String[] args) {
// Set up the properties for the Kafka consumer
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092"); // Replace with your Kafka server addresses
props.put("group.id", "test-group");
props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
// Create the consumer
KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
// Subscribe to the "test" topic
consumer.subscribe(Collections.singletonList("test"));
// Continuously listen for new messages
while (true) {
ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100));
for (ConsumerRecord<String, String> record : records) {
System.out.printf("Received message: (%s, %s)%n", record.key(), record.value());
}
}
}
}
@@ -0,0 +1,25 @@
package com.zingplay.tiennm5;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;
import java.util.Properties;
public class KafkaMessageSender {
public static void main(String[] args) {
// Set up the properties for the Kafka producer
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092"); // Replace with your Kafka server addresses
props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
// Create the producer
KafkaProducer<String, String> producer = new KafkaProducer<>(props);
// Send a message to the "test" topic
producer.send(new ProducerRecord<String, String>("test", "key", "value"));
// Close the producer
producer.close();
}
}