Created
December 25, 2018 09:54
-
-
Save john-nash-rs/fa5d5b4033188120b972038d56bbac53 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.wp.npe.client; | |
import com.google.common.util.concurrent.Runnables; | |
import com.google.gson.Gson; | |
import com.google.inject.Guice; | |
import com.google.inject.Inject; | |
import com.google.inject.Injector; | |
import com.wp.npe.NpeModule; | |
import com.wp.npe.core.KafkaHelper; | |
import com.wp.npe.db.dao.StudentDao; | |
import com.wp.npe.db.entities.Student; | |
import com.wp.npe.models.Bulk; | |
import io.dropwizard.hibernate.UnitOfWork; | |
import org.apache.kafka.clients.consumer.ConsumerRecord; | |
import org.apache.kafka.clients.consumer.ConsumerRecords; | |
import org.apache.kafka.clients.consumer.KafkaConsumer; | |
import java.util.Arrays; | |
import java.util.Properties; | |
public class KafkaConsumerAPI implements Runnable { | |
Properties props = new Properties(); | |
KafkaConsumer<String, String> consumer; | |
StudentDao studentDao; | |
@Inject | |
public KafkaConsumerAPI(StudentDao studentDao) { | |
props.put("bootstrap.servers", "localhost:9092"); | |
props.put("group.id", "test"); | |
props.put("enable.auto.commit", "true"); | |
props.put("auto.commit.interval.ms", "1000"); | |
props.put("session.timeout.ms", "30000"); | |
props.put("key.deserializer", | |
"org.apache.kafka.common.serialization.StringDeserializer"); | |
props.put("value.deserializer", | |
"org.apache.kafka.common.serialization.StringDeserializer"); | |
consumer = new KafkaConsumer | |
<String, String>(props); | |
consumer.subscribe(Arrays.asList(KafkaProducerAPI.TOPIC)); | |
this.studentDao = studentDao; | |
} | |
public void run(){ | |
System.out.println("-------------------------Consumer----------------------"); | |
while (true) { | |
ConsumerRecords<String, String> records = consumer.poll(100); | |
for (ConsumerRecord<String, String> record : records) { | |
System.out.printf("Consumer is working : offset = %d, key = %s, value = %s\n", | |
record.offset(), record.key(), record.value()); | |
processInputAsync(record.value()); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment