authorUuidList = subscribeRepository.findByAuthorUuid(kafkaFeedRequestDto.getUuid());
List feed 서비스의 consumer 역할을 하는 구독 service에서
feed 작성자의 정보에 그 feed 작성자에 대하여 게시글 알람 신청을 한 user들에 대한 정보를 통합 통합된 정보를 다시 한번 kafka 메세지로 notification(알림) 서비스에 정보를 보내는 producer 역할을 하도록 설정 보내는 topic은 “feed-create-join-subscribe” 보내는 정보는 미디어에 대한 정보를 넘기고 있으나 현재 notification service에서는 미디어를 따로 받지는 않음 ← type 정보를 통하여 분기 처리@KafkaListener(topics = "feed-create"
, groupId = "feed-join-subscribe"
, containerFactory = "feedEventListenerContainerFactory")
public void consumeFeedEvent(FeedKafkaRequestDto kafkaFeedRequestDto) {
log.info("consumeFeedEvent: {}", kafkaFeedRequestDto.getContent());
List<Subscribe> authorUuidList = subscribeRepository.findByAuthorUuid(kafkaFeedRequestDto.getUuid());
List<String> receiverUuidList = authorUuidList
.stream()
.map(Subscribe::getSubscriberUuid)
.toList();
String splitedContent = kafkaFeedRequestDto.getContent().length() > 20 ? kafkaFeedRequestDto.getContent().substring(0, 20) + "..." : kafkaFeedRequestDto.getContent();
//AlarmKafkaRequestDto kafkaAlarmRequestDto = AlarmKafkaRequestDto.toDto(kafkaFeedRequestDto, receiverUuidList, splitedContent, TYPE);
NotificationKafkaRequestDto notificationKafkaRequestDto = NotificationKafkaRequestDto.toDto(kafkaFeedRequestDto, receiverUuidList,
splitedContent, TYPE);
sendMessage("feed-create-join-subscribe", notificationKafkaRequestDto);
}
public void sendMessage(String topic, NotificationKafkaRequestDto kafkaAlarmRequestDto) {
kafkaTemplate.send(topic, kafkaAlarmRequestDto);
}
@Getter
@NoArgsConstructor
public class NotificationKafkaRequestDto {
private String senderUuid;
private List<String> receiverUuidList;
private String content;
private String mediaUrl;
private String type;
@Builder
public NotificationKafkaRequestDto(
String senderUuid,
List<String> receiverUuidList,
String content,
String mediaUrl,
String type
) {
this.senderUuid = senderUuid;
this.receiverUuidList = receiverUuidList;
this.content = content;
this.mediaUrl = mediaUrl;
this.type = type;
}
public static NotificationKafkaRequestDto toDto(FeedKafkaRequestDto kafkaFeedRequestDto, List<String> receiverUuidList, String splitedContent, String type) {
return NotificationKafkaRequestDto.builder()
.senderUuid(kafkaFeedRequestDto.getUuid())
.receiverUuidList(receiverUuidList)
.content(splitedContent)
.mediaUrl(kafkaFeedRequestDto.getMediaUrlList().get(0))
.type(type)
.build();
}
}