-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: add separate class for send messages to kafka
- Loading branch information
1 parent
2818aec
commit 1566de2
Showing
3 changed files
with
35 additions
and
6 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
order-service/src/main/java/by/devtools/order/service/impl/KafkaProducer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package by.devtools.order.service.impl; | ||
|
||
import by.devtools.order.util.JsonUtil; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.kafka.core.KafkaTemplate; | ||
import org.springframework.kafka.support.SendResult; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
|
||
@Slf4j | ||
@Service | ||
@RequiredArgsConstructor | ||
public class KafkaProducer { | ||
|
||
private final KafkaTemplate<Integer, String> kafkaTemplate; | ||
|
||
public <T> void sendMessage(String topic, T payload) { | ||
CompletableFuture<SendResult<Integer, String>> future = kafkaTemplate.send(topic, JsonUtil.toJson(payload)); | ||
future.whenComplete((result, ex) -> { | ||
if (ex == null) { | ||
log.info("Successfully sent message={} to topic={}", payload, topic); | ||
} else { | ||
log.warn("Error occurred when sending message={} to topic={}. Exception message: {} ", | ||
payload, topic, ex.getMessage()); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters