Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
K
Kafka and reactive spring boot program
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
Ramakrushna Sahoo
Kafka and reactive spring boot program
Commits
4c7a2e14
Commit
4c7a2e14
authored
Oct 10, 2024
by
Ramakrushna Sahoo
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Initial commit
parent
6c0abe6d
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
56 additions
and
12 deletions
+56
-12
git_log.txt
git_log.txt
+5
-0
EmployeeController.java
src/main/java/com/main/controller/EmployeeController.java
+28
-12
EmployeeProducer.java
src/main/java/com/main/producer/EmployeeProducer.java
+23
-0
No files found.
git_log.txt
0 → 100644
View file @
4c7a2e14
commit 6c0abe6d77f394ecf996d89673fb5052675a0463
Author: Ramakrushna Sahoo <rasahoo@nisum.com>
Date: Wed Oct 9 16:26:34 2024 +0530
initial commit
src/main/java/com/main/controller/EmployeeController.java
View file @
4c7a2e14
...
...
@@ -28,7 +28,20 @@ public class EmployeeController {
@GetMapping
(
"/getEmpById/{id}"
)
public
Mono
<
Employee
>
getEmployeeById
(
@PathVariable
Integer
id
){
return
service
.
getById
(
id
);
Mono
<
Employee
>
mono
=
service
.
getById
(
id
)
.
doOnNext
(
emp
->
{
try
{
producer
.
sendEmployee
(
emp
);
}
catch
(
JsonProcessingException
e
)
{
throw
new
RuntimeException
(
e
);
}
})
.
doOnNext
(
value
->
log
.
info
(
"Employee data get by id successfully: {}"
,
value
))
.
doOnError
(
error
->
log
.
error
(
"Data not found"
,
error
));
return
mono
;
}
@GetMapping
(
"/getAll"
)
...
...
@@ -52,19 +65,22 @@ public class EmployeeController {
.
doOnError
(
error
->
log
.
error
(
"Data not found"
,
error
));
// Collect employees into a list and send to producer
flux
.
collectList
()
.
doOnNext
(
list
->
{
try
{
producer
.
sendEmployee
(
list
);
}
catch
(
JsonProcessingException
e
)
{
throw
new
RuntimeException
(
e
);
}
log
.
info
(
"Employee list sent to producer: {}"
,
list
);
})
.
doOnError
(
error
->
log
.
error
(
"Error while sending employee list: {}"
,
error
))
.
subscribe
();
//
flux.collectList()
//
.doOnNext(list -> {
//
try {
//
producer.sendEmployee(list);
//
} catch (JsonProcessingException e) {
//
throw new RuntimeException(e);
//
}
//
log.info("Employee list sent to producer: {}", list);
//
})
//
.doOnError(error -> log.error("Error while sending employee list: {}", error))
//
.subscribe();
return
flux
;
// Return the Flux for further processing
}
}
src/main/java/com/main/producer/EmployeeProducer.java
View file @
4c7a2e14
...
...
@@ -28,6 +28,9 @@ public class EmployeeProducer {
this
.
objectMapper
=
objectMapper
;
}
public
CompletableFuture
<
SendResult
<
Integer
,
String
>>
sendEmployee
(
List
<
Employee
>
emp
)
throws
JsonProcessingException
{
if
(
emp
.
isEmpty
())
{
throw
new
IllegalArgumentException
(
"Employee list cannot be empty"
);
...
...
@@ -39,6 +42,9 @@ public class EmployeeProducer {
return
sendWithRetry
(
key
,
value
,
3
,
1000
);
// Retry up to 3 times with a backoff of 1000 ms
}
private
CompletableFuture
<
SendResult
<
Integer
,
String
>>
sendWithRetry
(
Integer
key
,
String
value
,
int
retries
,
long
backoff
)
{
return
kafkaTemplate
.
send
(
topic
,
key
,
value
).
handle
((
sendResult
,
throwable
)
->
{
if
(
throwable
!=
null
)
{
...
...
@@ -61,6 +67,23 @@ public class EmployeeProducer {
}
});
}
public
CompletableFuture
<
SendResult
<
Integer
,
String
>>
sendEmployee
(
Employee
emp
)
throws
JsonProcessingException
{
var
key
=
emp
.
getId
();
var
value
=
objectMapper
.
writeValueAsString
(
emp
);
return
kafkaTemplate
.
send
(
topic
,
key
,
value
);
// Retry up to 3 times with a backoff of 1000 ms
}
private
void
handleSuccess
(
Integer
key
,
String
value
)
{
log
.
info
(
"Message sent successfully for the key: {}, value: {} "
,
key
,
value
);
...
...
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