BAEL-578: Add spring-kafka module (#1407)
This commit is contained in:
committed by
Grzegorz Piwowarek
parent
6aefd62288
commit
dbc2c49fe2
@@ -0,0 +1,72 @@
|
||||
package com.baeldung.spring.kafka;
|
||||
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.kafka.annotation.KafkaListener;
|
||||
import org.springframework.kafka.core.KafkaTemplate;
|
||||
|
||||
@SpringBootApplication
|
||||
public class KafkaApplication {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
ConfigurableApplicationContext context = SpringApplication.run(KafkaApplication.class, args);
|
||||
MessageProducer producer = context.getBean(MessageProducer.class);
|
||||
producer.sendMessage("Hello, World!");
|
||||
|
||||
MessageListener listener = context.getBean(MessageListener.class);
|
||||
listener.latch.await(20, TimeUnit.SECONDS);
|
||||
Thread.sleep(60000);
|
||||
context.close();
|
||||
|
||||
}
|
||||
|
||||
@Bean
|
||||
public MessageProducer messageProducer() {
|
||||
return new MessageProducer();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public MessageListener messageListener() {
|
||||
return new MessageListener();
|
||||
}
|
||||
|
||||
public static class MessageProducer {
|
||||
|
||||
@Autowired
|
||||
private KafkaTemplate<String, String> kafkaTemplate;
|
||||
|
||||
@Value(value = "${message.topic.name}")
|
||||
private String topicName;
|
||||
|
||||
public void sendMessage(String message) {
|
||||
kafkaTemplate.send(topicName, message);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class MessageListener {
|
||||
|
||||
private CountDownLatch latch = new CountDownLatch(2);
|
||||
|
||||
@KafkaListener(topics = "${message.topic.name}", group = "foo", containerFactory = "fooKafkaListenerContainerFactory")
|
||||
public void listenGroupFoo(String message) {
|
||||
System.out.println("Received Messasge in group 'foo': " + message);
|
||||
latch.countDown();
|
||||
}
|
||||
|
||||
@KafkaListener(topics = "${message.topic.name}", group = "bar", containerFactory = "barKafkaListenerContainerFactory")
|
||||
public void listenGroupBar(String message) {
|
||||
System.out.println("Received Messasge in group 'bar': " + message);
|
||||
latch.countDown();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user