Skip to content

Commit

Permalink
refactor: Expo 동시 요청 제한으로 인한 chunk 수 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
stophwan committed Oct 23, 2024
1 parent 5dfc317 commit 7b2b24f
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.springframework.batch.core.Job
import org.springframework.batch.core.JobParameters
import org.springframework.batch.core.JobParametersBuilder
import org.springframework.batch.core.launch.JobLauncher
import org.springframework.scheduling.annotation.Scheduled
import org.springframework.stereotype.Component
import org.springframework.util.StopWatch
import java.util.concurrent.TimeUnit
Expand All @@ -14,6 +15,7 @@ class NotificationScheduler(
private val jobLauncher: JobLauncher,
private val notificationJob: Job,
) {
@Scheduled(cron = "0 0 5-23 * * *")
fun run() {
val jobParameters: JobParameters =
JobParametersBuilder()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package dnd11th.blooming.batch.notification

import dnd11th.blooming.client.fcm.PushNotification
import dnd11th.blooming.client.expo.PushNotification
import dnd11th.blooming.core.entity.myplant.UserPlantDto
import org.springframework.batch.core.Job
import org.springframework.batch.core.Step
Expand All @@ -20,7 +20,7 @@ import org.springframework.transaction.PlatformTransactionManager
@Configuration
class PlantNotificationJobConfig {
companion object {
const val CHUNK_SIZE: Int = 1000
const val CHUNK_SIZE: Int = 100
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package dnd11th.blooming.batch.notification

import dnd11th.blooming.client.fcm.PushNotification
import dnd11th.blooming.client.expo.PushNotification
import dnd11th.blooming.core.entity.myplant.UserPlantDto
import org.springframework.batch.core.configuration.annotation.StepScope
import org.springframework.batch.item.ItemProcessor
Expand All @@ -13,7 +13,7 @@ class PlantNotificationProcessor {
@StepScope
fun waterNotificationItemProcessor(): ItemProcessor<UserPlantDto, PushNotification> {
return ItemProcessor { userPlantDto ->
PushNotification.create(userPlantDto.myPlantId, userPlantDto.deviceToken, userPlantDto.plantNickname)
PushNotification.create(userPlantDto.deviceToken, userPlantDto.plantNickname)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ class PlantNotificationReader(
fun waterNotificationItemReader(): ListItemReader<UserPlantDto> {
val now: LocalTime = LocalTime.now()
val alarmTime = AlarmTime.fromHour(now)

val userPlantByAlarmTime: List<UserPlantDto> =
myPlantRepository.findNeedWaterPlantsByAlarmTimeInBatch(alarmTime)
return ListItemReader(userPlantByAlarmTime)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,36 @@
package dnd11th.blooming.batch.notification

import dnd11th.blooming.client.fcm.FcmService
import dnd11th.blooming.client.fcm.PushNotification
import dnd11th.blooming.client.expo.PushNotification
import dnd11th.blooming.client.expo.PushService
import dnd11th.blooming.common.util.Logger.Companion.log
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import org.springframework.batch.core.configuration.annotation.StepScope
import org.springframework.batch.item.ItemWriter
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import java.util.concurrent.Executors
import java.util.concurrent.atomic.AtomicInteger

@Configuration
class PlantNotificationWriter(
private val fcmService: FcmService,
private val pushService: PushService,
) {
private var counter = AtomicInteger(0)
private val customDispatcher = Executors.newFixedThreadPool(3).asCoroutineDispatcher()

@Bean
@StepScope
fun waterNotificationItemWriter(): ItemWriter<PushNotification> {
return ItemWriter { pushNotifications ->
runBlocking {
pushNotifications.forEach { pushNotification ->
launch(customDispatcher) {
val currentCount = counter.incrementAndGet()
val threadName = Thread.currentThread().name
fcmService.mock(pushNotification)
log.info { "Thread: $threadName, Count: $currentCount" }
}
val scope = CoroutineScope(customDispatcher)
return ItemWriter { chunk ->
val pushNotifications = chunk.toList()
scope.launch {
try {
val threadName = Thread.currentThread().name
pushService.mock(pushNotifications)
log.info { "Thread: $threadName, Sent ${pushNotifications.size} notifications" }
} catch (e: Exception) {
log.error(e) { "${e.message}" }
}
}
}
Expand Down

0 comments on commit 7b2b24f

Please sign in to comment.