Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

import java.util.concurrent.Executor;

@Configuration
public class SchedulerConfig {

Expand All @@ -16,4 +19,15 @@ public TaskScheduler taskScheduler() {
scheduler.initialize();
return scheduler;
}
}

@Bean(name = "notificationAsyncExecutor")
public Executor notificationAsyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(4);
executor.setMaxPoolSize(8);
executor.setQueueCapacity(200);
executor.setThreadNamePrefix("notification-");
executor.initialize();
return executor;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import devkor.ontime_back.entity.NotificationSchedule;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

@Repository
Expand All @@ -17,5 +19,11 @@ public interface NotificationScheduleRepository extends JpaRepository<Notificati
"WHERE n.notificationTime > :now AND n.isSent = false")
List<NotificationSchedule> findAllWithScheduleAndUser(LocalDateTime now);

@Query("SELECT n FROM NotificationSchedule n " +
"JOIN FETCH n.schedule s " +
"JOIN FETCH s.user " +
"WHERE n.id = :notificationId")
Optional<NotificationSchedule> findByIdWithScheduleAndUser(@Param("notificationId") Long notificationId);

List<NotificationSchedule> findAllByScheduleScheduleIdOrderByIdAsc(UUID scheduleId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package devkor.ontime_back.service;

import com.google.firebase.messaging.FirebaseMessaging;
import com.google.firebase.messaging.Message;
import devkor.ontime_back.entity.NotificationSchedule;
import devkor.ontime_back.entity.Schedule;
import devkor.ontime_back.entity.User;
import devkor.ontime_back.entity.UserSetting;
import devkor.ontime_back.repository.NotificationScheduleRepository;
import devkor.ontime_back.repository.UserSettingRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class NotificationDeliveryService {

private final UserSettingRepository userSettingRepository;
private final AlarmService alarmService;
private final NotificationScheduleRepository notificationScheduleRepository;

@Transactional
public void sendReminder(NotificationSchedule notificationSchedule, String message) {
Long userId = notificationSchedule.getSchedule().getUser().getId();

if (userId != null) {
UserSetting userSetting = userSettingRepository.findByUserId(userId)
.orElseThrow(() -> new IllegalArgumentException("No UserSetting found in schedule's user"));
log.debug("사용자 알림 전송 설정 여부: " + userSetting.getIsNotificationsEnabled());

if (Boolean.TRUE.equals(userSetting.getIsNotificationsEnabled())) {
if (alarmService.shouldSuppressLegacyReminder(
userId,
notificationSchedule.getSchedule().getScheduleId(),
notificationSchedule.getNotificationTime())) {
log.info("현재 기기 로컬 알람 커버리지로 인해 레거시 푸시 알림을 생략합니다. scheduleId={}",
notificationSchedule.getSchedule().getScheduleId());
return;
}
sendNotificationToUser(notificationSchedule.getSchedule(), message);
notificationSchedule.changeStatusToSent();
notificationScheduleRepository.save(notificationSchedule);
}
}
}

public void sendReminder(List<Schedule> schedules, String message) {
for (Schedule schedule : schedules) {
User user = schedule.getUser();
Long userId = user.getId();

if (userId != null) {
UserSetting userSetting = userSettingRepository.findByUserId(userId)
.orElseThrow(() -> new IllegalArgumentException("No UserSetting found in schedule's user"));

if (userSetting != null && userSetting.getIsNotificationsEnabled()) {
sendNotificationToUser(schedule, message);
}
}
}
}

public void sendNotificationToUser(Schedule schedule, String message) {
User user = schedule.getUser();
String firebaseToken = user.getFirebaseToken();

Message firebaseMessage = Message.builder()
.putData("title", "약속 알림")
.putData("content", user.getName() + "님 " + message + "\n약속명: " + schedule.getScheduleName())
.setToken(firebaseToken)
.build();

try {
FirebaseMessaging.getInstance().send(firebaseMessage);
log.info("Firebase에 성공적으로 push notification 요청을 보냈으며, Firebase로부터 적절한 응답을 받았습니다 \n알림 푸시한 약속:" + schedule.getScheduleName());
} catch (Exception e) {
e.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package devkor.ontime_back.service;

import devkor.ontime_back.entity.NotificationSchedule;
import devkor.ontime_back.repository.NotificationScheduleRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Service
@RequiredArgsConstructor
public class NotificationDispatchService {

private final NotificationScheduleRepository notificationScheduleRepository;
private final NotificationDeliveryService notificationDeliveryService;

@Async("notificationAsyncExecutor")
@Transactional
public void dispatchReminder(Long notificationId, String message) {
NotificationSchedule notificationSchedule = notificationScheduleRepository.findByIdWithScheduleAndUser(notificationId)
.orElse(null);
if (notificationSchedule == null) {
log.warn("예약된 알림을 찾을 수 없어 푸시 알림을 건너뜁니다. notificationId={}", notificationId);
return;
}

notificationDeliveryService.sendReminder(notificationSchedule, message);
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
package devkor.ontime_back.service;

import com.google.firebase.messaging.FirebaseMessaging;
import com.google.firebase.messaging.Message;
import devkor.ontime_back.entity.NotificationSchedule;
import devkor.ontime_back.entity.Schedule;
import devkor.ontime_back.entity.User;
import devkor.ontime_back.entity.UserSetting;
import devkor.ontime_back.repository.NotificationScheduleRepository;
import devkor.ontime_back.repository.UserSettingRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.ZoneId;
Expand All @@ -26,13 +17,11 @@
@Slf4j
@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class NotificationService {

private final UserSettingRepository userSettingRepository;
private final AlarmService alarmService;
private final TaskScheduler taskScheduler;
private final NotificationScheduleRepository notificationScheduleRepository;
private final NotificationDispatchService notificationDispatchService;
private final NotificationDeliveryService notificationDeliveryService;
private final ConcurrentHashMap<Long, ScheduledFuture<?>> scheduledTasks = new ConcurrentHashMap<>();

public void scheduleReminder(NotificationSchedule notificationSchedule) {
Expand All @@ -43,12 +32,19 @@ public void scheduleReminder(NotificationSchedule notificationSchedule) {
return;
}

Long notificationId = notificationSchedule.getId();
if (notificationId == null) {
throw new IllegalArgumentException("NotificationSchedule must be persisted before scheduling");
}

ScheduledFuture<?> future = taskScheduler.schedule(
() -> sendReminder(notificationSchedule, "준비 시작해야 합니다.(현재 시각: 약속시각 - (여유시간 + 이동시간 + 총준비시간) )"),
() -> notificationDispatchService.dispatchReminder(
notificationId,
"준비 시작해야 합니다.(현재 시각: 약속시각 - (여유시간 + 이동시간 + 총준비시간) )"),
Date.from(reminderTime.atZone(ZoneId.systemDefault()).toInstant())
);

scheduledTasks.put(notificationSchedule.getId(), future);
scheduledTasks.put(notificationId, future);

log.info("스케줄 등록 완료 {} ({})", notificationSchedule.getSchedule().getScheduleName(), reminderTime);
}
Expand All @@ -62,64 +58,15 @@ public void cancelScheduledNotification(Long notificationId) {
}
}

@Async
@Transactional
public void sendReminder(NotificationSchedule notificationSchedule, String message) {
Long userId = notificationSchedule.getSchedule().getUser().getId();

if (userId != null) {
UserSetting userSetting = userSettingRepository.findByUserId(userId)
.orElseThrow(() -> new IllegalArgumentException("No UserSetting found in schedule's user"));
log.debug("사용자 알림 전송 설정 여부: " + userSetting.getIsNotificationsEnabled());

if (Boolean.TRUE.equals(userSetting.getIsNotificationsEnabled())) {
if (alarmService.shouldSuppressLegacyReminder(
userId,
notificationSchedule.getSchedule().getScheduleId(),
notificationSchedule.getNotificationTime())) {
log.info("현재 기기 로컬 알람 커버리지로 인해 레거시 푸시 알림을 생략합니다. scheduleId={}",
notificationSchedule.getSchedule().getScheduleId());
return;
}
sendNotificationToUser(notificationSchedule.getSchedule(), message);
notificationSchedule.changeStatusToSent();
notificationScheduleRepository.save(notificationSchedule);
}
}
notificationDeliveryService.sendReminder(notificationSchedule, message);
}

public void sendReminder(List<Schedule> schedules, String message) {
for (Schedule schedule : schedules) {
User user = schedule.getUser();
Long userId = user.getId();

if (userId != null) {
UserSetting userSetting = userSettingRepository.findByUserId(userId)
.orElseThrow(() -> new IllegalArgumentException("No UserSetting found in schedule's user"));// Repository 메서드 가정

if (userSetting != null && userSetting.getIsNotificationsEnabled()) {
sendNotificationToUser(schedule, message);
}
}
}
notificationDeliveryService.sendReminder(schedules, message);
}

@Transactional
public void sendNotificationToUser(Schedule schedule, String message) {
User user = schedule.getUser();
String firebaseToken = user.getFirebaseToken();

Message firebaseMessage = Message.builder()
.putData("title", "약속 알림")
.putData("content", user.getName() + "님 " + message + "\n약속명: " + schedule.getScheduleName())
.setToken(firebaseToken)
.build();

try {
FirebaseMessaging.getInstance().send(firebaseMessage);
log.info("Firebase에 성공적으로 push notification 요청을 보냈으며, Firebase로부터 적절한 응답을 받았습니다 \n알림 푸시한 약속:" + schedule.getScheduleName());
} catch (Exception e) {
e.printStackTrace();
}
notificationDeliveryService.sendNotificationToUser(schedule, message);
}
}
Loading
Loading