Skip to content

Commit

Permalink
feat : 티켓팅 알림 예약 구독 구현 (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
GaBaljaintheroom authored Aug 18, 2024
1 parent d2f2e68 commit 9c842ef
Show file tree
Hide file tree
Showing 20 changed files with 311 additions and 52 deletions.
2 changes: 2 additions & 0 deletions app/api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ allprojects {
jar.enabled = true

dependencies {
implementation project(":app:domain:common-domain")

implementation 'org.springframework.data:spring-data-commons:3.3.0'

implementation "org.springframework.boot:spring-boot-starter-web"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package org.example.config;

import lombok.RequiredArgsConstructor;
import org.example.listener.RegisterShowMessageLister;
import org.example.listener.SubscriptionArtistMessageLister;
import org.example.listener.SubscriptionGenreMessageLister;
import org.example.listener.UnsubscriptionArtistMessageLister;
import org.example.listener.UnsubscriptionGenreMessageLister;
import org.example.listener.UpdateShowMessageLister;
import org.example.listener.RegisterShowMessageListener;
import org.example.listener.SubscriptionArtistMessageListener;
import org.example.listener.SubscriptionGenreMessageListener;
import org.example.listener.UnsubscriptionArtistMessageListener;
import org.example.listener.UnsubscriptionGenreMessageListener;
import org.example.listener.UpdateShowMessageListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
Expand All @@ -21,52 +21,52 @@
@Import({
SubscriptionDomainConfig.class,
PubSubConfig.class,
RegisterShowMessageLister.class,
UpdateShowMessageLister.class,
SubscriptionArtistMessageLister.class,
UnsubscriptionArtistMessageLister.class,
SubscriptionGenreMessageLister.class,
UnsubscriptionGenreMessageLister.class
RegisterShowMessageListener.class,
UpdateShowMessageListener.class,
SubscriptionArtistMessageListener.class,
UnsubscriptionArtistMessageListener.class,
SubscriptionGenreMessageListener.class,
UnsubscriptionGenreMessageListener.class
})
@ComponentScan(basePackages = "org.example")
@RequiredArgsConstructor
public class SubscriptionApiConfig {

private final MessageListener registerShowMessageLister;
private final MessageListener updateShowMessageLister;
private final MessageListener subscriptionArtistMessageLister;
private final MessageListener unsubscriptionArtistMessageLister;
private final MessageListener subscriptionGenreMessageLister;
private final MessageListener unsubscriptionGenreMessageLister;
private final MessageListener registerShowMessageListener;
private final MessageListener updateShowMessageListener;
private final MessageListener subscriptionArtistMessageListener;
private final MessageListener unsubscriptionArtistMessageListener;
private final MessageListener subscriptionGenreMessageListener;
private final MessageListener unsubscriptionGenreMessageListener;

@Bean
MessageListenerAdapter registerShowMessageListenerAdapter() {
return new MessageListenerAdapter(registerShowMessageLister);
return new MessageListenerAdapter(registerShowMessageListener);
}

@Bean
MessageListenerAdapter updateShowMessageListenerAdapter() {
return new MessageListenerAdapter(updateShowMessageLister);
return new MessageListenerAdapter(updateShowMessageListener);
}

@Bean
MessageListenerAdapter subscriptionArtistMessageListerAdapter() {
return new MessageListenerAdapter(subscriptionArtistMessageLister);
return new MessageListenerAdapter(subscriptionArtistMessageListener);
}

@Bean
MessageListenerAdapter unsubscriptionArtistMessageListerAdapter() {
return new MessageListenerAdapter(unsubscriptionArtistMessageLister);
return new MessageListenerAdapter(unsubscriptionArtistMessageListener);
}

@Bean
MessageListenerAdapter subscriptionGenreMessageListerAdapter() {
return new MessageListenerAdapter(subscriptionGenreMessageLister);
return new MessageListenerAdapter(subscriptionGenreMessageListener);
}

@Bean
MessageListenerAdapter unsubscriptionGenreMessageListerAdapter() {
return new MessageListenerAdapter(unsubscriptionGenreMessageLister);
return new MessageListenerAdapter(unsubscriptionGenreMessageListener);
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import org.springframework.data.redis.connection.Message;

@Slf4j
public class MessageConverter {
public class SubscriptionMessageConverter {

private static final ObjectMapper objectMapper = new ObjectMapper();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.example.listener;

import lombok.RequiredArgsConstructor;
import org.example.converter.MessageConverter;
import org.example.converter.SubscriptionMessageConverter;
import org.example.service.SubscriptionAlarmService;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.connection.Message;
Expand All @@ -10,14 +10,14 @@

@Controller
@RequiredArgsConstructor
@Qualifier(value = "updateShowMessageLister")
public class UpdateShowMessageLister implements MessageListener {
@Qualifier(value = "registerShowMessageListener")
public class RegisterShowMessageListener implements MessageListener {

private final SubscriptionAlarmService subscriptionAlarmService;

@Override
public void onMessage(Message message, byte[] pattern) {
var request = MessageConverter.toShowRelationSubscriptionMessage(message);
var request = SubscriptionMessageConverter.toShowRelationSubscriptionMessage(message);
subscriptionAlarmService.showRelationSubscription(request.toServiceRequest());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.example.listener;

import lombok.RequiredArgsConstructor;
import org.example.converter.MessageConverter;
import org.example.converter.SubscriptionMessageConverter;
import org.example.service.SubscriptionAlarmService;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.connection.Message;
Expand All @@ -10,14 +10,14 @@

@Controller
@RequiredArgsConstructor
@Qualifier(value = "subscriptionArtistMessageLister")
public class SubscriptionArtistMessageLister implements MessageListener {
@Qualifier(value = "subscriptionArtistMessageListener")
public class SubscriptionArtistMessageListener implements MessageListener {

private final SubscriptionAlarmService subscriptionAlarmService;

@Override
public void onMessage(Message message, byte[] pattern) {
var request = MessageConverter.toArtistSubscriptionMessage(message);
var request = SubscriptionMessageConverter.toArtistSubscriptionMessage(message);
subscriptionAlarmService.artistSubscribe(request.toServiceRequest());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.example.listener;

import lombok.RequiredArgsConstructor;
import org.example.converter.MessageConverter;
import org.example.converter.SubscriptionMessageConverter;
import org.example.service.SubscriptionAlarmService;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.connection.Message;
Expand All @@ -10,14 +10,14 @@

@Controller
@RequiredArgsConstructor
@Qualifier(value = "subscriptionGenreMessageLister")
public class SubscriptionGenreMessageLister implements MessageListener {
@Qualifier(value = "subscriptionGenreMessageListener")
public class SubscriptionGenreMessageListener implements MessageListener {

private final SubscriptionAlarmService subscriptionAlarmService;

@Override
public void onMessage(Message message, byte[] pattern) {
var request = MessageConverter.toGenreSubscriptionMessage(message);
var request = SubscriptionMessageConverter.toGenreSubscriptionMessage(message);
subscriptionAlarmService.genreSubscribe(request.toServiceRequest());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.example.listener;

import lombok.RequiredArgsConstructor;
import org.example.converter.MessageConverter;
import org.example.converter.SubscriptionMessageConverter;
import org.example.service.SubscriptionAlarmService;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.connection.Message;
Expand All @@ -10,14 +10,14 @@

@Controller
@RequiredArgsConstructor
@Qualifier(value = "unsubscriptionArtistMessageLister")
public class UnsubscriptionArtistMessageLister implements MessageListener {
@Qualifier(value = "unsubscriptionArtistMessageListener")
public class UnsubscriptionArtistMessageListener implements MessageListener {

private final SubscriptionAlarmService subscriptionAlarmService;

@Override
public void onMessage(Message message, byte[] pattern) {
var request = MessageConverter.toArtistSubscriptionMessage(message);
var request = SubscriptionMessageConverter.toArtistSubscriptionMessage(message);
subscriptionAlarmService.artistUnsubscribe(request.toServiceRequest());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.example.listener;

import lombok.RequiredArgsConstructor;
import org.example.converter.MessageConverter;
import org.example.converter.SubscriptionMessageConverter;
import org.example.service.SubscriptionAlarmService;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.connection.Message;
Expand All @@ -10,14 +10,14 @@

@Controller
@RequiredArgsConstructor
@Qualifier(value = "unsubscriptionGenreMessageLister")
public class UnsubscriptionGenreMessageLister implements MessageListener {
@Qualifier(value = "unsubscriptionGenreMessageListener")
public class UnsubscriptionGenreMessageListener implements MessageListener {

private final SubscriptionAlarmService subscriptionAlarmService;

@Override
public void onMessage(Message message, byte[] pattern) {
var request = MessageConverter.toGenreSubscriptionMessage(message);
var request = SubscriptionMessageConverter.toGenreSubscriptionMessage(message);
subscriptionAlarmService.genreUnsubscribe(request.toServiceRequest());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.example.listener;

import lombok.RequiredArgsConstructor;
import org.example.converter.MessageConverter;
import org.example.converter.SubscriptionMessageConverter;
import org.example.service.SubscriptionAlarmService;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.connection.Message;
Expand All @@ -10,14 +10,14 @@

@Controller
@RequiredArgsConstructor
@Qualifier(value = "registerShowMessageLister")
public class RegisterShowMessageLister implements MessageListener {
@Qualifier(value = "updateShowMessageListener")
public class UpdateShowMessageListener implements MessageListener {

private final SubscriptionAlarmService subscriptionAlarmService;

@Override
public void onMessage(Message message, byte[] pattern) {
var request = MessageConverter.toShowRelationSubscriptionMessage(message);
var request = SubscriptionMessageConverter.toShowRelationSubscriptionMessage(message);
subscriptionAlarmService.showRelationSubscription(request.toServiceRequest());
}

Expand Down
4 changes: 4 additions & 0 deletions app/api/ticketing-api/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
dependencies {
implementation project(":app:domain:ticketing-domain")
implementation project(":app:api:common-api")

//redis sub
implementation 'org.springframework.data:spring-data-redis'
implementation 'io.lettuce:lettuce-core:6.3.0.RELEASE'
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,46 @@
package org.example.config;

import lombok.RequiredArgsConstructor;
import org.example.listener.TicketingAlertMessageListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

@Configuration
@Import(TicketingDomainConfig.class)
@Import({
PubSubConfig.class,
TicketingDomainConfig.class,
TicketingAlertMessageListener.class
})
@ComponentScan(basePackages = "org.example")
@RequiredArgsConstructor
public class TicketingApiConfig {

private final MessageListener ticketingAlertMessageListener;

@Bean
MessageListenerAdapter ticketingAlertMessageListenerAdapter() {
return new MessageListenerAdapter(ticketingAlertMessageListener);
}

@Bean
RedisMessageListenerContainer ticketingAlertMessageListenerContainer(
RedisConnectionFactory connectionFactory,
MessageListenerAdapter ticketingAlertMessageListenerAdapter
) {

RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
container.addMessageListener(
ticketingAlertMessageListenerAdapter,
ChannelTopic.of("ticketingAlert")
);
return container;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.example.converter;

import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import lombok.extern.slf4j.Slf4j;
import org.example.listener.dto.TicketingReservationMessageApiRequest;
import org.springframework.data.redis.connection.Message;

@Slf4j
public class TicketingMessageConverter {

private static final ObjectMapper objectMapper = new ObjectMapper();

public static TicketingReservationMessageApiRequest toTicketingReservationMessage(Message message) {
try {
var convertedMessage = objectMapper.readValue(
message.getBody(),
TicketingReservationMessageApiRequest.class
);
log.info("Message published successfully to topic: {}",
new String(message.getChannel()));
log.info(
"Subscribe Message Contents ( TicketingReservationMessageApiRequest : {} )",
message);

return convertedMessage;
} catch (IOException e) {
log.error("Failed to convert message to TicketingReservationMessageApiRequest", e);
throw new IllegalArgumentException("메시지를 받지 못했습니다.");
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.example.listener;

import lombok.RequiredArgsConstructor;
import org.example.converter.TicketingMessageConverter;
import org.example.service.TicketingAlertService;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.stereotype.Controller;

@Controller
@RequiredArgsConstructor
@Qualifier(value = "ticketingAlertMessageListener")
public class TicketingAlertMessageListener implements MessageListener {

private final TicketingAlertService ticketingAlertService;

@Override
public void onMessage(Message message, byte[] pattern) {
var request = TicketingMessageConverter.toTicketingReservationMessage(message);
ticketingAlertService.reserveTicketingAlert(request.toServiceRequest());
}
}
Loading

0 comments on commit 9c842ef

Please sign in to comment.