Polish "Add Kafka sample"

Closes gh-11597
This commit is contained in:
Stephane Nicoll 2018-02-08 12:37:56 +01:00
parent 89e1d95363
commit 07fa8bcf75
9 changed files with 84 additions and 59 deletions

View File

@ -119,6 +119,9 @@ The following sample applications are provided:
| link:spring-boot-sample-junit-jupiter[spring-boot-sample-junit-jupiter] | link:spring-boot-sample-junit-jupiter[spring-boot-sample-junit-jupiter]
| Demonstrates JUnit Jupiter-based testing | Demonstrates JUnit Jupiter-based testing
| link:spring-boot-sample-kafka[spring-boot-sample-kafka]
| consumer and producer using Apache Kafka
| link:spring-boot-sample-liquibase[spring-boot-sample-liquibase] | link:spring-boot-sample-liquibase[spring-boot-sample-liquibase]
| Database migrations with Liquibase | Database migrations with Liquibase
@ -230,6 +233,3 @@ The following sample applications are provided:
| link:spring-boot-sample-xml[spring-boot-sample-xml] | link:spring-boot-sample-xml[spring-boot-sample-xml]
| Example show how Spring Boot can be mixed with traditional XML configuration (we | Example show how Spring Boot can be mixed with traditional XML configuration (we
generally recommend using Java `@Configuration` whenever possible generally recommend using Java `@Configuration` whenever possible
| link:spring-boot-sample-kafka[spring-boot-sample-kafka]
| consumer and producer using Apache Kafka

View File

@ -57,6 +57,7 @@
<module>spring-boot-sample-jta-narayana</module> <module>spring-boot-sample-jta-narayana</module>
<module>spring-boot-sample-jta-jndi</module> <module>spring-boot-sample-jta-jndi</module>
<module>spring-boot-sample-junit-jupiter</module> <module>spring-boot-sample-junit-jupiter</module>
<module>spring-boot-sample-kafka</module>
<module>spring-boot-sample-liquibase</module> <module>spring-boot-sample-liquibase</module>
<module>spring-boot-sample-logback</module> <module>spring-boot-sample-logback</module>
<module>spring-boot-sample-oauth2-client</module> <module>spring-boot-sample-oauth2-client</module>
@ -97,7 +98,6 @@
<module>spring-boot-sample-websocket-undertow</module> <module>spring-boot-sample-websocket-undertow</module>
<module>spring-boot-sample-webservices</module> <module>spring-boot-sample-webservices</module>
<module>spring-boot-sample-xml</module> <module>spring-boot-sample-xml</module>
<module>spring-boot-sample-kafka</module>
</modules> </modules>
<!-- No dependencies - otherwise the samples won't work if you change the <!-- No dependencies - otherwise the samples won't work if you change the
parent --> parent -->

View File

@ -20,16 +20,12 @@
<!-- Compile --> <!-- Compile -->
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId> <artifactId>spring-boot-starter-json</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.kafka</groupId> <groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId> <artifactId>spring-kafka</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
</dependency>
<!-- Test --> <!-- Test -->
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
@ -39,6 +35,7 @@
<dependency> <dependency>
<groupId>org.springframework.kafka</groupId> <groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka-test</artifactId> <artifactId>spring-kafka-test</artifactId>
<scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>

View File

@ -19,10 +19,11 @@ import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@Component @Component
public class Consumer { class Consumer {
@KafkaListener(topics = "myTopic") @KafkaListener(topics = "testTopic")
public void processMessage(SampleMessage message) { public void processMessage(SampleMessage message) {
System.out.println("consumer has received message : [" + message + "]"); System.out.println("Received sample message [" + message + "]");
} }
} }

View File

@ -15,18 +15,21 @@
*/ */
package sample.kafka; package sample.kafka;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate; import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@Component @Component
public class Producer { public class Producer {
@Autowired private final KafkaTemplate<Object, SampleMessage> kafkaTemplate;
private KafkaTemplate kafkaTemplate;
Producer(KafkaTemplate<Object, SampleMessage> kafkaTemplate) {
this.kafkaTemplate = kafkaTemplate;
}
public void send(SampleMessage message) { public void send(SampleMessage message) {
kafkaTemplate.send("myTopic", message); this.kafkaTemplate.send("testTopic", message);
System.out.println("producer has sent message."); System.out.println("Sent sample message [" + message + "]");
} }
} }

View File

@ -15,8 +15,11 @@
*/ */
package sample.kafka; package sample.kafka;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.Bean;
@SpringBootApplication @SpringBootApplication
public class SampleKafkaApplication { public class SampleKafkaApplication {
@ -24,4 +27,10 @@ public class SampleKafkaApplication {
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(SampleKafkaApplication.class, args); SpringApplication.run(SampleKafkaApplication.class, args);
} }
@Bean
public ApplicationRunner runner(Producer producer) {
return args -> producer.send(new SampleMessage(1, "A simple test message"));
}
} }

View File

@ -15,39 +15,37 @@
*/ */
package sample.kafka; package sample.kafka;
public class SampleMessage { import com.fasterxml.jackson.annotation.JsonCreator;
private Integer id; import com.fasterxml.jackson.annotation.JsonProperty;
private String message;
public Integer getId() { public class SampleMessage {
return id;
private final Integer id;
private final String message;
@JsonCreator
public SampleMessage(@JsonProperty("id") Integer id,
@JsonProperty("message") String message) {
this.id = id;
this.message = message;
} }
public void setId(Integer id) { public Integer getId() {
this.id = id; return this.id;
} }
public String getMessage() { public String getMessage() {
return message; return this.message;
}
public void setMessage(String message) {
this.message = message;
}
public SampleMessage() {
}
public SampleMessage(Integer id, String message) {
this.id = id;
this.message = message;
} }
@Override @Override
public String toString() { public String toString() {
return "SampleMessage{" + final StringBuilder sb = new StringBuilder("SampleMessage{");
"id=" + id + sb.append("id=").append(this.id);
", message='" + message + '\'' + sb.append(", message='").append(this.message).append('\'');
'}'; sb.append('}');
return sb.toString();
} }
} }

View File

@ -1,5 +1,6 @@
spring.kafka.bootstrap-servers=localhost:9092 spring.kafka.bootstrap-servers=localhost:9092
spring.kafka.consumer.group-id=myGroup spring.kafka.consumer.group-id=testGroup
spring.kafka.consumer.auto-offset-reset=earliest
spring.kafka.consumer.value-deserializer=org.springframework.kafka.support.serializer.JsonDeserializer spring.kafka.consumer.value-deserializer=org.springframework.kafka.support.serializer.JsonDeserializer
spring.kafka.Producer.value-serializer=org.springframework.kafka.support.serializer.JsonSerializer spring.kafka.producer.value-serializer=org.springframework.kafka.support.serializer.JsonSerializer
spring.kafka.consumer.properties.spring.json.trusted.packages=sample.kafka spring.kafka.consumer.properties.spring.json.trusted.packages=sample.kafka

View File

@ -15,13 +15,20 @@
*/ */
package sample.kafka; package sample.kafka;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.test.rule.OutputCapture; import org.springframework.boot.test.rule.OutputCapture;
import org.springframework.kafka.test.rule.KafkaEmbedded; import org.springframework.context.annotation.Bean;
import org.springframework.kafka.test.context.EmbeddedKafka;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -30,32 +37,41 @@ import static org.assertj.core.api.Assertions.assertThat;
* Integration tests for demo application. * Integration tests for demo application.
* *
* @author hcxin * @author hcxin
* @author Gary Russell
* @author Stephane Nicoll
*/ */
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@SpringBootTest @SpringBootTest
@TestPropertySource(properties = "spring.kafka.bootstrap-servers=${spring.embedded.kafka.brokers}")
@EmbeddedKafka
public class SampleKafkaApplicationTests { public class SampleKafkaApplicationTests {
private static final CountDownLatch latch = new CountDownLatch(1);
@Rule @Rule
public OutputCapture outputCapture = new OutputCapture(); public OutputCapture outputCapture = new OutputCapture();
@Autowired
private Producer producer;
@Test @Test
public void sendSimpleMessage() throws Exception { public void testVanillaExchange() throws Exception {
initKafkaEmbedded(); assertThat(latch.await(10, TimeUnit.SECONDS)).isTrue();
SampleMessage message = new SampleMessage(1, "Test message"); assertThat(this.outputCapture.toString().contains("A simple test message"))
producer.send(message); .isTrue();
Thread.sleep(1000L);
assertThat(this.outputCapture.toString().contains("Test message")).isTrue();
} }
public void initKafkaEmbedded() throws Exception { @TestConfiguration
KafkaEmbedded embeddedKafka = new KafkaEmbedded(1, true); public static class Config {
embeddedKafka.setKafkaPorts(9092);
embeddedKafka.afterPropertiesSet(); @Bean
//Need 10s, waiting for the Kafka server start. public Consumer consumer() {
Thread.sleep(10000L); return new Consumer() {
@Override
public void processMessage(SampleMessage message) {
super.processMessage(message);
latch.countDown();
}
};
}
} }
} }