-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(#53): converted application config classes java to kotlin
- Loading branch information
1 parent
1ff4dc3
commit 285db7e
Showing
4 changed files
with
86 additions
and
91 deletions.
There are no files selected for viewing
64 changes: 0 additions & 64 deletions
64
archive-application/src/main/java/site/archive/config/AsyncConfiguration.java
This file was deleted.
Oops, something went wrong.
27 changes: 0 additions & 27 deletions
27
archive-application/src/main/java/site/archive/config/ClientConfig.java
This file was deleted.
Oops, something went wrong.
54 changes: 54 additions & 0 deletions
54
archive-application/src/main/kotlin/site/archive/config/AsyncConfiguration.kt
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,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-" | ||
} | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
archive-application/src/main/kotlin/site/archive/config/ClientConfig.kt
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,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() | ||
} | ||
|
||
} |