Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
springboot-demo
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Leona
W
web
backend
springboot-demo
Commits
0fc74f13
Commit
0fc74f13
authored
Oct 21, 2019
by
duanledexianxianxian
😁
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
dataStream api
parent
1ae838fa
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
59 additions
and
19 deletions
+59
-19
kafka/pom.xml
kafka/pom.xml
+6
-1
kafka/src/main/java/com/duanledexianxianxian/demo/kafka/consumer/TestConsumer.java
...uanledexianxianxian/demo/kafka/consumer/TestConsumer.java
+1
-1
kafka/src/main/java/com/duanledexianxianxian/demo/kafka/listener/AppListener.java
...duanledexianxianxian/demo/kafka/listener/AppListener.java
+36
-16
kafka/src/main/java/com/duanledexianxianxian/demo/kafka/model/Student.java
...va/com/duanledexianxianxian/demo/kafka/model/Student.java
+16
-1
No files found.
kafka/pom.xml
View file @
0fc74f13
...
@@ -32,5 +32,10 @@
...
@@ -32,5 +32,10 @@
<artifactId>
spring-kafka-test
</artifactId>
<artifactId>
spring-kafka-test
</artifactId>
<scope>
test
</scope>
<scope>
test
</scope>
</dependency>
</dependency>
<dependency>
<groupId>
org.apache.commons
</groupId>
<artifactId>
commons-lang3
</artifactId>
<version>
3.9
</version>
</dependency>
</dependencies>
</dependencies>
</project>
</project>
kafka/src/main/java/com/duanledexianxianxian/demo/kafka/consumer/TestConsumer.java
View file @
0fc74f13
...
@@ -10,7 +10,7 @@ import org.springframework.stereotype.Component;
...
@@ -10,7 +10,7 @@ import org.springframework.stereotype.Component;
* @author fengyuchenglun
* @author fengyuchenglun
* @version 1.0.0
* @version 1.0.0
*/
*/
@Component
//
@Component
public
class
TestConsumer
{
public
class
TestConsumer
{
/**
/**
...
...
kafka/src/main/java/com/duanledexianxianxian/demo/kafka/listener/AppListener.java
View file @
0fc74f13
...
@@ -3,6 +3,10 @@ package com.duanledexianxianxian.demo.kafka.listener;
...
@@ -3,6 +3,10 @@ package com.duanledexianxianxian.demo.kafka.listener;
import
com.alibaba.fastjson.JSONObject
;
import
com.alibaba.fastjson.JSONObject
;
import
com.duanledexianxianxian.demo.kafka.model.Student
;
import
com.duanledexianxianxian.demo.kafka.model.Student
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
import
org.apache.commons.lang3.RandomStringUtils
;
import
org.apache.commons.lang3.RandomUtils
;
import
org.apache.commons.lang3.concurrent.BasicThreadFactory
;
import
org.apache.tomcat.util.threads.ThreadPoolExecutor
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.context.event.ApplicationReadyEvent
;
import
org.springframework.boot.context.event.ApplicationReadyEvent
;
import
org.springframework.context.ApplicationListener
;
import
org.springframework.context.ApplicationListener
;
...
@@ -11,6 +15,7 @@ import org.springframework.stereotype.Component;
...
@@ -11,6 +15,7 @@ import org.springframework.stereotype.Component;
import
java.time.LocalDateTime
;
import
java.time.LocalDateTime
;
import
java.util.Random
;
import
java.util.Random
;
import
java.util.concurrent.*
;
/**
/**
* @author duanledexianxianxian
* @author duanledexianxianxian
...
@@ -23,24 +28,39 @@ public class AppListener implements ApplicationListener<ApplicationReadyEvent> {
...
@@ -23,24 +28,39 @@ public class AppListener implements ApplicationListener<ApplicationReadyEvent> {
@Autowired
@Autowired
private
KafkaTemplate
<
String
,
Object
>
kafkaTemplate
;
private
KafkaTemplate
<
String
,
Object
>
kafkaTemplate
;
@Override
@Override
public
void
onApplicationEvent
(
ApplicationReadyEvent
applicationReadyEvent
)
{
public
void
onApplicationEvent
(
ApplicationReadyEvent
applicationReadyEvent
)
{
//labdam方式
new
Thread
(()
->
{
ThreadFactory
namedThreadFactory
=
new
BasicThreadFactory
.
Builder
().
namingPattern
(
"producer-kafka-pool-%d"
).
daemon
(
true
).
build
();
log
.
info
(
Thread
.
currentThread
().
getName
()
+
":使用lambda表达式创建线程"
);
ExecutorService
pool
=
new
ThreadPoolExecutor
(
5
,
200
,
0L
,
TimeUnit
.
MILLISECONDS
,
new
LinkedBlockingQueue
<
Runnable
>(
1024
),
namedThreadFactory
,
new
ThreadPoolExecutor
.
AbortPolicy
());
int
i
=
0
;
while
(
true
)
{
while
(
true
)
{
Student
student
=
new
Student
()
;
int
finalI
=
i
;
student
.
setStudentId
(
String
.
valueOf
(
LocalDateTime
.
now
().
getNano
()));
pool
.
execute
(()
->
{
student
.
setName
(
Thread
.
currentThread
().
getName
()
);
this
.
sendData
(
finalI
);
student
.
setAge
(
String
.
valueOf
(
new
Random
().
nextInt
()));
// sleep 5 seconds
log
.
info
(
"Send Data:{}"
,
student
);
}
);
kafkaTemplate
.
send
(
"topic_first"
,
JSONObject
.
toJSONString
(
student
));
try
{
try
{
Thread
.
sleep
(
5000
);
Thread
.
sleep
(
5000
);
}
catch
(
InterruptedException
e
)
{
}
catch
(
InterruptedException
e
)
{
e
.
printStackTrace
();
e
.
printStackTrace
();
}
}
i
++;
}
}
}
}).
start
();
private
void
sendData
(
int
i
)
{
Student
student
=
new
Student
();
student
.
setStudentId
(
"duanledexianxianxian"
+
i
);
student
.
setName
(
RandomStringUtils
.
randomAlphabetic
(
10
));
student
.
setAge
(
new
Random
().
nextInt
(
19
)
+
6
);
student
.
setSex
(
Byte
.
valueOf
(
String
.
valueOf
((
new
Random
().
nextInt
(
2
)))));
log
.
info
(
"Thread:{} Send Data:{}"
,
Thread
.
currentThread
().
getName
(),
student
);
kafkaTemplate
.
send
(
"topic_first"
,
JSONObject
.
toJSONString
(
student
));
}
}
}
}
kafka/src/main/java/com/duanledexianxianxian/demo/kafka/model/Student.java
View file @
0fc74f13
...
@@ -14,7 +14,22 @@ import java.io.Serializable;
...
@@ -14,7 +14,22 @@ import java.io.Serializable;
@Data
@Data
public
class
Student
implements
Serializable
{
public
class
Student
implements
Serializable
{
private
static
final
long
serialVersionUID
=
-
4572819276161640638L
;
private
static
final
long
serialVersionUID
=
-
4572819276161640638L
;
/**
* 学生学号
*/
private
String
studentId
;
private
String
studentId
;
/**
* 学生姓名
*/
private
String
name
;
private
String
name
;
private
String
age
;
/**
* 学生年龄
*/
private
Integer
age
;
/**
* 学生性别
* 0-男
* 1-女
*/
private
Byte
sex
;
}
}
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