-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Spring Scheduler + Cron tab을 이용하여 구현하였습니다.
- Loading branch information
Showing
3 changed files
with
46 additions
and
37 deletions.
There are no files selected for viewing
37 changes: 0 additions & 37 deletions
37
clody-batch/src/test/java/com/clody/clodybatch/reply/ReplyScheduledServiceTest.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,51 +1,14 @@ | ||
package com.clody.clodybatch.reply; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.Mockito.when; | ||
|
||
import com.clody.clodybatch.service.ReplyScheduleService; | ||
import com.clody.domain.reply.ReplyType; | ||
import com.clody.meta.Schedule; | ||
import com.clody.meta.repository.ScheduleMetaRepository; | ||
import java.time.LocalDateTime; | ||
import java.time.temporal.ChronoUnit; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
public class ReplyScheduledServiceTest { | ||
|
||
@Mock | ||
private ScheduleMetaRepository scheduleMetaRepository; | ||
|
||
@InjectMocks | ||
private ReplyScheduleService replyScheduleService; | ||
|
||
|
||
@Test | ||
public void 스케줄_알림_발송할_대상_탐색() { | ||
|
||
//given | ||
LocalDateTime now = LocalDateTime.now().truncatedTo(ChronoUnit.SECONDS); | ||
|
||
Long userId = 1L; | ||
LocalDateTime yesterday = LocalDateTime.now().minusDays(1); | ||
ReplyType replyType = ReplyType.DYNAMIC; | ||
|
||
Schedule schedule = Schedule.create(userId, yesterday, replyType); | ||
|
||
//when | ||
when(scheduleMetaRepository.findSchedulesToNotify(now)).thenReturn( | ||
Collections.singletonList(schedule)); | ||
|
||
//then | ||
List<Schedule> result = replyScheduleService.findSchedulesToNotify(now); | ||
assertEquals(1, result.size()); | ||
assertEquals(schedule, result.getFirst()); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
clody-infra/src/main/java/com/clody/infra/config/SchedulingConfig.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,10 @@ | ||
package com.clody.infra.config; | ||
|
||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.scheduling.annotation.EnableScheduling; | ||
|
||
@Configuration | ||
@EnableScheduling | ||
public class SchedulingConfig { | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
clody-infra/src/main/java/com/clody/infra/models/alarm/DiaryNotifyService.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,36 @@ | ||
package com.clody.infra.models.alarm; | ||
|
||
import com.clody.domain.alarm.Alarm; | ||
import com.clody.domain.alarm.repository.AlarmRepository; | ||
import com.clody.infra.external.fcm.FcmService; | ||
import java.time.LocalTime; | ||
import java.time.temporal.ChronoUnit; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class DiaryNotifyService { | ||
|
||
private final AlarmRepository alarmRepository; | ||
private final FcmService fcmService; | ||
|
||
@Scheduled(cron = "0 0/10 * * * *") | ||
public void sendDiaryAlarms() { | ||
LocalTime time = LocalTime.now().truncatedTo(ChronoUnit.SECONDS); | ||
|
||
List<Alarm> alarmTarget = alarmRepository.findAllByTime(time); | ||
|
||
alarmTarget.stream() | ||
.filter(Alarm::isDiaryAlarm) | ||
.forEach( | ||
it -> fcmService.sendDiaryAlarm(it.getFcmToken()) | ||
); | ||
log.info("Alarm Successfully Sent"); | ||
} | ||
|
||
} |