Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
dd989e58
Commit
dd989e58
authored
Feb 08, 2018
by
Stephane Nicoll
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #11597 from hcxin:master
* pr/11597: Polish "Add Kafka sample" Add Kafka sample
parents
7b32cf82
07fa8bcf
Changes
9
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
288 additions
and
0 deletions
+288
-0
README.adoc
spring-boot-samples/README.adoc
+3
-0
pom.xml
spring-boot-samples/pom.xml
+1
-0
pom.xml
spring-boot-samples/spring-boot-sample-kafka/pom.xml
+50
-0
Consumer.java
...oot-sample-kafka/src/main/java/sample/kafka/Consumer.java
+29
-0
Producer.java
...oot-sample-kafka/src/main/java/sample/kafka/Producer.java
+35
-0
SampleKafkaApplication.java
...ka/src/main/java/sample/kafka/SampleKafkaApplication.java
+36
-0
SampleMessage.java
...ample-kafka/src/main/java/sample/kafka/SampleMessage.java
+51
-0
application.properties
...ot-sample-kafka/src/main/resources/application.properties
+6
-0
SampleKafkaApplicationTests.java
...c/test/java/sample/kafka/SampleKafkaApplicationTests.java
+77
-0
No files found.
spring-boot-samples/README.adoc
View file @
dd989e58
...
...
@@ -119,6 +119,9 @@ The following sample applications are provided:
| link:spring-boot-sample-junit-jupiter[spring-boot-sample-junit-jupiter]
| 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]
| Database migrations with Liquibase
...
...
spring-boot-samples/pom.xml
View file @
dd989e58
...
...
@@ -57,6 +57,7 @@
<module>
spring-boot-sample-jta-narayana
</module>
<module>
spring-boot-sample-jta-jndi
</module>
<module>
spring-boot-sample-junit-jupiter
</module>
<module>
spring-boot-sample-kafka
</module>
<module>
spring-boot-sample-liquibase
</module>
<module>
spring-boot-sample-logback
</module>
<module>
spring-boot-sample-oauth2-client
</module>
...
...
spring-boot-samples/spring-boot-sample-kafka/pom.xml
0 → 100644
View file @
dd989e58
<?xml version="1.0" encoding="UTF-8"?>
<project
xmlns=
"http://maven.apache.org/POM/4.0.0"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<modelVersion>
4.0.0
</modelVersion>
<parent>
<!-- Your own application should inherit from spring-boot-starter-parent -->
<artifactId>
spring-boot-samples
</artifactId>
<groupId>
org.springframework.boot
</groupId>
<version>
${revision}
</version>
</parent>
<artifactId>
spring-boot-sample-kafka
</artifactId>
<name>
Spring Boot Kafka Sample
</name>
<description>
Spring Boot Kafka Sample
</description>
<properties>
<main.basedir>
${basedir}/../..
</main.basedir>
</properties>
<dependencies>
<!-- Compile -->
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-json
</artifactId>
</dependency>
<dependency>
<groupId>
org.springframework.kafka
</groupId>
<artifactId>
spring-kafka
</artifactId>
</dependency>
<!-- Test -->
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-test
</artifactId>
<scope>
test
</scope>
</dependency>
<dependency>
<groupId>
org.springframework.kafka
</groupId>
<artifactId>
spring-kafka-test
</artifactId>
<scope>
test
</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-maven-plugin
</artifactId>
</plugin>
</plugins>
</build>
</project>
spring-boot-samples/spring-boot-sample-kafka/src/main/java/sample/kafka/Consumer.java
0 → 100644
View file @
dd989e58
/*
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
sample
.
kafka
;
import
org.springframework.kafka.annotation.KafkaListener
;
import
org.springframework.stereotype.Component
;
@Component
class
Consumer
{
@KafkaListener
(
topics
=
"testTopic"
)
public
void
processMessage
(
SampleMessage
message
)
{
System
.
out
.
println
(
"Received sample message ["
+
message
+
"]"
);
}
}
\ No newline at end of file
spring-boot-samples/spring-boot-sample-kafka/src/main/java/sample/kafka/Producer.java
0 → 100644
View file @
dd989e58
/*
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
sample
.
kafka
;
import
org.springframework.kafka.core.KafkaTemplate
;
import
org.springframework.stereotype.Component
;
@Component
public
class
Producer
{
private
final
KafkaTemplate
<
Object
,
SampleMessage
>
kafkaTemplate
;
Producer
(
KafkaTemplate
<
Object
,
SampleMessage
>
kafkaTemplate
)
{
this
.
kafkaTemplate
=
kafkaTemplate
;
}
public
void
send
(
SampleMessage
message
)
{
this
.
kafkaTemplate
.
send
(
"testTopic"
,
message
);
System
.
out
.
println
(
"Sent sample message ["
+
message
+
"]"
);
}
}
\ No newline at end of file
spring-boot-samples/spring-boot-sample-kafka/src/main/java/sample/kafka/SampleKafkaApplication.java
0 → 100644
View file @
dd989e58
/*
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
sample
.
kafka
;
import
org.springframework.boot.ApplicationRunner
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.autoconfigure.SpringBootApplication
;
import
org.springframework.boot.builder.SpringApplicationBuilder
;
import
org.springframework.context.annotation.Bean
;
@SpringBootApplication
public
class
SampleKafkaApplication
{
public
static
void
main
(
String
[]
args
)
{
SpringApplication
.
run
(
SampleKafkaApplication
.
class
,
args
);
}
@Bean
public
ApplicationRunner
runner
(
Producer
producer
)
{
return
args
->
producer
.
send
(
new
SampleMessage
(
1
,
"A simple test message"
));
}
}
spring-boot-samples/spring-boot-sample-kafka/src/main/java/sample/kafka/SampleMessage.java
0 → 100644
View file @
dd989e58
/*
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
sample
.
kafka
;
import
com.fasterxml.jackson.annotation.JsonCreator
;
import
com.fasterxml.jackson.annotation.JsonProperty
;
public
class
SampleMessage
{
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
Integer
getId
()
{
return
this
.
id
;
}
public
String
getMessage
()
{
return
this
.
message
;
}
@Override
public
String
toString
()
{
final
StringBuilder
sb
=
new
StringBuilder
(
"SampleMessage{"
);
sb
.
append
(
"id="
).
append
(
this
.
id
);
sb
.
append
(
", message='"
).
append
(
this
.
message
).
append
(
'\''
);
sb
.
append
(
'}'
);
return
sb
.
toString
();
}
}
spring-boot-samples/spring-boot-sample-kafka/src/main/resources/application.properties
0 → 100644
View file @
dd989e58
spring.kafka.bootstrap-servers
=
localhost:9092
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.producer.value-serializer
=
org.springframework.kafka.support.serializer.JsonSerializer
spring.kafka.consumer.properties.spring.json.trusted.packages
=
sample.kafka
\ No newline at end of file
spring-boot-samples/spring-boot-sample-kafka/src/test/java/sample/kafka/SampleKafkaApplicationTests.java
0 → 100644
View file @
dd989e58
/*
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
sample
.
kafka
;
import
java.util.concurrent.CountDownLatch
;
import
java.util.concurrent.TimeUnit
;
import
org.junit.Rule
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.boot.test.context.TestConfiguration
;
import
org.springframework.boot.test.rule.OutputCapture
;
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
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
/**
* Integration tests for demo application.
*
* @author hcxin
* @author Gary Russell
* @author Stephane Nicoll
*/
@RunWith
(
SpringRunner
.
class
)
@SpringBootTest
@TestPropertySource
(
properties
=
"spring.kafka.bootstrap-servers=${spring.embedded.kafka.brokers}"
)
@EmbeddedKafka
public
class
SampleKafkaApplicationTests
{
private
static
final
CountDownLatch
latch
=
new
CountDownLatch
(
1
);
@Rule
public
OutputCapture
outputCapture
=
new
OutputCapture
();
@Test
public
void
testVanillaExchange
()
throws
Exception
{
assertThat
(
latch
.
await
(
10
,
TimeUnit
.
SECONDS
)).
isTrue
();
assertThat
(
this
.
outputCapture
.
toString
().
contains
(
"A simple test message"
))
.
isTrue
();
}
@TestConfiguration
public
static
class
Config
{
@Bean
public
Consumer
consumer
()
{
return
new
Consumer
()
{
@Override
public
void
processMessage
(
SampleMessage
message
)
{
super
.
processMessage
(
message
);
latch
.
countDown
();
}
};
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment