Skip to content

Instantly share code, notes, and snippets.

@watsh-rajneesh
Last active March 2, 2017 07:14
Show Gist options
  • Save watsh-rajneesh/ae29b54646d1dd1c2d3f98be32c44c3b to your computer and use it in GitHub Desktop.
Save watsh-rajneesh/ae29b54646d1dd1c2d3f98be32c44c3b to your computer and use it in GitHub Desktop.
Apache Kafka Consumer Example
/*
* Copyright (c) 2017 San Jose State University.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*/
package edu.sjsu.amigo.cp.kafka;
import edu.sjsu.amigo.cp.jobs.JobManager;
import edu.sjsu.amigo.cp.jobs.MessageProcessorJob;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.errors.WakeupException;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.quartz.JobDataMap;
import org.quartz.SchedulerException;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import static edu.sjsu.amigo.mp.kafka.MessageQueueConstants.AMIGO_CHATBOT_GROUP;
import static edu.sjsu.amigo.mp.kafka.MessageQueueConstants.USER_MSG_TOPIC;
/**
* A Kafka message consumer. As soon as it receives a message it will spawn a job to process it.
*
* Based on https://www.confluent.io/blog/tutorial-getting-started-with-the-new-apache-kafka-0-9-consumer-client/
*
* @author rwatsh on 2/25/17.
*/
public class ConsumerLoop implements Runnable {
private final KafkaConsumer<String, String> consumer;
private final List<String> topics;
private final int id;
public ConsumerLoop(int id,
String groupId,
List<String> topics) {
this.id = id;
this.topics = topics;
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("group.id", groupId);
props.put("key.deserializer", StringDeserializer.class.getName());
props.put("value.deserializer", StringDeserializer.class.getName());
this.consumer = new KafkaConsumer<>(props);
}
@Override
public void run() {
try {
consumer.subscribe(topics);
while (true) {
ConsumerRecords<String, String> records = consumer.poll(Long.MAX_VALUE);
for (ConsumerRecord<String, String> record : records) {
Map<String, Object> data = new HashMap<>();
data.put("partition", record.partition());
data.put("offset", record.offset());
String value = record.value();
data.put("value", value);
processMessageAsync(value);
System.out.println(this.id + ": " + data);
}
}
} catch (WakeupException e) {
// ignore for shutdown
} finally {
consumer.close();
}
}
private void processMessageAsync(String value) {
if (value != null && !value.trim().isEmpty()) {
try {
// Using Quartz job scheduler to process the message asynchronously
// Job code is not shown in this gist.
// Some unique job name
String jobName = "MESG-JOB-" + UUID.randomUUID().toString();
String groupName = "CHATBOT-GRP";
JobDataMap params = new JobDataMap();
params.put("message", value);
JobManager.getInstance().scheduleJob(MessageProcessorJob.class, jobName, groupName, params);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void shutdown() {
consumer.wakeup();
}
/**
* Run the client program.
*
* @param args
*/
public static void main(String[] args) throws SchedulerException {
//Start the job scheduler
JobManager.getInstance().startScheduler();
int numConsumers = 3;
String groupId = AMIGO_CHATBOT_GROUP;
List<String> topics = Arrays.asList(USER_MSG_TOPIC);
ExecutorService executor = Executors.newFixedThreadPool(numConsumers);
final List<ConsumerLoop> consumers = new ArrayList<>();
for (int i = 0; i < numConsumers; i++) {
ConsumerLoop consumer = new ConsumerLoop(i, groupId, topics);
consumers.add(consumer);
executor.submit(consumer);
}
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
for (ConsumerLoop consumer : consumers) {
consumer.shutdown();
}
executor.shutdown();
try {
executor.awaitTermination(5000, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment