[image: image.png]
Consumer immediately terminated not waiting for message by producer ,
below is my code to send a message to Topic
@Service
public class KafkaProducer {
@Value("${spring.kafka.bootstrap-servers:localhost:9092}")
private String bootstrapServers;
private static final Logger
log=LoggerFactory.getLogger(KafkaProducer.class);
private KafkaTemplate<String, String> kafkaTemplate;
public KafkaProducer(KafkaTemplate<String, String> kafkaTemplate) {
this.kafkaTemplate = kafkaTemplate;
}
public void sendMessage(String message) {
try {
// Force send and wait for acknowledgment
kafkaTemplate.send("javaguides", message).get();
log.info(" Message sent successfully: {}", message);
} catch (Exception ex) {
log.error(" Failed to send message: {}", ex.getMessage(), ex);
}
Properties props = new Properties();
props.put("bootstrap.servers", bootstrapServers);
try (AdminClient adminClient = AdminClient.create(props)) {
DescribeClusterResult clusterResult = adminClient.describeCluster();
String clusterId = clusterResult.clusterId().get();
String controller = clusterResult.controller().get().toString();
String nodes = clusterResult.nodes().get().toString();
log.info(" Kafka Cluster ID: {}", clusterId);
log.info(" Kafka Controller: {}", controller);
log.info(" Kafka Nodes: {}", nodes);
} catch (ExecutionException | InterruptedException e) {
log.error(" Failed to fetch Kafka cluster info: {}", e.getMessage());
}
}
if you need more information please let me know i will gave you
On Sun, 17 Aug 2025 at 21:36, Subra I <[email protected]> wrote:
> 1. Did you verify your kafka setup by producing from producer console and
> then seeing the output for that topic on consumer console? If not, please
> do that to validate your kafka setup itself.
>
> 2. You don't need a kafka consumer class in Springboot Java code - unless u
> plan to do some processing in your code after consuming the data. For
> testing at this point, I think the kafka consumer console should be enough.
>
> 3. On what basis are u saying that kafka produce worked? Usually, when u do
> kafkaProducer.send() in Java code, an exception will be thrown if there is
> a failure. Did you check for that? If you are ok, pl share code snippet
> here.
>
> On Sun, Aug 17, 2025 at 9:18 PM KIRAN KUMAR B O <[email protected]>
> wrote:
>
> > Hi,
> >
> > I am learning Apache Kafka and Spring Boot. I have a Kafka 4.0 broker
> > running locally and a Spring Boot application sending messages to a topic
> > javaguides.
> >
> > *Below steps I followed:*
> >
> > 1.
> >
> > Started Kafka broker on localhost:9092.
> > 2.
> >
> > Verified the topic exists:
> >
> > .\bin\windows\kafka-topics.bat --bootstrap-server localhost:9092 --list
> >
> > Output: javaguides
> >
> >
> > 3. Sent a message via Spring Boot:
> >
> > http://localhost:8080/api/v1/kafka/publish?message=Hello Kafka
> >
> > Spring Boot logs confirm the message is sent successfully.
> > 4. Tried to consume messages using console consumer:
> >
> > .\bin\windows\kafka-console-consumer.bat --bootstrap-server
> > localhost:9092 --topic javaguides --from-beginning
> >
> > The console consumer starts but *does not display any messages*, even
> > though the topic has data.
> >
> > *Kafka Version:* 4.0
> > *Spring Boot Version:* 3.x
> > *Spring Kafka Configuration:*
> >
> >
> >
> spring.kafka.consumer.bootstrap-servers=localhost:9092spring.kafka.consumer.group-id=mygroupspring.kafka.consumer.auto-offset-reset=earliestspring.kafka.consumer.key-deserializer=org.apache.kafka.common.serialization.StringDeserializerspring.kafka.consumer.value-deserializer=org.apache.kafka.common.serialization.StringDeserializer
> >
> >
> spring.kafka.producer.bootstrap-servers=localhost:9092spring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerializerspring.kafka.producer.value-serializer=org.apache.kafka.common.serialization.StringSerializer
> >
> > *Notes:*
> >
> > -
> >
> > I have *not created a consumer class* in Spring Boot; I am relying on
> > the console consumer to read messages.
> >
> > *Questions:*
> >
> > 1.
> >
> > Why does the console consumer not show messages sent by my Spring Boot
> > producer?
> > 2.
> >
> > Is a Spring Boot KafkaConsumer class required for messages to appear
> in
> > the console consumer?
> > 3.
> >
> > Are there any Kafka 4.0 configuration changes that could prevent the
> > console consumer from receiving messages?
> >
> > Any guidance would be greatly appreciated!
> >
> > Thank you,
> > Kiran Kumar B O
> >
>