Skip to content

Commit

Permalink
refactor : final 키워드 추가 및 SseEventBuilder import 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
waterricecake committed Nov 7, 2024
1 parent ef61dec commit c908a37
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 18 deletions.
20 changes: 11 additions & 9 deletions src/main/java/mafia/mafiatogether/common/aspect/SseAspect.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter.SseEventBuilder;


import java.io.IOException;
import java.lang.annotation.Annotation;
Expand All @@ -29,13 +31,13 @@ public class SseAspect {

@Around("@annotation(mafia.mafiatogether.common.annotation.SseSubscribe)")
public Object subscribe(final ProceedingJoinPoint joinPoint) throws Throwable {
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
Method method = methodSignature.getMethod();
final MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
final Method method = methodSignature.getMethod();

Annotation[][] parameterAnnotations = method.getParameterAnnotations();
Object[] args = joinPoint.getArgs();
final Annotation[][] parameterAnnotations = method.getParameterAnnotations();
final Object[] args = joinPoint.getArgs();

String[] codeAndName = new String[2];
final String[] codeAndName = new String[2];
for (int i = 0; i < parameterAnnotations.length; i++) {
if (hasPlayerInfo(parameterAnnotations[i])) {
PlayerInfoDto playerInfoDto = (PlayerInfoDto) args[i];
Expand All @@ -52,21 +54,21 @@ public Object subscribe(final ProceedingJoinPoint joinPoint) throws Throwable {
final String code = codeAndName[0];
final String name = codeAndName[1];

SseEmitter sseEmitter = (SseEmitter) joinPoint.proceed();
final SseEmitter sseEmitter = (SseEmitter) joinPoint.proceed();
sseEmitterSession.save(code, name, sseEmitter);
sseEmitter.onCompletion(() -> sseEmitterSession.deleteByCodeAndEmitter(code, name));
sseEmitter.onTimeout(sseEmitter::complete);

return sseEmitter;
}

private boolean hasPlayerInfo(Annotation[] annotations) {
private boolean hasPlayerInfo(final Annotation[] annotations) {
return Arrays.stream(annotations).anyMatch(PlayerInfo.class::isInstance);
}

public static SseEmitter getSseEmitter(final String name, final Object event) throws IOException {
SseEmitter sseEmitter = new SseEmitter(HOURS_12);
SseEmitter.SseEventBuilder sseEventBuilder = SseEventPublisher.getSseEventBuilder(name, event);
final SseEmitter sseEmitter = new SseEmitter(HOURS_12);
SseEventBuilder sseEventBuilder = SseEventPublisher.getSseEventBuilder(name, event);
sseEmitter.send(sseEventBuilder);
return sseEmitter;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,28 @@ public void save(final String code, final String name, final SseEmitter sseEmitt
}

@Override
public List<SseEmitter> findByCode(String code) {
public List<SseEmitter> findByCode(final String code) {
if (!emitters.containsKey(code)) {
return new ArrayList<>();
}
return emitters.get(code).values().stream().toList();
}

@Override
public SseEmitter findByCodeAndName(String code, String name) {
public SseEmitter findByCodeAndName(final String code, final String name) {
if (!emitters.containsKey(code) || !emitters.get(code).containsKey(name)) {
throw new ServerException(ExceptionCode.INVALID_PLAYER);
}
return emitters.get(code).get(name);
}

@Override
public void deleteByCode(String code) {
public void deleteByCode(final String code) {
emitters.remove(code);
}

@Override
public void deleteByCodeAndEmitter(String code, final String name) {
public void deleteByCodeAndEmitter(final String code, final String name) {
if (!emitters.containsKey(code)) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import mafia.mafiatogether.common.domain.SseEmitterSession;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter.SseEventBuilder;

import java.io.IOException;
import java.util.List;
Expand All @@ -18,20 +19,20 @@ public class SseEventPublisher {
public void publishEventToAllSseClient(final String code, final String eventName, final Object event) {
List<SseEmitter> emitters = sseEmitterSession.findByCode(code);
for (SseEmitter emitter : emitters) {
SseEmitter.SseEventBuilder builder = getSseEventBuilder(eventName, event);
SseEventBuilder builder = getSseEventBuilder(eventName, event);
publishSseEvent(emitter, builder);
}
}

private void publishSseEvent(final SseEmitter emitter, final SseEmitter.SseEventBuilder eventBuilder) {
private void publishSseEvent(final SseEmitter emitter, final SseEventBuilder eventBuilder) {
try {
emitter.send(eventBuilder);
} catch (IOException e) {
emitter.completeWithError(e);
}
}

public static SseEmitter.SseEventBuilder getSseEventBuilder(final String eventName, final Object event) {
public static SseEventBuilder getSseEventBuilder(final String eventName, final Object event) {
return SseEmitter.event()
.name(eventName)
.data(event)
Expand All @@ -45,7 +46,7 @@ public void publishEventByCodeAndName(
final Object event
) {
SseEmitter sseEmitter = sseEmitterSession.findByCodeAndName(code, participantName);
SseEmitter.SseEventBuilder eventBuilder = getSseEventBuilder(eventName, event);
SseEventBuilder eventBuilder = getSseEventBuilder(eventName, event);
publishSseEvent(sseEmitter, eventBuilder);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
public class SseAspectTestService {

@SseSubscribe
public SseEmitter subscribe(@PlayerInfo PlayerInfoDto playerInfoDto){
public SseEmitter subscribe(@PlayerInfo final PlayerInfoDto playerInfoDto){
return mock(SseEmitter.class);
}
}

0 comments on commit c908a37

Please sign in to comment.