-
Notifications
You must be signed in to change notification settings - Fork 0
Merge/noti kafak into develop #84
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
ee23aeb
e26d869
6e4af25
d62ce79
d9deea2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,7 @@ name: CI | |
|
|
||
| on: | ||
| pull_request: | ||
| branches: [main] | ||
| branches: [main, develop] | ||
|
|
||
| jobs: | ||
| test: | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -1,11 +1,20 @@ | ||||||||
| package com.catchtable.global.config; | ||||||||
|
|
||||||||
| import org.apache.kafka.clients.consumer.ConsumerConfig; | ||||||||
| import org.apache.kafka.clients.producer.ProducerConfig; | ||||||||
| import org.apache.kafka.common.serialization.StringDeserializer; | ||||||||
| import org.apache.kafka.common.serialization.StringSerializer; | ||||||||
| import org.springframework.context.annotation.Bean; | ||||||||
| import org.springframework.context.annotation.Configuration; | ||||||||
| import org.springframework.kafka.annotation.EnableKafka; | ||||||||
| import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory; | ||||||||
| import org.springframework.kafka.core.*; | ||||||||
| import org.springframework.scheduling.annotation.EnableScheduling; | ||||||||
|
|
||||||||
| import java.time.Clock; | ||||||||
| import java.time.ZoneId; | ||||||||
| import java.util.HashMap; | ||||||||
| import java.util.Map; | ||||||||
|
|
||||||||
| @Configuration | ||||||||
| @EnableScheduling | ||||||||
|
|
@@ -20,4 +29,50 @@ public class SchedulerConfig { | |||||||
| public Clock clock() { | ||||||||
| return Clock.system(ZoneId.of("Asia/Seoul")); | ||||||||
| } | ||||||||
|
|
||||||||
| @EnableKafka | ||||||||
| @Configuration | ||||||||
| public static class KafkaConfig { | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||
|
|
||||||||
| private final String bootstrapServers = "localhost:9092"; | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. bootstrapServers ์ฃผ์๊ฐ ํ๋์ฝ๋ฉ๋์ด ์์ต๋๋ค. ํ๊ฒฝ๋ณ๋ก ์ ์ฐํ๊ฒ ๊ด๋ฆฌํ ์ ์๋๋ก @value๋ฅผ ์ฌ์ฉํ์ฌ application.yml ์ค์ ํ์ผ์์ ๊ฐ์ ๊ฐ์ ธ์ค๋๋ก ์์ ํ์ญ์์ค.
Suggested change
|
||||||||
|
|
||||||||
| @Bean | ||||||||
| public KafkaTemplate<String, Object> kafkaTemplate() { | ||||||||
| return new KafkaTemplate<>(producerFactory()); | ||||||||
| } | ||||||||
|
|
||||||||
| @Bean | ||||||||
| public ProducerFactory<String, Object> producerFactory() { | ||||||||
| Map<String, Object> config = new HashMap<>(); | ||||||||
| config.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers); | ||||||||
| config.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class); | ||||||||
|
|
||||||||
| // ๐จ [์์ ] ์ํฌํธ ์์ด ์คํ๋ง ์ ๊ณต ๋ฌธ์์ด ๊ฒฝ๋ก๋ก ์ ํํ๊ฒ ๊ณ ์ ํฉ๋๋ค. | ||||||||
| config.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, "org.springframework.kafka.support.serializer.JsonSerializer"); | ||||||||
| return new DefaultKafkaProducerFactory<>(config); | ||||||||
| } | ||||||||
|
|
||||||||
| @Bean | ||||||||
| public ConcurrentKafkaListenerContainerFactory<String, Object> kafkaListenerContainerFactory() { | ||||||||
| ConcurrentKafkaListenerContainerFactory<String, Object> factory = new ConcurrentKafkaListenerContainerFactory<>(); | ||||||||
| factory.setConsumerFactory(consumerFactory()); | ||||||||
| return factory; | ||||||||
| } | ||||||||
|
|
||||||||
| @Bean | ||||||||
| public ConsumerFactory<String, Object> consumerFactory() { | ||||||||
| Map<String, Object> config = new HashMap<>(); | ||||||||
| config.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers); | ||||||||
| config.put(ConsumerConfig.GROUP_ID_CONFIG, "catchtable-notification-group"); | ||||||||
| config.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest"); | ||||||||
| config.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class); | ||||||||
|
|
||||||||
| config.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, "org.springframework.kafka.support.serializer.JsonDeserializer"); | ||||||||
|
|
||||||||
| config.put("spring.json.trusted.packages", "com.catchtable.notification.event,java.lang.String,java.lang.Object"); | ||||||||
| config.put("spring.json.use.type.headers", true); | ||||||||
|
|
||||||||
| return new DefaultKafkaConsumerFactory<>(config); | ||||||||
| } | ||||||||
| } | ||||||||
| } | ||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,16 @@ | ||
| package com.catchtable.notification.event; | ||
|
|
||
| import lombok.AllArgsConstructor; | ||
| import lombok.Getter; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| @Getter | ||
| @RequiredArgsConstructor | ||
| @NoArgsConstructor | ||
| @AllArgsConstructor | ||
| public class ReservationCanceledEvent { | ||
| private final Long reservationId; | ||
| private final Long userId; | ||
| private final String storeName; | ||
| private final String remainDate; | ||
| private final String remainTime; | ||
| private Long reservationId; | ||
| private Long userId; | ||
| private String storeName; | ||
| private String remainDate; | ||
| private String remainTime; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,18 @@ | ||
| package com.catchtable.notification.event; | ||
|
|
||
| import lombok.AllArgsConstructor; | ||
| import lombok.Getter; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| @Getter | ||
| @RequiredArgsConstructor | ||
| @NoArgsConstructor | ||
| @AllArgsConstructor | ||
| public class ReservationChangedEvent { | ||
| private final Long newReservationId; | ||
| private final Long userId; | ||
| private final String storeName; | ||
| private final String oldRemainDate; | ||
| private final String oldRemainTime; | ||
| private final String newRemainDate; | ||
| private final String newRemainTime; | ||
| private Long newReservationId; | ||
| private Long userId; | ||
| private String storeName; | ||
| private String oldRemainDate; | ||
| private String oldRemainTime; | ||
| private String newRemainDate; | ||
| private String newRemainTime; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,16 @@ | ||
| package com.catchtable.notification.event; | ||
|
|
||
| import lombok.AllArgsConstructor; | ||
| import lombok.Getter; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| @Getter | ||
| @RequiredArgsConstructor | ||
| @NoArgsConstructor | ||
| @AllArgsConstructor | ||
| public class ReservationConfirmedEvent { | ||
| private final Long reservationId; | ||
| private final Long userId; | ||
| private final String storeName; | ||
| private final String remainDate; | ||
| private final String remainTime; | ||
| private Long reservationId; | ||
| private Long userId; | ||
| private String storeName; | ||
| private String remainDate; | ||
| private String remainTime; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,16 @@ | ||
| package com.catchtable.notification.event; | ||
|
|
||
| import lombok.AllArgsConstructor; | ||
| import lombok.Getter; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| @Getter | ||
| @RequiredArgsConstructor | ||
| @NoArgsConstructor | ||
| @AllArgsConstructor | ||
| public class ReservationReminderEvent { | ||
| private final Long reservationId; | ||
| private final Long userId; | ||
| private final String storeName; | ||
| private final String remainDate; | ||
| private final String remainTime; | ||
| private Long reservationId; | ||
| private Long userId; | ||
| private String storeName; | ||
| private String remainDate; | ||
| private String remainTime; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,16 @@ | ||
| package com.catchtable.notification.event; | ||
|
|
||
| import lombok.AllArgsConstructor; | ||
| import lombok.Getter; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| @Getter | ||
| @RequiredArgsConstructor | ||
| @NoArgsConstructor | ||
| @AllArgsConstructor | ||
| public class ReservationVisitedEvent { | ||
| private final Long reservationId; | ||
| private final Long userId; | ||
| private final String storeName; | ||
| private final String remainDate; | ||
| private final String remainTime; | ||
| private Long reservationId; | ||
| private Long userId; | ||
| private String storeName; | ||
| private String remainDate; | ||
| private String remainTime; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,12 @@ | ||
| package com.catchtable.notification.event; | ||
|
|
||
| import lombok.AllArgsConstructor; | ||
| import lombok.Getter; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| @Getter | ||
| @RequiredArgsConstructor | ||
| @NoArgsConstructor | ||
| @AllArgsConstructor | ||
| public class VacancyEvent { | ||
| private final Long remainId; | ||
| } | ||
| private Long remainId; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,50 +1,50 @@ | ||
| package com.catchtable.notification.listener; | ||
|
|
||
| import com.catchtable.global.exception.CustomException; | ||
| import com.catchtable.global.exception.ErrorCode; | ||
| import com.catchtable.notification.entity.NotificationType; | ||
| import com.catchtable.notification.event.ReservationCanceledEvent; | ||
| import com.catchtable.notification.service.NotificationService; | ||
| import com.catchtable.user.entity.User; | ||
| import com.catchtable.user.repository.UserRepository; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.scheduling.annotation.Async; | ||
| import org.springframework.stereotype.Component; | ||
| import org.springframework.transaction.annotation.Propagation; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
| import org.springframework.transaction.event.TransactionPhase; | ||
| import org.springframework.transaction.event.TransactionalEventListener; | ||
|
|
||
| @Slf4j | ||
| @Component | ||
| @RequiredArgsConstructor | ||
| public class ReservationCanceledNotificationListener { | ||
|
|
||
| private final NotificationService notificationService; | ||
| private final UserRepository userRepository; | ||
|
|
||
| @Async | ||
| @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT) | ||
| @Transactional(propagation = Propagation.REQUIRES_NEW) | ||
| public void handleReservationCanceledEvent(ReservationCanceledEvent event) { | ||
| User user = userRepository.findById(event.getUserId()) | ||
| .orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND)); | ||
|
|
||
| String title = "์์ฝ์ด ์ทจ์๋์์ต๋๋ค."; | ||
| String content = String.format("'%s' ๋งค์ฅ %s %s ์์ฝ์ด ์ทจ์๋์์ต๋๋ค.", | ||
| event.getStoreName(), | ||
| event.getRemainDate(), | ||
| event.getRemainTime()); | ||
|
|
||
| notificationService.createNotification( | ||
| user, | ||
| NotificationType.RESERVATION_CANCELED, | ||
| title, | ||
| content, | ||
| event.getReservationId() | ||
| ); | ||
|
|
||
| log.info("[์์ฝ ์ทจ์ ์๋ฆผ] userId: {}, reservationId: {} ์๋ฆผ ์์ฑ ์๋ฃ", event.getUserId(), event.getReservationId()); | ||
| } | ||
| } | ||
| //package com.catchtable.notification.listener; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ์นดํ์นด ๋์ ์ผ๋ก ์ธํด ๋ ์ด์ ์ฌ์ฉํ์ง ์๋ ๊ธฐ์กด ๋ฆฌ์ค๋ ์ฝ๋๊ฐ ์ฃผ์ ์ฒ๋ฆฌ๋ ์ฑ๋ก ๋จ์ ์์ต๋๋ค. ์ฝ๋ ๋ฒ ์ด์ค์ ์ฒญ๊ฒฐํจ์ ์ํด ์ฃผ์ ์ฒ๋ฆฌ๋ ํ์ผ์ ์ญ์ ํ๊ฑฐ๋ ๋ด์ฉ์ ์ ๊ฑฐํ๋ ๊ฒ์ ๊ถ์ฅํฉ๋๋ค. (๋ค๋ฅธ ๋ฆฌ์ค๋ ํ์ผ๋ค๋ ๋์ผํ๊ฒ ์ ์ฉ๋ฉ๋๋ค.) |
||
| // | ||
| //import com.catchtable.global.exception.CustomException; | ||
| //import com.catchtable.global.exception.ErrorCode; | ||
| //import com.catchtable.notification.entity.NotificationType; | ||
| //import com.catchtable.notification.event.ReservationCanceledEvent; | ||
| //import com.catchtable.notification.service.NotificationService; | ||
| //import com.catchtable.user.entity.User; | ||
| //import com.catchtable.user.repository.UserRepository; | ||
| //import lombok.RequiredArgsConstructor; | ||
| //import lombok.extern.slf4j.Slf4j; | ||
| //import org.springframework.scheduling.annotation.Async; | ||
| //import org.springframework.stereotype.Component; | ||
| //import org.springframework.transaction.annotation.Propagation; | ||
| //import org.springframework.transaction.annotation.Transactional; | ||
| //import org.springframework.transaction.event.TransactionPhase; | ||
| //import org.springframework.transaction.event.TransactionalEventListener; | ||
| // | ||
| //@Slf4j | ||
| //@Component | ||
| //@RequiredArgsConstructor | ||
| //public class ReservationCanceledNotificationListener { | ||
| // | ||
| // private final NotificationService notificationService; | ||
| // private final UserRepository userRepository; | ||
| // | ||
| // @Async | ||
| // @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT) | ||
| // @Transactional(propagation = Propagation.REQUIRES_NEW) | ||
| // public void handleReservationCanceledEvent(ReservationCanceledEvent event) { | ||
| // User user = userRepository.findById(event.getUserId()) | ||
| // .orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND)); | ||
| // | ||
| // String title = "์์ฝ์ด ์ทจ์๋์์ต๋๋ค."; | ||
| // String content = String.format("'%s' ๋งค์ฅ %s %s ์์ฝ์ด ์ทจ์๋์์ต๋๋ค.", | ||
| // event.getStoreName(), | ||
| // event.getRemainDate(), | ||
| // event.getRemainTime()); | ||
| // | ||
| // notificationService.createNotification( | ||
| // user, | ||
| // NotificationType.RESERVATION_CANCELED, | ||
| // title, | ||
| // content, | ||
| // event.getReservationId() | ||
| // ); | ||
| // | ||
| // log.info("[์์ฝ ์ทจ์ ์๋ฆผ] userId: {}, reservationId: {} ์๋ฆผ ์์ฑ ์๋ฃ", event.getUserId(), event.getReservationId()); | ||
| // } | ||
| //} | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@value ์ด๋ ธํ ์ด์ ์ ์ฌ์ฉํ์ฌ ์ค์ ์ ์ธ๋ถํํ๊ธฐ ์ํด ํ์ํ ์ํฌํธ๋ฅผ ์ถ๊ฐํ์ญ์์ค.