-
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.
Merge pull request #36 from lotteon2/develop
release
- Loading branch information
Showing
7 changed files
with
165 additions
and
15 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
src/main/java/com/dailyon/notificationservice/config/NotificationConfig.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 |
---|---|---|
@@ -1,7 +1,14 @@ | ||
package com.dailyon.notificationservice.config; | ||
|
||
|
||
import java.util.UUID; | ||
|
||
public class NotificationConfig { | ||
public static final String NOTIFICATIONS_STREAM_KEY = "notifications:stream"; | ||
public static final Long NOTIFICATION_STREAM_TTL = 5 * 60 * 1000L; // ms단위 5분 | ||
|
||
public static final String MEMBER_NOTIFICATION_CONNECTION_CHANNEL = "memberNotificationConnection"; | ||
|
||
public static final String CONSUMER_GROUP_NAME = "notification-group"; | ||
public static final String UNIQUE_CONSUMER_IDENTIFIER = CONSUMER_GROUP_NAME + "-" + UUID.randomUUID(); | ||
} |
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
25 changes: 25 additions & 0 deletions
25
src/main/java/com/dailyon/notificationservice/domain/notification/dto/DisconnectInfoDto.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,25 @@ | ||
package com.dailyon.notificationservice.domain.notification.dto; | ||
|
||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
import static com.dailyon.notificationservice.config.NotificationConfig.UNIQUE_CONSUMER_IDENTIFIER; | ||
|
||
@Builder | ||
@Data | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class DisconnectInfoDto { | ||
private Long memberId; | ||
private String uniqueConsumerName; | ||
|
||
public static DisconnectInfoDto create(Long memberId) { | ||
return DisconnectInfoDto.builder() | ||
.memberId(memberId) | ||
.uniqueConsumerName(UNIQUE_CONSUMER_IDENTIFIER) | ||
.build(); | ||
} | ||
} |
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
79 changes: 79 additions & 0 deletions
79
.../java/com/dailyon/notificationservice/domain/notification/service/RedisPubSubService.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,79 @@ | ||
package com.dailyon.notificationservice.domain.notification.service; | ||
|
||
import com.dailyon.notificationservice.domain.notification.dto.DisconnectInfoDto; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.data.redis.connection.ReactiveSubscription; | ||
import org.springframework.data.redis.core.ReactiveRedisTemplate; | ||
import org.springframework.data.redis.listener.ChannelTopic; | ||
import org.springframework.stereotype.Service; | ||
import reactor.core.publisher.Mono; | ||
|
||
import java.util.Objects; | ||
|
||
import static com.dailyon.notificationservice.config.NotificationConfig.MEMBER_NOTIFICATION_CONNECTION_CHANNEL; | ||
import static com.dailyon.notificationservice.config.NotificationConfig.UNIQUE_CONSUMER_IDENTIFIER; | ||
|
||
|
||
@Slf4j | ||
@Service | ||
public class RedisPubSubService { | ||
|
||
private final ReactiveRedisTemplate<String, String> reactiveRedisTemplate; | ||
private final SseNotificationService sseNotificationService; | ||
private final ObjectMapper objectMapper; | ||
|
||
public RedisPubSubService( | ||
ReactiveRedisTemplate<String, String> reactiveRedisTemplate, | ||
SseNotificationService sseNotificationService, | ||
ObjectMapper objectMapper) { | ||
this.reactiveRedisTemplate = Objects.requireNonNull(reactiveRedisTemplate); | ||
this.sseNotificationService = Objects.requireNonNull(sseNotificationService); | ||
this.objectMapper = Objects.requireNonNull(objectMapper); | ||
|
||
// 빈 생성하면서 구독 시작 | ||
log.info("RedisPubSubService 빈을 생성하면서 구독 시작합니다."); | ||
subscribeToMemberConnectionChannel(); | ||
} | ||
|
||
private void subscribeToMemberConnectionChannel() { | ||
log.info("redis sub 시작합니다."); | ||
reactiveRedisTemplate.listenTo(ChannelTopic.of(MEMBER_NOTIFICATION_CONNECTION_CHANNEL)) | ||
.map(ReactiveSubscription.Message::getMessage) | ||
.doOnNext(this::handleMemberDisconnection) | ||
.subscribe(); | ||
// .subscribe(this::handleMemberDisconnection); | ||
} | ||
|
||
private void handleMemberDisconnection(String disconnectInfoDtoJson) { | ||
try { | ||
DisconnectInfoDto disconnectInfoDto = objectMapper.readValue(disconnectInfoDtoJson, DisconnectInfoDto.class); | ||
if (disconnectInfoDto.getUniqueConsumerName().equals(UNIQUE_CONSUMER_IDENTIFIER)) { | ||
log.info("동일 인스턴스에서 보낸 연결해제 pub입니다."); | ||
return; | ||
} | ||
|
||
Long memberIdToDisconnect = disconnectInfoDto.getMemberId(); | ||
|
||
if (sseNotificationService.isUserConnected(memberIdToDisconnect)) { | ||
log.info(memberIdToDisconnect + "의 연결을 해제합니다."); | ||
sseNotificationService.disconnectMember(memberIdToDisconnect); | ||
} | ||
} catch (JsonProcessingException e) { | ||
log.error("handleMemberDisconnection의 readValue 도중 에러발생", e); | ||
} | ||
} | ||
|
||
public Mono<Void> publishMemberConnection(Long memberId) { | ||
log.info(memberId + "접속. disconnect 메세지 발행합니다."); | ||
DisconnectInfoDto disconnectInfoDto = DisconnectInfoDto.create(memberId); | ||
|
||
return Mono.fromCallable(() -> objectMapper.writeValueAsString(disconnectInfoDto)) | ||
.flatMap(jsonData -> reactiveRedisTemplate.convertAndSend(MEMBER_NOTIFICATION_CONNECTION_CHANNEL, jsonData)) | ||
.doOnError(e -> { | ||
log.error("publishMemberConnection 도중 에러발생", e); | ||
}) | ||
.then(); | ||
} | ||
} |
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