Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
R
ReactiveKafkawithMongo
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
Potharaju Peddi
ReactiveKafkawithMongo
Commits
00ce5455
Commit
00ce5455
authored
Apr 19, 2023
by
Potharaju Peddi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
for webflux reactive kafka with mongoDB
parent
cd6e981e
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
48 additions
and
5 deletions
+48
-5
StudentController.java
...uxReactiveMongo/Reactor/Controller/StudentController.java
+46
-0
SampleProducer.java
...om/WebFluxReactiveMongo/Reactor/kafka/SampleProducer.java
+2
-5
No files found.
src/main/java/com/WebFluxReactiveMongo/Reactor/Controller/StudentController.java
View file @
00ce5455
...
@@ -2,11 +2,22 @@ package com.WebFluxReactiveMongo.Reactor.Controller;
...
@@ -2,11 +2,22 @@ package com.WebFluxReactiveMongo.Reactor.Controller;
import
com.WebFluxReactiveMongo.Reactor.Dto.StudentDto
;
import
com.WebFluxReactiveMongo.Reactor.Dto.StudentDto
;
import
com.WebFluxReactiveMongo.Reactor.Service.StudentService
;
import
com.WebFluxReactiveMongo.Reactor.Service.StudentService
;
import
com.fasterxml.jackson.core.JsonProcessingException
;
import
com.fasterxml.jackson.databind.JsonNode
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
import
org.apache.kafka.clients.producer.ProducerConfig
;
import
org.apache.kafka.clients.producer.ProducerRecord
;
import
org.apache.kafka.common.serialization.StringSerializer
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.kafka.support.serializer.JsonSerializer
;
import
org.springframework.web.bind.annotation.*
;
import
org.springframework.web.bind.annotation.*
;
import
reactor.core.publisher.Flux
;
import
reactor.core.publisher.Flux
;
import
reactor.core.publisher.Mono
;
import
reactor.core.publisher.Mono
;
import
reactor.kafka.sender.KafkaSender
;
import
reactor.kafka.sender.SenderOptions
;
import
java.util.HashMap
;
import
java.util.Map
;
@RestController
@RestController
...
@@ -16,6 +27,22 @@ public class StudentController {
...
@@ -16,6 +27,22 @@ public class StudentController {
@Autowired
@Autowired
private
StudentService
studentService
;
private
StudentService
studentService
;
private
static
final
String
TOPIC
=
"TestTopic1"
;
public
static
KafkaSender
sampleProducer
()
{
Map
<
String
,
Object
>
props
=
new
HashMap
<>();
props
.
put
(
ProducerConfig
.
BOOTSTRAP_SERVERS_CONFIG
,
"localhost:9092"
);
props
.
put
(
ProducerConfig
.
CLIENT_ID_CONFIG
,
"sample-producer"
);
props
.
put
(
ProducerConfig
.
ACKS_CONFIG
,
"all"
);
props
.
put
(
ProducerConfig
.
KEY_SERIALIZER_CLASS_CONFIG
,
StringSerializer
.
class
);
props
.
put
(
ProducerConfig
.
VALUE_SERIALIZER_CLASS_CONFIG
,
JsonSerializer
.
class
);
SenderOptions
<
String
,
JsonNode
>
senderOptions
=
SenderOptions
.
create
(
props
);
return
KafkaSender
.
create
(
senderOptions
);
}
@GetMapping
@GetMapping
public
Flux
<
StudentDto
>
getStudents
(){
public
Flux
<
StudentDto
>
getStudents
(){
return
studentService
.
getStudents
();
return
studentService
.
getStudents
();
...
@@ -52,4 +79,23 @@ public class StudentController {
...
@@ -52,4 +79,23 @@ public class StudentController {
return
studentService
.
deleteStudent
(
id
);
return
studentService
.
deleteStudent
(
id
);
}
}
@GetMapping
(
"/publishReactorJson"
)
public
Mono
<
StudentDto
>
sendMessages
(
@RequestBody
StudentDto
studentDto
)
throws
InterruptedException
,
JsonProcessingException
{
Mono
<
StudentDto
>
stockDTO
=
studentService
.
saveStudent
(
Mono
.
just
(
studentDto
));
return
stockDTO
.
flatMap
(
data
->
sendToKafka
(
data
,
"create"
));
}
private
Mono
<
StudentDto
>
sendToKafka
(
StudentDto
studentDto
,
String
create
)
{
return
StudentController
.
sampleProducer
().
createOutbound
()
.
send
(
Mono
.
just
(
new
ProducerRecord
<>(
"TestTopic1"
,
"dto.getSalt()"
,
studentDto
))).
then
().
log
()
.
doOnError
(
e
->
log
.
error
(
String
.
format
(
"Failed to send topic: %s value: %s"
,
"TestTopic1"
,
studentDto
),
e
))
.
thenReturn
(
studentDto
);
}
}
}
src/main/java/com/WebFluxReactiveMongo/Reactor/kafka/SampleProducer.java
View file @
00ce5455
package
com
.
WebFluxReactiveMongo
.
Reactor
.
kafka
;
package
com
.
WebFluxReactiveMongo
.
Reactor
.
kafka
;
import
com.WebFluxReactiveMongo.Reactor.Dto.StudentDto
;
import
com.WebFluxReactiveMongo.Reactor.Dto.StudentDto
;
import
com.WebFluxReactiveMongo.Reactor.Entity.Student
;
import
com.WebFluxReactiveMongo.Reactor.Repository.StudentRepository
;
import
com.WebFluxReactiveMongo.Reactor.Repository.StudentRepository
;
import
com.WebFluxReactiveMongo.Reactor.Service.StudentService
;
import
com.WebFluxReactiveMongo.Reactor.Service.StudentService
;
import
com.fasterxml.jackson.core.JsonProcessingException
;
import
com.fasterxml.jackson.core.JsonProcessingException
;
...
@@ -55,15 +56,11 @@ public class SampleProducer {
...
@@ -55,15 +56,11 @@ public class SampleProducer {
public
void
sendMessages
(
String
topic
,
int
count
,
CountDownLatch
latch
)
throws
InterruptedException
{
public
void
sendMessages
(
String
topic
,
int
count
,
CountDownLatch
latch
)
throws
InterruptedException
{
ObjectMapper
objectMapper
=
new
ObjectMapper
();
ObjectMapper
objectMapper
=
new
ObjectMapper
();
//Flux<StudentDto> studentDto = Flux.just(new StudentDto("106","AADHI","DIYA",50000),
//new StudentDto("107","VASU","KIYA",45000));
sender
.<
Integer
>
send
(
Flux
.
range
(
1
,
1
)
sender
.<
Integer
>
send
(
Flux
.
range
(
1
,
1
)
//.map(i -> SenderRecord.create(new ProducerRecord<>(topic, i, "Message_" + i), i)))
.
map
(
i
->
{
.
map
(
i
->
{
JsonNode
jsonValue
=
null
;
JsonNode
jsonValue
=
null
;
try
{
try
{
Mono
<
StudentDto
>
studentDtoMono
=
Mono
.
just
(
new
StudentDto
(
"5855"
,
"Kafka Producer message"
,
"Nisum School"
,
60000
));
String
value
=
objectMapper
.
writeValueAsString
(
new
Student
(
"5855"
,
"Kafka Producer message"
,
"Nisum School"
,
60000
));
String
value
=
objectMapper
.
writeValueAsString
(
studentDtoMono
);
jsonValue
=
objectMapper
.
readTree
(
value
);
jsonValue
=
objectMapper
.
readTree
(
value
);
}
catch
(
JsonProcessingException
e
)
{
}
catch
(
JsonProcessingException
e
)
{
...
...
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