Skip to content

Commit

Permalink
feat(#53): converted application config classes java to kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
KimDoubleB committed Jan 20, 2023
1 parent 1ff4dc3 commit 285db7e
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 91 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package site.archive.config

import org.slf4j.LoggerFactory
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler
import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.scheduling.annotation.AsyncConfigurer
import org.springframework.scheduling.annotation.EnableAsync
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor
import site.archive.config.AsyncConfiguration.MailExecutorProperty
import java.lang.reflect.Method
import java.util.concurrent.Executor

@Configuration
@EnableConfigurationProperties(MailExecutorProperty::class)
@EnableAsync
class AsyncConfiguration(val mailExecutorProperty: MailExecutorProperty) : AsyncConfigurer {

private val log = LoggerFactory.getLogger(javaClass)

@Bean("mailExecutor")
override fun getAsyncExecutor(): Executor? {
val executor = ThreadPoolTaskExecutor()
executor.corePoolSize = mailExecutorProperty.corePoolSize
executor.maxPoolSize = mailExecutorProperty.maxPoolSize
executor.queueCapacity = mailExecutorProperty.queueCapacity
executor.setAwaitTerminationSeconds(mailExecutorProperty.awaitTerminationSeconds)
executor.setWaitForTasksToCompleteOnShutdown(true)
executor.setThreadNamePrefix(MailExecutorProperty.THREAD_NAME_PREFIX)
executor.initialize()
return executor
}

override fun getAsyncUncaughtExceptionHandler(): AsyncUncaughtExceptionHandler? {
return AsyncUncaughtExceptionHandler { exception: Throwable?, method: Method, _: Array<Any?>? ->
log.error("Async Exception handler: '{}' threw unexpected exception ", method.toGenericString(), exception)
}
}

@ConfigurationProperties(prefix = "spring.mail.executor")
class MailExecutorProperty(
val corePoolSize: Int = 0,
val maxPoolSize: Int = 0,
val queueCapacity: Int = 0,
val awaitTerminationSeconds: Int = 0
) {
companion object {
const val THREAD_NAME_PREFIX = "mail-executor-"
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package site.archive.config

import org.springframework.boot.web.client.RestTemplateBuilder
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.http.client.BufferingClientHttpRequestFactory
import org.springframework.http.client.SimpleClientHttpRequestFactory
import org.springframework.http.converter.StringHttpMessageConverter
import org.springframework.web.client.RestTemplate
import java.nio.charset.StandardCharsets
import java.time.Duration
import java.util.function.Supplier

@Configuration
class ClientConfig {

@Bean
fun restTemplateBuilder(): RestTemplateBuilder {
return RestTemplateBuilder()
}

@Bean
fun restTemplate(restTemplateBuilder: RestTemplateBuilder): RestTemplate {
return restTemplateBuilder
.requestFactory(Supplier { BufferingClientHttpRequestFactory(SimpleClientHttpRequestFactory()) })
.setConnectTimeout(Duration.ofMillis(3000))
.setReadTimeout(Duration.ofMillis(3000))
.additionalMessageConverters(StringHttpMessageConverter(StandardCharsets.UTF_8))
.build()
}

}

0 comments on commit 285db7e

Please sign in to comment.