gpt-3.5-turbo-0301· OpenAI
WildChat-4.8M (AI2)
create kafka processor in java spring boot
- User
create kafka processor in java spring boot
- Assistant
Here's an example of how to create a Kafka processor in Java Spring Boot:
- Add the Kafka dependency in your pom.xml file:
<dependency> <groupId>org.springframework.kafka</groupId> <artifactId>spring-kafka</artifactId> <version>2.7.3</version> </dependency>- Create a Kafka listener class:
@Component public class KafkaProcessor { @KafkaListener(topics = "test-topic") public void processMessage(String message) { System.out.println("Received message: " + message); // process the message here } }-
In the above code,
@KafkaListenerannotation is used to listen to a Kafka topic namedtest-topic. Once a message is received on this topic, theprocessMessagemethod will be called for further processing. -
You can configure the Kafka properties in your application.properties file:
spring.kafka.consumer.bootstrap-servers=localhost:9092 spring.kafka.consumer.auto-offset-reset=earliest spring.kafka.consumer.group-id=my-group-id