-
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 #75 from f-lab-edu/feature/73-pubsub
[#73] Redis Pub/Sub 구현
- Loading branch information
Showing
16 changed files
with
368 additions
and
71 deletions.
There are no files selected for viewing
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
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/modoospace/alarm/publisher/RedisPublisher.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.modoospace.alarm.publisher; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.modoospace.common.exception.MessageParsingError; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.redis.core.StringRedisTemplate; | ||
import org.springframework.stereotype.Component; | ||
|
||
@RequiredArgsConstructor | ||
@Component | ||
public class RedisPublisher { | ||
|
||
private final ObjectMapper objectMapper; | ||
private final StringRedisTemplate redisTemplate; | ||
|
||
public void publish(String channel, Object data) { | ||
try { | ||
String message = objectMapper.writeValueAsString(data); | ||
redisTemplate.convertAndSend(channel, message); | ||
} catch (JsonProcessingException e) { | ||
throw new MessageParsingError(e.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
30 changes: 30 additions & 0 deletions
30
src/main/java/com/modoospace/alarm/service/RedisMessageService.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 com.modoospace.alarm.service; | ||
|
||
import com.modoospace.alarm.publisher.RedisPublisher; | ||
import com.modoospace.alarm.subscriber.RedisSubscribeListener; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.data.redis.listener.ChannelTopic; | ||
import org.springframework.data.redis.listener.RedisMessageListenerContainer; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class RedisMessageService { | ||
|
||
private final RedisMessageListenerContainer redisMessageListenerContainer; | ||
private final RedisPublisher redisPublisher; | ||
private final RedisSubscribeListener redisSubscribeListener; | ||
|
||
// 채널 구독 | ||
public void subscribe(String channel) { | ||
redisMessageListenerContainer.addMessageListener(redisSubscribeListener, | ||
ChannelTopic.of(channel)); | ||
} | ||
|
||
// 이벤트 발행 | ||
public void publish(String channel, Object message) { | ||
redisPublisher.publish(channel, message); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
src/main/java/com/modoospace/alarm/service/SseEmitterService.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,51 @@ | ||
package com.modoospace.alarm.service; | ||
|
||
import com.modoospace.alarm.repository.EmitterMemoryRepository; | ||
import com.modoospace.common.exception.SSEConnectError; | ||
import java.io.IOException; | ||
import java.util.Optional; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
@Slf4j | ||
public class SseEmitterService { | ||
|
||
@Value("${spring.sse.timeout}") | ||
private Long timeout; | ||
|
||
@Value("${spring.sse.name}") | ||
private String name; | ||
|
||
private final EmitterMemoryRepository emitterRepository; | ||
|
||
public SseEmitter save(String email) { | ||
return emitterRepository.save(email, new SseEmitter(timeout)); | ||
} | ||
|
||
public void delete(String email) { | ||
emitterRepository.delete(email); | ||
} | ||
|
||
public void sendToClient(String email, Object data) { | ||
Optional<SseEmitter> optionalSseEmitter = emitterRepository.find(email); | ||
optionalSseEmitter.ifPresent(sseEmitter -> send(sseEmitter, email, data)); | ||
} | ||
|
||
public void send(SseEmitter emitter, String email, Object data) { | ||
try { | ||
emitter.send(SseEmitter.event() | ||
.id(email) | ||
.name(name) | ||
.data(data)); | ||
log.info("SSE Send Event To: {}", email); | ||
} catch (IOException exception) { | ||
emitterRepository.delete(email); | ||
throw new SSEConnectError(exception.getMessage()); | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/main/java/com/modoospace/alarm/subscriber/RedisSubscribeListener.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,38 @@ | ||
package com.modoospace.alarm.subscriber; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.modoospace.alarm.controller.dto.AlarmResponse; | ||
import com.modoospace.alarm.service.SseEmitterService; | ||
import com.modoospace.common.exception.MessageParsingError; | ||
import java.io.IOException; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.data.redis.connection.Message; | ||
import org.springframework.data.redis.connection.MessageListener; | ||
import org.springframework.stereotype.Component; | ||
|
||
@RequiredArgsConstructor | ||
@Component | ||
@Slf4j | ||
public class RedisSubscribeListener implements MessageListener { | ||
|
||
private final SseEmitterService sseEmitterService; | ||
private final ObjectMapper objectMapper; | ||
|
||
// 채널 구독 객체 | ||
@Override | ||
public void onMessage(Message message, byte[] pattern) { | ||
|
||
try { | ||
String email = new String(message.getChannel()); | ||
AlarmResponse alarmResponse = objectMapper.readValue(message.getBody(), | ||
AlarmResponse.class); | ||
log.info("Redis Subscribe Channel: {}", email); | ||
log.info("Redis Subscribe Message: {}", alarmResponse.getMessage()); | ||
|
||
sseEmitterService.sendToClient(email, alarmResponse); | ||
} catch (IOException e) { | ||
throw new MessageParsingError(e.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
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
Oops, something went wrong.