Skip to content

Commit

Permalink
Merge pull request #41 from FinalDoubleTen/BE-66-Refactor-STOMP
Browse files Browse the repository at this point in the history
Be 66 refactor stomp
  • Loading branch information
kdjun99 authored Jan 14, 2024
2 parents 4edf3b0 + 212edb2 commit 5a819cb
Show file tree
Hide file tree
Showing 10 changed files with 139 additions and 115 deletions.
20 changes: 13 additions & 7 deletions src/main/java/org/tenten/tentenstomp/config/WebSocketConfig.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,38 @@
package org.tenten.tentenstomp.config;

import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.ChannelRegistration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
import org.tenten.tentenstomp.global.stomp.StompExceptionHandler;
import org.tenten.tentenstomp.global.stomp.StompPreHandler;

@Configuration
@EnableWebSocket
@EnableWebSocketMessageBroker
@RequiredArgsConstructor
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

private final StompExceptionHandler stompExceptionHandler;
private final StompPreHandler stompPreHandler;
@Override
public void registerStompEndpoints(StompEndpointRegistry endpointRegistry) {
endpointRegistry.addEndpoint("/ws-stomp")// 소켓 연결 Endpoint 설정
.setAllowedOriginPatterns("http://*:8080", "http://*.*.*.*:8080", "https://jxy.me/", "http://localhost:5173", "https://weplanplans.vercel.app", "https://dev-weplanplans.vercel.app");
// .withSockJS()
// .setClientLibraryUrl("https://cdnjs.cloudflare.com/ajax/libs/sockjs-client/1.1.2/sockjs.js"); // Todo 추후 특정 url 변경

endpointRegistry.setErrorHandler(stompExceptionHandler);
}

@Override
public void configureMessageBroker(MessageBrokerRegistry brokerRegistry) {
// 클라이언트가 Server로 메세지 발행 -> @MessageMapping 이 붙어있는 메서드와 연결
brokerRegistry.setApplicationDestinationPrefixes("/pub");
// 메서드에서 처리된 메세지의 결과를 broker를 통해서 /sub/message 를 구독하고 있는
// 모든 클라이언트들에게 메세지를 전달
brokerRegistry.enableSimpleBroker("/sub");
}

@Override
public void configureClientInboundChannel(ChannelRegistration registration) {
registration.interceptors(stompPreHandler);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,57 +17,49 @@ public class TripController {

private final TripService tripService;
private final KafkaProducer kafkaProducer;
/*
TODO : 백엔드에서 예외가 발생하면, 프론트로 예외 발생하기 전 시점 데이터를 보내줘야하는데, 이걸 어떻게 할 수 있을까
TODO : 실시간 편집인데, 노션 처럼 누가 어떤 것을 변경했는지 알려줄 필요가 있지 않을까?
*/

@MessageMapping("/kafka")
public void testKafka(@Payload TripUpdateMsg tripUpdateMsg) {
kafkaProducer.send("kafka", tripUpdateMsg);
}

@MessageMapping("/trips/{tripId}/connectMember")
public void connectMember(@DestinationVariable String tripId, @Payload MemberConnectMsg memberConnectMsg) {
log.info("/trips/"+tripId+"/connectMember");
tripService.connectMember(tripId, memberConnectMsg);
}

@MessageMapping("/trips/{tripId}/getConnectedMember")
public void getConnectedMember(@DestinationVariable String tripId) {
log.info("/trips/"+tripId+"/connectMember");
tripService.getConnectedMember(tripId);
}

@MessageMapping("/trips/{tripId}/disconnectMember")
public void disconnectMember(@DestinationVariable String tripId, @Payload MemberDisconnectMsg memberDisconnectMsg) {
log.info("/trips/"+tripId+"/disconnectMember");
tripService.disconnectMember(tripId, memberDisconnectMsg);
}

@MessageMapping("/trips/{tripId}/enterMember")
public void enterMember(@DestinationVariable String tripId, @Payload MemberConnectMsg memberConnectMsg) {
log.info("/trips/"+tripId+"/enterMember");
tripService.enterMember(tripId, memberConnectMsg);
}

@MessageMapping("/trips/{tripId}/info")
public void editPlan(@DestinationVariable String tripId, @Payload TripUpdateMsg tripUpdateMsg) {
log.info("/trips/"+tripId+"/info");
tripService.updateTrip(tripId, tripUpdateMsg);
}

@MessageMapping("/trips/{tripId}/addTripItems")
public void addTripItem(@DestinationVariable String tripId, @Payload TripItemAddMsg tripItemAddMsg) {
log.info("/trips/"+tripId+"/addTripItems");
tripService.addTripItem(tripId, tripItemAddMsg);
}

@MessageMapping("/trips/{tripId}/updateTripItemOrder")
public void updateTripItemOrder(@DestinationVariable String tripId, @Payload TripItemOrderUpdateMsg orderUpdateMsg) {
log.info("/trips/"+tripId+"/updateTripItemOrder");
tripService.updateTripItemOrder(tripId, orderUpdateMsg);
}

@MessageMapping("/trips/{tripId}/getPathAndItems")
public void getPathAndItems(@DestinationVariable String tripId, @Payload PathAndItemRequestMsg pathAndItemRequestMsg) {
log.info("/trips/"+tripId+"/getPathAndItems");
tripService.getPathAndItems(tripId, pathAndItemRequestMsg);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,21 @@ public class TripItemController {

@MessageMapping("/tripItems/{tripItemId}/updatePrice")
public void updateTripItemPrice(@DestinationVariable String tripItemId, @Payload TripItemPriceUpdateMsg priceUpdateMsg) {
log.info("/tripItems/"+tripItemId+"/updatePrice");
tripItemService.updateTripItemPrice(tripItemId, priceUpdateMsg);
}

@MessageMapping("/tripItems/{tripItemId}/updateVisitDate")
public void updateTripItemVisitDate(@DestinationVariable String tripItemId, @Payload TripItemVisitDateUpdateMsg visitDateUpdateMsg) {
log.info("/tripItems/"+tripItemId+"/updateVisitDate");
tripItemService.updateTripItemVisitDate(tripItemId, visitDateUpdateMsg);
}

@MessageMapping("/tripItems/{tripItemId}/updateTransportation")
public void updateTripItemTransportation(@DestinationVariable String tripItemId, @Payload TripItemTransportationUpdateMsg tripItemTransportationUpdateMsg) {
log.info("/tripItems/"+tripItemId+"/updateTransportation");
tripItemService.updateTripItemTransportation(tripItemId, tripItemTransportationUpdateMsg);
}

@MessageMapping("/tripItems/{tripItemId}/deleteItem")
public void deleteTripItem(@DestinationVariable String tripItemId) {
log.info("/tripItems/"+tripItemId+"/deleteItem");
tripItemService.deleteTripItem(tripItemId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
import org.tenten.tentenstomp.domain.trip.dto.request.TripItemPriceUpdateMsg;
import org.tenten.tentenstomp.domain.trip.dto.request.TripItemTransportationUpdateMsg;
import org.tenten.tentenstomp.domain.trip.dto.request.TripItemVisitDateUpdateMsg;
import org.tenten.tentenstomp.domain.trip.dto.response.*;
import org.tenten.tentenstomp.domain.trip.dto.response.TripBudgetMsg;
import org.tenten.tentenstomp.domain.trip.dto.response.TripItemMsg;
import org.tenten.tentenstomp.domain.trip.dto.response.TripPathMsg;
import org.tenten.tentenstomp.domain.trip.entity.Trip;
import org.tenten.tentenstomp.domain.trip.entity.TripItem;
import org.tenten.tentenstomp.domain.trip.repository.TripItemRepository;
import org.tenten.tentenstomp.domain.trip.repository.TripRepository;
import org.tenten.tentenstomp.global.cache.RedisCache;
import org.tenten.tentenstomp.global.component.PathComponent;
import org.tenten.tentenstomp.global.component.dto.request.TripPlace;
import org.tenten.tentenstomp.global.component.dto.response.TripPathCalculationResult;
Expand All @@ -24,14 +25,12 @@
import java.util.Map;

import static org.springframework.http.HttpStatus.NOT_FOUND;
import static org.tenten.tentenstomp.global.common.constant.TopicConstant.*;

@Service
@RequiredArgsConstructor
public class TripItemService {
private final TripItemRepository tripItemRepository;
private final TripRepository tripRepository;
private final RedisCache redisCache;
private final KafkaProducer kafkaProducer;
private final PathComponent pathComponent;
@Transactional
Expand All @@ -45,12 +44,12 @@ public void updateTripItemPrice(String tripItemId, TripItemPriceUpdateMsg priceU
List<TripItem> tripItems = trip.getTripItems();
TripBudgetMsg tripBudgetMsg = new TripBudgetMsg(trip.getId(), trip.getBudget(), trip.getTripItemPriceSum() + trip.getTransportationPriceSum());
TripItemMsg tripItemMsg = TripItemMsg.fromTripItemList(trip.getId(), tripItem.getVisitDate().toString(), tripItems, tripItem.getId(), priceUpdateMsg);
sendToKafkaAndSave(tripBudgetMsg, tripItemMsg);
kafkaProducer.sendAndSaveToRedis(tripBudgetMsg, tripItemMsg);
}
@Transactional
public void updateTripItemVisitDate(String tripItemId, TripItemVisitDateUpdateMsg visitDateUpdateMsg) {
TripItem tripItem = tripItemRepository.findById(Long.parseLong(tripItemId)).orElseThrow(() -> new GlobalException("해당 아이디로 존재하는 tripItem이 없다 " + tripItemId, NOT_FOUND));
Trip trip = tripRepository.findById(tripItem.getTrip().getId()).orElseThrow(() -> new GlobalException("해당 아이디로 존재하는 trip이 없다 " + tripItem.getTrip().getId(), NOT_FOUND));
Trip trip = tripRepository.getReferenceById(tripItem.getTrip().getId());
LocalDate pastDate = tripItem.getVisitDate();
LocalDate newDate = LocalDate.parse(visitDateUpdateMsg.visitDate());

Expand Down Expand Up @@ -90,13 +89,13 @@ public void updateTripItemVisitDate(String tripItemId, TripItemVisitDateUpdateMs
TripPathMsg newDateTripPathMsg = new TripPathMsg(trip.getId(), newDate.toString(), newDateTripPath.tripPathInfoMsgs());
TripBudgetMsg tripBudgetMsg = new TripBudgetMsg(trip.getId(), trip.getBudget(), trip.getTripItemPriceSum() + trip.getTransportationPriceSum());

sendToKafkaAndSave(pastDateTripItemMsg, newDateTripItemMsg, pastDateTripPathMsg, newDateTripPathMsg, tripBudgetMsg);
kafkaProducer.sendAndSaveToRedis(pastDateTripItemMsg, newDateTripItemMsg, pastDateTripPathMsg, newDateTripPathMsg, tripBudgetMsg);

}
@Transactional
public void deleteTripItem(String tripItemId) {
TripItem tripItem = tripItemRepository.findById(Long.parseLong(tripItemId)).orElseThrow(() -> new GlobalException("해당 아이디로 존재하는 tripItem이 없다 " + tripItemId, NOT_FOUND));
Trip trip = tripRepository.findById(tripItem.getTrip().getId()).orElseThrow(() -> new GlobalException("해당 아이디로 존재하는 trip이 없다 " + tripItem.getTrip().getId(), NOT_FOUND));
Trip trip = tripRepository.getReferenceById(tripItem.getTrip().getId());

LocalDate visitDate = tripItem.getVisitDate();

Expand Down Expand Up @@ -124,13 +123,13 @@ public void deleteTripItem(String tripItemId) {
TripPathMsg tripPathMsg = new TripPathMsg(trip.getId(), visitDate.toString(), tripPath.tripPathInfoMsgs());
TripBudgetMsg tripBudgetMsg = new TripBudgetMsg(trip.getId(), trip.getBudget(), trip.getTripItemPriceSum() + trip.getTransportationPriceSum());

sendToKafkaAndSave(tripItemMsg, tripPathMsg, tripBudgetMsg);
kafkaProducer.sendAndSaveToRedis(tripItemMsg, tripPathMsg, tripBudgetMsg);

}
@Transactional
public void updateTripItemTransportation(String tripItemId, TripItemTransportationUpdateMsg tripItemTransportationUpdateMsg) {
TripItem tripItem = tripItemRepository.findById(Long.parseLong(tripItemId)).orElseThrow(() -> new GlobalException("해당 아이디로 존재하는 tripItem이 없다 " + tripItemId, NOT_FOUND));
Trip trip = tripRepository.findById(tripItem.getTrip().getId()).orElseThrow(() -> new GlobalException("해당 아이디로 존재하는 trip이 없다 " + tripItem.getTrip().getId(), NOT_FOUND));
Trip trip = tripRepository.getReferenceById(tripItem.getTrip().getId());

LocalDate visitDate = tripItem.getVisitDate();
List<TripItem> tripItems = tripItemRepository.findTripItemByTripIdAndVisitDate(tripItem.getTrip().getId(), visitDate);
Expand All @@ -153,37 +152,8 @@ public void updateTripItemTransportation(String tripItemId, TripItemTransportati
TripPathMsg tripPathMsg = new TripPathMsg(trip.getId(), visitDate.toString(), tripPath.tripPathInfoMsgs());
TripBudgetMsg tripBudgetMsg = new TripBudgetMsg(trip.getId(), trip.getBudget(), trip.getTripItemPriceSum() + trip.getTransportationPriceSum());

sendToKafkaAndSave(tripItemMsg, tripPathMsg, tripBudgetMsg);
kafkaProducer.sendAndSaveToRedis(tripItemMsg, tripPathMsg, tripBudgetMsg);
}

private void sendToKafkaAndSave(Object... dataArgs) {
for (Object data : dataArgs) {
if (data.getClass().equals(TripPathMsg.class)) {
kafkaProducer.send(PATH, data);
TripPathMsg tripPathMsg = (TripPathMsg) data;
redisCache.save(PATH, Long.toString(tripPathMsg.tripId()), tripPathMsg.visitDate(), tripPathMsg);
}
if (data.getClass().equals(TripItemMsg.class)) {
kafkaProducer.send(TRIP_ITEM, data);
TripItemMsg tripItemMsg = (TripItemMsg) data;
redisCache.save(TRIP_ITEM, Long.toString(tripItemMsg.tripId()), tripItemMsg.visitDate(), tripItemMsg);
}
if (data.getClass().equals(TripInfoMsg.class)) {
kafkaProducer.send(TRIP_INFO, data);
TripInfoMsg tripInfoMsg = (TripInfoMsg) data;
redisCache.save(TRIP_INFO, Long.toString(tripInfoMsg.tripId()), tripInfoMsg);
}
if (data.getClass().equals(TripMemberMsg.class)) {
kafkaProducer.send(MEMBER, data);
TripMemberMsg tripMemberMsg = (TripMemberMsg) data;
redisCache.save(MEMBER, Long.toString(tripMemberMsg.tripId()), tripMemberMsg);
}
if (data.getClass().equals(TripBudgetMsg.class)) {
kafkaProducer.send(BUDGET, data);
TripBudgetMsg tripBudgetMsg = (TripBudgetMsg) data;
redisCache.save(BUDGET, Long.toString(tripBudgetMsg.tripId()), tripBudgetMsg);
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void connectMember(String tripId, MemberConnectMsg memberConnectMsg) {
Long.parseLong(tripId), connectedMemberMap.values().stream().toList(), memberRepository.findTripMemberInfoByTripId(Long.parseLong(tripId)), trip.getNumberOfPeople()
);
tripConnectedMemberMap.put(tripId, connectedMemberMap);
sendToKafkaAndSave(tripMemberMsg);
kafkaProducer.sendAndSaveToRedis(tripMemberMsg);
}

@Transactional
Expand All @@ -62,12 +62,12 @@ public void getConnectedMember(String tripId) {
Trip trip = tripRepository.getReferenceById(Long.parseLong(tripId));

TripMemberMsg tripMemberMsg = new TripMemberMsg(
Long.parseLong(tripId),
connectedMemberMap.values().stream().toList(),
memberRepository.findTripMemberInfoByTripId(Long.parseLong(tripId)),
trip.getNumberOfPeople()
Long.parseLong(tripId),
connectedMemberMap.values().stream().toList(),
memberRepository.findTripMemberInfoByTripId(Long.parseLong(tripId)),
trip.getNumberOfPeople()
);
sendToKafkaAndSave(tripMemberMsg);
kafkaProducer.sendAndSaveToRedis(tripMemberMsg);
}

@Transactional
Expand All @@ -80,7 +80,7 @@ public void disconnectMember(String tripId, MemberDisconnectMsg memberDisconnect
Long.parseLong(tripId), connectedMemberMap.values().stream().toList(), memberRepository.findTripMemberInfoByTripId(Long.parseLong(tripId)), trip.getNumberOfPeople()
);
tripConnectedMemberMap.put(tripId, connectedMemberMap);
sendToKafkaAndSave(tripMemberMsg);
kafkaProducer.sendAndSaveToRedis(tripMemberMsg);

}

Expand All @@ -106,7 +106,7 @@ public void updateTrip(String tripId, TripUpdateMsg tripUpdateMsg) {
);
tripRepository.save(trip);

sendToKafkaAndSave(tripInfoMsg, tripBudgetMsg);
kafkaProducer.sendAndSaveToRedis(tripInfoMsg, tripBudgetMsg);

}

Expand Down Expand Up @@ -138,7 +138,7 @@ private void updateBudgetAndItemsAndPath(Trip trip, List<TripItem> tripItems, St
TripItemMsg tripItemMsg = TripItemMsg.fromTripItemList(trip.getId(), visitDate, tripItems);
TripPathMsg tripPathMsg = new TripPathMsg(trip.getId(), visitDate, tripPath.tripPathInfoMsgs());

sendToKafkaAndSave(tripBudgetMsg, tripItemMsg, tripPathMsg);
kafkaProducer.sendAndSaveToRedis(tripBudgetMsg, tripItemMsg, tripPathMsg);
}

@Transactional
Expand All @@ -164,36 +164,6 @@ public void getPathAndItems(String tripId, PathAndItemRequestMsg pathAndItemRequ
kafkaProducer.send(PATH, getTripPathMsg(trip, pathAndItemRequestMsg.visitDate()));
}

private void sendToKafkaAndSave(Object... dataArgs) {
for (Object data : dataArgs) {
if (data.getClass().equals(TripPathMsg.class)) {
kafkaProducer.send(PATH, data);
TripPathMsg tripPathMsg = (TripPathMsg) data;
redisCache.save(PATH, Long.toString(tripPathMsg.tripId()), tripPathMsg.visitDate(), tripPathMsg);
}
if (data.getClass().equals(TripItemMsg.class)) {
kafkaProducer.send(TRIP_ITEM, data);
TripItemMsg tripItemMsg = (TripItemMsg) data;
redisCache.save(TRIP_ITEM, Long.toString(tripItemMsg.tripId()), tripItemMsg.visitDate(), tripItemMsg);
}
if (data.getClass().equals(TripInfoMsg.class)) {
kafkaProducer.send(TRIP_INFO, data);
TripInfoMsg tripInfoMsg = (TripInfoMsg) data;
redisCache.save(TRIP_INFO, Long.toString(tripInfoMsg.tripId()), tripInfoMsg);
}
if (data.getClass().equals(TripMemberMsg.class)) {
kafkaProducer.send(MEMBER, data);
TripMemberMsg tripMemberMsg = (TripMemberMsg) data;
redisCache.save(MEMBER, Long.toString(tripMemberMsg.tripId()), tripMemberMsg);
}
if (data.getClass().equals(TripBudgetMsg.class)) {
kafkaProducer.send(BUDGET, data);
TripBudgetMsg tripBudgetMsg = (TripBudgetMsg) data;
redisCache.save(BUDGET, Long.toString(tripBudgetMsg.tripId()), tripBudgetMsg);
}
}
}

private TripMemberMsg getTripMemberMsg(String tripId) {
Object cached = redisCache.get(MEMBER, tripId);
if (cached != null) {
Expand Down
Loading

0 comments on commit 5a819cb

Please sign in to comment.