This will start a consumer thread in the background.
Also it will send a message after every 10 seconds to test-topic
.
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.apache.kafka.common.serialization.StringSerializer;
import org.springframework.kafka.core.DefaultKafkaConsumerFactory;
import org.springframework.kafka.core.DefaultKafkaProducerFactory;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.core.ProducerFactory;
import org.springframework.kafka.listener.ConcurrentMessageListenerContainer;
import org.springframework.kafka.listener.config.ContainerProperties;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class PeriodicProducerConsumer implements Runnable {
KafkaTemplate<String, Object> kafkaTemplate;
ScheduledExecutorService service;
PeriodicProducerConsumer() {
// Producer Declaration
Map<String, Object> configs = new HashMap<>();
configs.put("bootstrap.servers", "localhost:9092");
configs.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
configs.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
ProducerFactory<String, Object> producerFactory = new DefaultKafkaProducerFactory<>(configs);
this.kafkaTemplate = new KafkaTemplate<>(producerFactory);
// Consumer Declaration
Map<String, Object> consumerConfig = new HashMap<>();
consumerConfig.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "http://localhost:9092");
consumerConfig.put(ConsumerConfig.GROUP_ID_CONFIG, "airbus-service-ka-consumer-group");
DefaultKafkaConsumerFactory<String, String> kafkaConsumerFactory =
new DefaultKafkaConsumerFactory<>(
consumerConfig,
new StringDeserializer(),
new StringDeserializer());
String topic = "test-topic";
ContainerProperties containerProperties = new ContainerProperties(topic);
containerProperties.setMessageListener(new AirbusServiceKaMessageListener());
ConcurrentMessageListenerContainer<String, String> container =
new ConcurrentMessageListenerContainer<>(
kafkaConsumerFactory,
containerProperties);
container.start();
}
public void start() {
service = Executors.newSingleThreadScheduledExecutor();
service.scheduleWithFixedDelay(this, 5, 10, TimeUnit.SECONDS);
}
public void stop() {
service.shutdown();
}
@Override
public void run() {
String data = String.format("New Airbus Hello at %s", new Date());
kafkaTemplate.send("test-topic", data);
}
}
Here is Consumed message processing function:
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.listener.MessageListener;
public class AirbusServiceKaMessageListener implements MessageListener<String, Object> {
private static final Logger LOG = LoggerFactory.getLogger(AirbusServiceKaMessageListener.class);
@Override
public void onMessage(ConsumerRecord<String, Object> data) {
LOG.info("########################## New Consuming Message From Message Listener ##########################");
LOG.info("Message # {}", data.value());
LOG.info("#################################################################################################");
}
}
Bean.xml
<bean name="purekafkaProduceConsume" class="com.myntra.airbus.purekafka.PeriodicProducerConsumer" scope="singleton"
init-method="start" destroy-method="stop">
</bean>