11package com.tiarebalbi.infinitic.spring
22
33import io.infinitic.clients.InfiniticClient
4- import io.infinitic.pulsar.config.ClientConfig
54import io.infinitic.workers.InfiniticWorker
6- import io.infinitic.workers.config.WorkerConfig
75import org.slf4j.LoggerFactory
8- import org.springframework.beans.factory.InitializingBean
96import org.springframework.boot.autoconfigure.AutoConfiguration
107import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
118import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
@@ -17,33 +14,46 @@ import org.springframework.core.io.ResourceLoader
1714/* *
1815 * Configuration class for Infinitic Auto-Configuration.
1916 *
20- * This class is responsible for configuring the Infinitic library based on the provided application properties.
21- * It creates an InfiniticClient and configures it with the specified client configuration file.
17+ * This class is responsible for configuring the Infinitic library based on
18+ * the provided application properties. It creates an InfiniticClient and
19+ * configures it with the specified client configuration file.
2220 *
23- * This class is annotated with `@Configuration` to indicate that it should be processed by Spring's application context.
24- * It is also annotated with `@ConditionalOnProperty` to conditionally enable this configuration based on the value of the
25- * "infinitic.enabled" property. If the property is not present or has a value other than "true", this configuration will
21+ * This class is annotated with `@Configuration` to indicate that it should
22+ * be processed by Spring's application context. It is also annotated with
23+ * `@ConditionalOnProperty` to conditionally enable this configuration
24+ * based on the value of the "infinitic.enabled" property. If the property
25+ * is not present or has a value other than "true", this configuration will
2626 * not be loaded.
2727 *
28- * Configuration properties can be provided using the `InfiniticProperties` class, which is enabled with the
29- * `@EnableConfigurationProperties` annotation.
28+ * Configuration properties can be provided using the `InfiniticProperties`
29+ * class, which is enabled with the `@EnableConfigurationProperties`
30+ * annotation.
3031 *
31- * The InfiniticAutoConfiguration class requires two constructor arguments: `InfiniticProperties` and `ResourceLoader`.
32+ * The InfiniticAutoConfiguration class requires two constructor arguments:
33+ * `InfiniticProperties` and `ResourceLoader`.
3234 *
33- * The `InfiniticProperties` argument is used to access the configuration properties for the Infinitic library,
34- * including the location of the client configuration file.
35+ * The `InfiniticProperties` argument is used to access the configuration
36+ * properties for the Infinitic library, including the location of the
37+ * client configuration file.
3538 *
36- * The `ResourceLoader` argument is used to load the client configuration file from the classpath or file system.
39+ * The `ResourceLoader` argument is used to load the client configuration
40+ * file from the classpath or file system.
3741 *
38- * The `configureClient` method is annotated with `@Bean`, `@ConditionalOnProperty`, and `@ConditionalOnMissingBean`.
39- * It creates and configures an InfiniticClient bean if the "infinitic.client.enabled" property is present and has a
40- * value of "true". It uses the `getClientConfig` method to obtain the client configuration.
42+ * The `configureClient` method is annotated with `@Bean`,
43+ * `@ConditionalOnProperty`, and `@ConditionalOnMissingBean`. It creates
44+ * and configures an InfiniticClient bean if the "infinitic.client.enabled"
45+ * property is present and has a value of "true". It uses the
46+ * `getClientConfig` method to obtain the client configuration.
4147 *
42- * The `getClientConfig` method retrieves the client configuration file location from the `InfiniticProperties` object,
43- * loads the file using the `ResourceLoader`, and creates a `ClientConfig` object from the loaded file.
48+ * The `getClientConfig` method retrieves the client configuration file
49+ * location from the `InfiniticProperties` object, loads the file using the
50+ * `ResourceLoader`, and creates a `ClientConfig` object from the loaded
51+ * file.
4452 *
45- * @param properties the InfiniticProperties object containing the configuration properties for the Infinitic library
46- * @param resourceLoader the ResourceLoader object used to load the client configuration file
53+ * @param properties the InfiniticProperties object containing the
54+ * configuration properties for the Infinitic library
55+ * @param resourceLoader the ResourceLoader object used to load the client
56+ * configuration file
4757 * @author tiare.balbi
4858 */
4959@AutoConfiguration
@@ -62,40 +72,42 @@ class InfiniticAutoConfiguration(
6272 @Bean
6373 @ConditionalOnProperty(name = [" infinitic.worker.enabled" ], havingValue = " true" )
6474 @ConditionalOnMissingBean
65- fun configureWorker () =
66- InitializingBean {
67- val config = getWorkerConfig()
68- InfiniticWorker .fromConfig(config).use {
69- logger.info(" Starting infinitic worker" )
70- if (properties.worker.executionMode.isAsync()) {
71- it.startAsync()
72- } else {
73- it.start()
74- }
75+ fun configureWorker (): InfiniticWorker {
76+ val config = getWorkerConfig()
77+ val worker = InfiniticWorker .fromConfigFile(config)
78+ worker.use {
79+ logger.info(" Starting infinitic worker" )
80+ if (properties.worker.executionMode.isAsync()) {
81+ it.startAsync()
82+ } else {
83+ it.start()
7584 }
7685 }
7786
87+ return worker
88+ }
89+
7890 @Bean
7991 @ConditionalOnProperty(name = [" infinitic.client.enabled" ], havingValue = " true" )
8092 @ConditionalOnMissingBean
8193 fun configureClient (): InfiniticClient {
8294 logger.info(" Configuring infinitic client" )
83- return InfiniticClient .fromConfig (getClientConfig(), getWorkerConfig())
95+ return InfiniticClient .fromConfigFile (getClientConfig(), getWorkerConfig())
8496 }
8597
86- private fun getClientConfig (): ClientConfig {
98+ private fun getClientConfig (): String {
8799 val configuration =
88100 properties.client.configuration
89101 ? : throw IllegalStateException (" Unable to find client configuration file for infinitic." )
90102 val configurationFile = resourceLoader.getResource(configuration)
91- return ClientConfig .fromFile( configurationFile.file.absolutePath)
103+ return configurationFile.file.absolutePath
92104 }
93105
94- private fun getWorkerConfig (): WorkerConfig {
106+ private fun getWorkerConfig (): String {
95107 val configuration =
96108 properties.worker.configuration
97109 ? : throw IllegalStateException (" Unable to find worker infinitic configuration" )
98110 val configurationFile = resourceLoader.getResource(configuration)
99- return WorkerConfig .fromFile( configurationFile.file.absolutePath)
111+ return configurationFile.file.absolutePath
100112 }
101113}
0 commit comments