Skip to content

Commit dbc2c49

Browse files
vivekkr12pivovarit
authored andcommitted
BAEL-578: Add spring-kafka module (eugenp#1407)
1 parent 6aefd62 commit dbc2c49

File tree

6 files changed

+210
-0
lines changed

6 files changed

+210
-0
lines changed

spring-kafka/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Spring Kakfa
2+
3+
This is a simple Spring Boot app to demonstrate sending and receiving of messages in Kafka using spring-kafka.
4+
5+
As Kafka topics are not created automatically by default, this application requires that a topic named 'baeldung' is created manually.
6+
7+
`$ bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic baeldung`
8+
9+
Two listeners with group Ids **foo** and **bar** are configured. When run successfully, the *Hello World!* message will be received by both the listeners and logged on console.

spring-kafka/pom.xml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>com.baeldung</groupId>
6+
<artifactId>spring-kafka</artifactId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
9+
<name>spring-kafka</name>
10+
<description>Intro to Kafka with Spring</description>
11+
12+
<properties>
13+
<java.version>1.8</java.version>
14+
<spring-kafka.version>1.1.3.RELEASE</spring-kafka.version>
15+
</properties>
16+
17+
<parent>
18+
<groupId>org.springframework.boot</groupId>
19+
<artifactId>spring-boot-starter-parent</artifactId>
20+
<version>1.5.2.RELEASE</version>
21+
</parent>
22+
23+
<dependencies>
24+
25+
<dependency>
26+
<groupId>org.springframework.boot</groupId>
27+
<artifactId>spring-boot-starter</artifactId>
28+
</dependency>
29+
30+
<dependency>
31+
<groupId>org.springframework.kafka</groupId>
32+
<artifactId>spring-kafka</artifactId>
33+
</dependency>
34+
35+
</dependencies>
36+
37+
<build>
38+
<plugins>
39+
<plugin>
40+
<groupId>org.springframework.boot</groupId>
41+
<artifactId>spring-boot-maven-plugin</artifactId>
42+
</plugin>
43+
</plugins>
44+
</build>
45+
46+
</project>
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.baeldung.spring.kafka;
2+
3+
import java.util.concurrent.CountDownLatch;
4+
import java.util.concurrent.TimeUnit;
5+
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.beans.factory.annotation.Value;
8+
import org.springframework.boot.SpringApplication;
9+
import org.springframework.boot.autoconfigure.SpringBootApplication;
10+
import org.springframework.context.ConfigurableApplicationContext;
11+
import org.springframework.context.annotation.Bean;
12+
import org.springframework.kafka.annotation.KafkaListener;
13+
import org.springframework.kafka.core.KafkaTemplate;
14+
15+
@SpringBootApplication
16+
public class KafkaApplication {
17+
18+
public static void main(String[] args) throws Exception {
19+
ConfigurableApplicationContext context = SpringApplication.run(KafkaApplication.class, args);
20+
MessageProducer producer = context.getBean(MessageProducer.class);
21+
producer.sendMessage("Hello, World!");
22+
23+
MessageListener listener = context.getBean(MessageListener.class);
24+
listener.latch.await(20, TimeUnit.SECONDS);
25+
Thread.sleep(60000);
26+
context.close();
27+
28+
}
29+
30+
@Bean
31+
public MessageProducer messageProducer() {
32+
return new MessageProducer();
33+
}
34+
35+
@Bean
36+
public MessageListener messageListener() {
37+
return new MessageListener();
38+
}
39+
40+
public static class MessageProducer {
41+
42+
@Autowired
43+
private KafkaTemplate<String, String> kafkaTemplate;
44+
45+
@Value(value = "${message.topic.name}")
46+
private String topicName;
47+
48+
public void sendMessage(String message) {
49+
kafkaTemplate.send(topicName, message);
50+
}
51+
52+
}
53+
54+
public static class MessageListener {
55+
56+
private CountDownLatch latch = new CountDownLatch(2);
57+
58+
@KafkaListener(topics = "${message.topic.name}", group = "foo", containerFactory = "fooKafkaListenerContainerFactory")
59+
public void listenGroupFoo(String message) {
60+
System.out.println("Received Messasge in group 'foo': " + message);
61+
latch.countDown();
62+
}
63+
64+
@KafkaListener(topics = "${message.topic.name}", group = "bar", containerFactory = "barKafkaListenerContainerFactory")
65+
public void listenGroupBar(String message) {
66+
System.out.println("Received Messasge in group 'bar': " + message);
67+
latch.countDown();
68+
}
69+
70+
}
71+
72+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.baeldung.spring.kafka;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
import org.apache.kafka.clients.consumer.ConsumerConfig;
7+
import org.apache.kafka.common.serialization.StringDeserializer;
8+
import org.springframework.beans.factory.annotation.Value;
9+
import org.springframework.context.annotation.Bean;
10+
import org.springframework.context.annotation.Configuration;
11+
import org.springframework.kafka.annotation.EnableKafka;
12+
import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory;
13+
import org.springframework.kafka.core.ConsumerFactory;
14+
import org.springframework.kafka.core.DefaultKafkaConsumerFactory;
15+
16+
@EnableKafka
17+
@Configuration
18+
public class KafkaConsumerConfig {
19+
20+
@Value(value = "${kafka.bootstrapAddress}")
21+
private String bootstrapAddress;
22+
23+
public ConsumerFactory<String, String> consumerFactory(String groupId) {
24+
Map<String, Object> props = new HashMap<>();
25+
props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapAddress);
26+
props.put(ConsumerConfig.GROUP_ID_CONFIG, groupId);
27+
props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
28+
props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
29+
return new DefaultKafkaConsumerFactory<>(props);
30+
}
31+
32+
@Bean
33+
public ConcurrentKafkaListenerContainerFactory<String, String> fooKafkaListenerContainerFactory() {
34+
ConcurrentKafkaListenerContainerFactory<String, String> factory = new ConcurrentKafkaListenerContainerFactory<>();
35+
factory.setConsumerFactory(consumerFactory("foo"));
36+
return factory;
37+
}
38+
39+
@Bean
40+
public ConcurrentKafkaListenerContainerFactory<String, String> barKafkaListenerContainerFactory() {
41+
ConcurrentKafkaListenerContainerFactory<String, String> factory = new ConcurrentKafkaListenerContainerFactory<>();
42+
factory.setConsumerFactory(consumerFactory("bar"));
43+
return factory;
44+
}
45+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.baeldung.spring.kafka;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
import org.apache.kafka.clients.producer.ProducerConfig;
7+
import org.apache.kafka.common.serialization.StringSerializer;
8+
import org.springframework.beans.factory.annotation.Value;
9+
import org.springframework.context.annotation.Bean;
10+
import org.springframework.context.annotation.Configuration;
11+
import org.springframework.kafka.core.DefaultKafkaProducerFactory;
12+
import org.springframework.kafka.core.KafkaTemplate;
13+
import org.springframework.kafka.core.ProducerFactory;
14+
15+
@Configuration
16+
public class KafkaProducerConfig {
17+
18+
@Value(value = "${kafka.bootstrapAddress}")
19+
private String bootstrapAddress;
20+
21+
@Bean
22+
public ProducerFactory<String, String> producerFactory() {
23+
Map<String, Object> configProps = new HashMap<String, Object>();
24+
configProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapAddress);
25+
configProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
26+
configProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
27+
return new DefaultKafkaProducerFactory<String, String>(configProps);
28+
}
29+
30+
@Bean
31+
public KafkaTemplate<String, String> kafkaTemplate() {
32+
KafkaTemplate<String, String> template =
33+
new KafkaTemplate<String, String>(producerFactory());
34+
return template;
35+
}
36+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
kafka.bootstrapAddress=localhost:9092
2+
message.topic.name=baeldung

0 commit comments

Comments
 (0)