shreyas.k
shreyas.k

Reputation: 191

How to write a method subscribing to a topic in Spring Cloud Stream Kafka binding?

I want to write a consumer method which subscribes to a topic using KafkaListener.

I found an answer which suggested this -

@KafkaListener(id = "foo", topics = "dead-out")
public void dlq(Message<?> in) {
    System.out.println("DLQ:" + in);
}

Now, in the annotation 'topics' is the name of the subscribed topic. But what is 'id' field ? Or is there a better way?

Your help is appreciated.

Upvotes: 0

Views: 268

Answers (1)

Gary Russell
Gary Russell

Reputation: 174554

@KafkaListener has nothing to do with Spring Cloud Stream; it is in the Spring for Apache Kafka project (which Spring Cloud Stream uses for its Kafka binder).

See the javadocs

    /**
     * The unique identifier of the container managing for this endpoint.
     * <p>If none is specified an auto-generated one is provided.
     * <p>Note: When provided, this value will override the group id property
     * in the consumer factory configuration, unless {@link #idIsGroup()}
     * is set to false.
     * <p>SpEL {@code #{...}} and property place holders {@code ${...}} are supported.
     * @return the {@code id} for the container managing for this endpoint.
     * @see org.springframework.kafka.config.KafkaListenerEndpointRegistry#getListenerContainer(String)
     */
    String id() default "";

The id is also used to get the listener container from the KafkaListenerEndpointRegistry bean so you can stop() and start() it.

If you want to use spring-cloud-stream instead; read its documentation.

Upvotes: 2

Related Questions