-
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.
[Feat] 알람 기능구현 및 spring batch를 사용한 스케줄기능 구현 (#13)
* feat: Batch 설정 및 user엔티티 재정 * feat: Batch를 사용해 수업전 리마인드 알람 스케줄 작성 * feat: Batch를 사용해 수업전 리마인드 알람 스케줄 작성(JPA -> 쿼리로 변경) * feat: Batch를 사용해 수업후 알람 및 이용권 개수 차감 * feat: job을 실행시키는 스케줄러 구현 * feat: batch로 스케줄 로직 이동 및 클래스 네이밍 오타 수정 * feat: TaskExecutor 설정 * feat: 페키지 이동(service -> application) * feat: Batch 설정 및 user엔티티 재정 * feat: Batch를 사용해 수업전 리마인드 알람 스케줄 작성 * feat: Batch를 사용해 수업전 리마인드 알람 스케줄 작성(JPA -> 쿼리로 변경) * feat: Batch를 사용해 수업후 알람 및 이용권 개수 차감 * feat: job을 실행시키는 스케줄러 구현 * feat: batch로 스케줄 로직 이동 및 클래스 네이밍 오타 수정 * feat: TaskExecutor 설정 * feat: 페키지 이동(service -> application)
- Loading branch information
Showing
29 changed files
with
548 additions
and
157 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
4 changes: 3 additions & 1 deletion
4
...g/doochul/service/MessageSendManager.java → ...ochul/application/MessageSendManager.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
4 changes: 3 additions & 1 deletion
4
...ul/service/NotificationEventListener.java → ...pplication/NotificationEventListener.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
56 changes: 56 additions & 0 deletions
56
doochul/src/main/java/org/doochul/application/NotificationService.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,56 @@ | ||
package org.doochul.application; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.doochul.domain.lesson.Lesson; | ||
import org.doochul.domain.user.User; | ||
import org.doochul.application.event.LessonCreateEvent; | ||
import org.doochul.application.event.LessonWithdrawnEvent; | ||
import org.doochul.infra.dto.Letter; | ||
import org.doochul.support.KeyGenerator; | ||
import org.springframework.scheduling.annotation.Async; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.time.Duration; | ||
|
||
import static org.doochul.domain.lesson.LessonStatus.SCHEDULED_LESSON; | ||
import static org.doochul.domain.lesson.LessonStatus.WITHDRAWN_LESSON; | ||
|
||
@Slf4j | ||
@Service | ||
@RequiredArgsConstructor | ||
public class NotificationService { | ||
private final MessageSendManager messageSendManager; | ||
private final KeyGenerator keyGenerator; | ||
private final RedisService redisService; | ||
|
||
public void applyForLesson(final LessonCreateEvent event) { | ||
final User student = event.student(); | ||
final User teacher = event.teacher(); | ||
final Lesson lesson = event.lesson(); | ||
sendNotification( | ||
Letter.of(student.getDeviceToken(), | ||
student.getName(), | ||
teacher.getName(), | ||
lesson.getStartedAt(), | ||
SCHEDULED_LESSON)); | ||
} | ||
|
||
public void withdrawnForLessons(final LessonWithdrawnEvent event) { | ||
sendNotification( | ||
Letter.of(event.student().getDeviceToken(), | ||
event.student().getName(), | ||
event.teacher().getName(), | ||
event.lesson().getStartedAt(), | ||
WITHDRAWN_LESSON)); | ||
} | ||
|
||
@Async | ||
public void sendNotification(final Letter letter) { | ||
final String key = keyGenerator.generateAccountKey(letter.targetToken()); | ||
if (redisService.setNX(key, "notification", Duration.ofSeconds(5))) { | ||
messageSendManager.sendTo(letter); | ||
redisService.delete(key); | ||
} | ||
} | ||
} |
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
2 changes: 1 addition & 1 deletion
2
...rg/doochul/service/LessonCreateEvent.java → .../application/event/LessonCreateEvent.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
2 changes: 1 addition & 1 deletion
2
...doochul/service/LessonWithdrawnEvent.java → ...plication/event/LessonWithdrawnEvent.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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package org.doochul.config; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.scheduling.annotation.EnableAsync; | ||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; | ||
|
||
import java.util.concurrent.Executor; | ||
|
||
@EnableAsync | ||
@Configuration | ||
public class AsyncConfig { | ||
|
||
@Bean | ||
public Executor taskExecutor() { | ||
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); | ||
executor.setCorePoolSize(4); | ||
executor.setWaitForTasksToCompleteOnShutdown(true); | ||
executor.setAwaitTerminationSeconds(60); | ||
executor.initialize(); | ||
return executor; | ||
} | ||
} |
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,19 @@ | ||
package org.doochul.config; | ||
|
||
import org.springframework.batch.core.configuration.JobRegistry; | ||
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; | ||
import org.springframework.batch.core.configuration.support.JobRegistryBeanPostProcessor; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@EnableBatchProcessing | ||
@Configuration | ||
public class BatchConfig { | ||
|
||
@Bean | ||
public JobRegistryBeanPostProcessor jobRegistryBeanPostProcessor(JobRegistry jobRegistry) { | ||
JobRegistryBeanPostProcessor jobRegistryBeanPostProcessor = new JobRegistryBeanPostProcessor(); | ||
jobRegistryBeanPostProcessor.setJobRegistry(jobRegistry); | ||
return jobRegistryBeanPostProcessor; | ||
} | ||
} |
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
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
4 changes: 1 addition & 3 deletions
4
doochul/src/main/java/org/doochul/domain/product/ProductRepository.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 |
---|---|---|
@@ -1,10 +1,8 @@ | ||
package org.doochul.domain.product; | ||
|
||
import java.util.List; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public interface ProductRepository extends JpaRepository<Product, Long> { | ||
List<Product> findAll(); | ||
public interface ProductRepository extends JpaRepository<ProductRepository, Long> { | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package org.doochul.infra.dto; | ||
|
||
import org.doochul.domain.lesson.LessonStatus; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
public record Letter( | ||
String targetToken, | ||
String title, | ||
String body | ||
) { | ||
public static Letter of( | ||
final String studentToken, | ||
final String studentName, | ||
final String teacherName, | ||
final LocalDateTime startedAt, | ||
final LessonStatus lessonStatus | ||
) { | ||
final String message = lessonStatus.getMessage(studentName, startedAt, teacherName); | ||
return new Letter(studentToken, lessonStatus.getTitle(), message); | ||
} | ||
} |
Oops, something went wrong.