Skip to content

Instantly share code, notes, and snippets.

@watsh-rajneesh
Created March 2, 2017 07:08
Show Gist options
  • Save watsh-rajneesh/0feb72e09d5c7624ad9771fa96a2bd28 to your computer and use it in GitHub Desktop.
Save watsh-rajneesh/0feb72e09d5c7624ad9771fa96a2bd28 to your computer and use it in GitHub Desktop.
Apache Kafka Message Producer
package edu.sjsu.amigo.mp.kafka;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import java.io.Closeable;
import java.util.Properties;
import static edu.sjsu.amigo.mp.kafka.MessageQueueConstants.USER_MSG_TOPIC;
/**
* A kafka message producer.
*
* @author rwatsh on 2/26/17.
*/
public class MessageProducer implements AutoCloseable {
private final Producer producer;
public MessageProducer() {
//Configure the Producer
Properties configProperties = new Properties();
// Assuming that localhost port 9092 is mapped to kafka container's port 9092
// TODO externalize the port
configProperties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,"localhost:9092");
configProperties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,"org.apache.kafka.common.serialization.ByteArraySerializer");
configProperties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,"org.apache.kafka.common.serialization.StringSerializer");
producer = new KafkaProducer<String, String>(configProperties);
}
public void sendUserMessage(String message) {
sendMessage(USER_MSG_TOPIC, message);
}
private void sendMessage(String topicName, String message) {
ProducerRecord<String, String> rec = new ProducerRecord<String, String>(topicName, message);
producer.send(rec);
}
/**
* Closes this resource, relinquishing any underlying resources.
* This method is invoked automatically on objects managed by the
* {@code try}-with-resources statement.
* <p>
* <p>While this interface method is declared to throw {@code
* Exception}, implementers are <em>strongly</em> encouraged to
* declare concrete implementations of the {@code close} method to
* throw more specific exceptions, or to throw no exception at all
* if the close operation cannot fail.
* <p>
* <p> Cases where the close operation may fail require careful
* attention by implementers. It is strongly advised to relinquish
* the underlying resources and to internally <em>mark</em> the
* resource as closed, prior to throwing the exception. The {@code
* close} method is unlikely to be invoked more than once and so
* this ensures that the resources are released in a timely manner.
* Furthermore it reduces problems that could arise when the resource
* wraps, or is wrapped, by another resource.
* <p>
* <p><em>Implementers of this interface are also strongly advised
* to not have the {@code close} method throw {@link
* InterruptedException}.</em>
* <p>
* This exception interacts with a thread's interrupted status,
* and runtime misbehavior is likely to occur if an {@code
* InterruptedException} is {@linkplain Throwable#addSuppressed
* suppressed}.
* <p>
* More generally, if it would cause problems for an
* exception to be suppressed, the {@code AutoCloseable.close}
* method should not throw it.
* <p>
* <p>Note that unlike the {@link Closeable#close close}
* method of {@link Closeable}, this {@code close} method
* is <em>not</em> required to be idempotent. In other words,
* calling this {@code close} method more than once may have some
* visible side effect, unlike {@code Closeable.close} which is
* required to have no effect if called more than once.
* <p>
* However, implementers of this interface are strongly encouraged
* to make their {@code close} methods idempotent.
*
* @throws Exception if this resource cannot be closed
*/
@Override
public void close() throws Exception {
if (producer != null) {
producer.close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment