Created
June 5, 2017 15:26
-
-
Save bufferings/074af0feb922f78e4838e3a65f4e5d49 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
spring.kafka.consumer.group-id=foo | |
spring.kafka.consumer.auto-offset-reset=earliest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.demo; | |
import java.util.concurrent.CountDownLatch; | |
import java.util.concurrent.TimeUnit; | |
import org.apache.kafka.clients.consumer.ConsumerRecord; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.boot.CommandLineRunner; | |
import org.springframework.boot.SpringApplication; | |
import org.springframework.boot.autoconfigure.SpringBootApplication; | |
import org.springframework.kafka.annotation.KafkaListener; | |
import org.springframework.kafka.core.KafkaTemplate; | |
@SpringBootApplication | |
public class DemoApplication implements CommandLineRunner { | |
public static Logger logger = LoggerFactory.getLogger(DemoApplication.class); | |
public static void main(String[] args) { | |
SpringApplication.run(DemoApplication.class, args); | |
} | |
@Autowired | |
private KafkaTemplate<String, String> template; | |
private final CountDownLatch latch = new CountDownLatch(3); | |
@Override | |
public void run(String... args) throws Exception { | |
this.template.send("myTopic", "foo1"); | |
this.template.send("myTopic", "foo2"); | |
this.template.send("myTopic", "foo3"); | |
latch.await(60, TimeUnit.SECONDS); | |
logger.info("All received"); | |
} | |
@KafkaListener(topics = "myTopic") | |
public void listen(ConsumerRecord<?, ?> cr) throws Exception { | |
logger.info(cr.toString()); | |
latch.countDown(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment