diff --git a/src/main/kotlin/gdsc/plantory/PlantoryApplication.kt b/src/main/kotlin/gdsc/plantory/PlantoryApplication.kt index 10b9590..df8fb20 100644 --- a/src/main/kotlin/gdsc/plantory/PlantoryApplication.kt +++ b/src/main/kotlin/gdsc/plantory/PlantoryApplication.kt @@ -2,10 +2,19 @@ package gdsc.plantory import org.springframework.boot.autoconfigure.SpringBootApplication import org.springframework.boot.runApplication +import org.springframework.scheduling.annotation.EnableScheduling +import java.util.TimeZone +import javax.annotation.PostConstruct +@EnableScheduling @SpringBootApplication class PlantoryApplication fun main(args: Array) { runApplication(*args) + + @PostConstruct + fun setTimezone() { + TimeZone.setDefault(TimeZone.getTimeZone("Asia/Seoul")) + } } diff --git a/src/main/kotlin/gdsc/plantory/event/notification/WaterCycleEventListener.kt b/src/main/kotlin/gdsc/plantory/event/notification/WaterCycleEventListener.kt index e16fe66..8df0b61 100644 --- a/src/main/kotlin/gdsc/plantory/event/notification/WaterCycleEventListener.kt +++ b/src/main/kotlin/gdsc/plantory/event/notification/WaterCycleEventListener.kt @@ -8,10 +8,9 @@ import com.google.firebase.messaging.Message import com.google.firebase.messaging.Notification import gdsc.plantory.event.FCMChannel import org.slf4j.LoggerFactory +import org.springframework.context.event.EventListener import org.springframework.scheduling.annotation.Async import org.springframework.stereotype.Component -import org.springframework.transaction.event.TransactionPhase -import org.springframework.transaction.event.TransactionalEventListener @Component class WaterCycleEventListener( @@ -22,12 +21,15 @@ class WaterCycleEventListener( private val log = LoggerFactory.getLogger(WaterCycleEventListener::class.java) } - @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT) @Async + @EventListener fun sendFcmNotification(events: WaterCycleEvents) { + log.info("send FCM notification by WaterCycleEventListener") val messages: List = createMessages(events.plantsNeedWateredToday, FCMChannel.WATER_ALERT.name) try { + if (messages.isEmpty()) return + firebaseMessaging.sendEach(messages) } catch (e: FirebaseMessagingException) { log.warn("fail send FCM message", e) diff --git a/src/main/kotlin/gdsc/plantory/plant/service/ReminderService.kt b/src/main/kotlin/gdsc/plantory/plant/service/ReminderService.kt index 2288850..4776de8 100644 --- a/src/main/kotlin/gdsc/plantory/plant/service/ReminderService.kt +++ b/src/main/kotlin/gdsc/plantory/plant/service/ReminderService.kt @@ -5,6 +5,7 @@ import gdsc.plantory.event.notification.WaterCycleEvent import gdsc.plantory.event.notification.WaterCycleEvents import gdsc.plantory.plant.domain.CompanionPlantRepository import gdsc.plantory.plant.presentation.dto.CompanionPlantWaterCycleDto +import org.slf4j.LoggerFactory import org.springframework.scheduling.annotation.Scheduled import org.springframework.stereotype.Service import org.springframework.transaction.annotation.Transactional @@ -16,8 +17,13 @@ class ReminderService( private val companionPlantRepository: CompanionPlantRepository, ) { - @Scheduled(cron = "0 0 8 * * *") + companion object { + private val log = LoggerFactory.getLogger(this.javaClass) + } + + @Scheduled(cron = "0 */5 * * * *") fun sendWaterNotification() { + log.info("물주기 알림을 전송합니다."); val companionPlants = companionPlantRepository.findAllByNextWaterDate(LocalDate.now()) val events = WaterCycleEvents(buildWaterCycleEvents(companionPlants)) Events.raise(events)