org.tinystruct.data.tools.KafkaClient Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of tinystruct Show documentation
Show all versions of tinystruct Show documentation
A simple framework for Java development. Simple is hard, Complex is easy. Better thinking, better design.
package org.tinystruct.data.tools;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.RecordMetadata;
import org.apache.kafka.common.serialization.StringSerializer;
import org.tinystruct.AbstractApplication;
import org.tinystruct.system.annotation.Action;
import java.util.Properties;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.logging.Level;
import java.util.logging.Logger;
public class KafkaClient extends AbstractApplication {
private final KafkaProducer producer;
private final static Logger logger = Logger.getLogger(KafkaClient.class.getName());
public KafkaClient() {
Properties properties = new Properties();
properties.put("bootstrap.servers", "192.168.1.101:9092");
properties.put("ack", "all");
properties.put("key.serializer", StringSerializer.class.getName());
properties.put("value.serializer", StringSerializer.class.getName());
producer = new KafkaProducer(properties);
}
@Action("send")
public void send(ProducerRecord record) {
Future future = producer.send(record);
try {
future.get();
} catch (InterruptedException e) {
logger.log(Level.SEVERE, e.getMessage(), e);
} catch (ExecutionException e) {
logger.log(Level.SEVERE, e.getMessage(), e);
} finally {
producer.close();
}
}
@Override
public void init() {
}
@Override
public String version() {
return null;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy