# AppConfig.java

    package cloud.wconnect.finance.config;

    import cloud.wconnect.commonlib.component.BeanMapper;
    import cloud.wconnect.commonlib.config.BaseConfig;
    import cloud.wconnect.commonlib.exception.handler.CustomAsyncExceptionHandler;
    import cloud.wconnect.commonlib.redis.ConfigChangeWatcher;
    import cloud.wconnect.commonlib.redis.FeatureRedisService;
    import cloud.wconnect.commonlib.redis.PropertyI18nRedisService;
    import cloud.wconnect.commonlib.redis.TenantRedisService;
    import cloud.wconnect.commonlib.service.*;
    import cloud.wconnect.commonlib.util.LineMessageUtil;
    import cloud.wconnect.commonlib.util.S3ControllerUtil;
    import cloud.wconnect.sns.http.processor.Sender;
    import com.google.common.collect.Lists;
    import lombok.extern.slf4j.Slf4j;
    import org.apache.hc.client5.http.classic.HttpClient;
    import org.apache.hc.client5.http.config.ConnectionConfig;
    import org.apache.hc.client5.http.config.RequestConfig;
    import org.apache.hc.client5.http.impl.DefaultConnectionKeepAliveStrategy;
    import org.apache.hc.client5.http.impl.DefaultHttpRequestRetryStrategy;
    import org.apache.hc.client5.http.impl.classic.HttpClients;
    import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
    import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder;
    import org.apache.hc.core5.http.HttpHeaders;
    import org.apache.hc.core5.http.message.BasicHeader;
    import org.apache.hc.core5.util.TimeValue;
    import org.apache.hc.core5.util.Timeout;
    import org.dozer.DozerBeanMapper;
    import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
    import org.springframework.cache.annotation.EnableCaching;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Primary;
    import org.springframework.data.redis.core.StringRedisTemplate;
    import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
    import org.springframework.lang.NonNull;
    import org.springframework.retry.annotation.EnableRetry;
    import org.springframework.scheduling.annotation.AsyncConfigurer;
    import org.springframework.scheduling.annotation.EnableAsync;
    import org.springframework.scheduling.annotation.EnableScheduling;
    import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
    import org.springframework.web.client.DefaultResponseErrorHandler;
    import org.springframework.web.client.RestTemplate;
    import software.amazon.awssdk.services.sns.model.PublishResponse;

    import java.util.Arrays;
    import java.util.concurrent.Executor;

    /**
     * 配置类：应用相关配置
     *
     * @author zc
     * @date 2023/7/7
     */
    @EnableAsync
    @EnableRetry
    @EnableCaching
    @Configuration
    @EnableScheduling
    @Slf4j
    public class AppConfig extends BaseConfig implements AsyncConfigurer {

        @Value("${spring.application.name}:${spring.profiles.active}:${spring.cloud.config.label}")
        private String configKey;

        @Value("http://localhost:${management.server.port}/actuator/refresh")
        private String configRefreshEndpoint;

        @Bean
        @ConditionalOnProperty(name = "w-connect.configServer.enabled", havingValue = "true")
        public ConfigChangeWatcher getConfigChangeWatcher(StringRedisTemplate redisTemplate) {
            return new ConfigChangeWatcher(configKey, redisTemplate);
        }

        @Bean
        @ConditionalOnProperty(name = "w-connect.configServer.enabled", havingValue = "true")
        public ConfigChangeTrigger getConfigChangeTrigger(RestTemplate restTemplate) {
            return new ConfigChangeTrigger(configRefreshEndpoint, restTemplate);
        }

        /**
         * Bean映射类
         *
         * @author zc
         * @date 2023/7/7
         */
        @Bean
        public BeanMapper beanMapper(DozerBeanMapper dozerBeanMapper) {
            return new BeanMapper(dozerBeanMapper);
        }

        /**
         * Dozer映射器
         *
         * @return DozerBeanMapper
         * @author zc
         * @date 2023/7/7
         */
        @Bean(name = "org.dozer.Mapper")
        public DozerBeanMapper dozerBean() {
            DozerBeanMapper dozerBeanMapper = new DozerBeanMapper();
            dozerBeanMapper.setMappingFiles(Lists.newArrayList(
                    "dozer/base.xml"
            ));

            return dozerBeanMapper;
        }

        /**
         * property 远程服务类
         */
        @Bean
        public PropertyInternalService propertyInternalService() {
            return new PropertyInternalService();
        }

        /**
         * 租户远程服务类
         */
        @Bean
        public TenantInternalService tenantInternalService() {
            return new TenantInternalService();
        }

        /**
         * 用户远程服务类
         */
        @Bean
        public UserInternalService userInternalService() {
            return new UserInternalService();
        }

        /**
         * 邮件远程服务类
         */
        @Bean
        public MailInternalService mailInternalService() {
            return new MailInternalService();
        }

        /**
         * 角色服务
         */
        @Bean
        public RoleInternalService roleInternalService(){
            return new RoleInternalService();
        }

        /**
         * 用户组远程服务类
         */
        @Bean
        public UsergroupInternalService usergroupInternalService() {
            return new UsergroupInternalService();
        }

        /**
         * 权限远程服务类
         */
        @Bean
        public PermissionInternalService permissionInternalService() {
            return new PermissionInternalService();
        }

        /**
         * notify
         */
        @Bean
        public NotifyInternalService notifyInternalService() {
            return new NotifyInternalService();
        }

        /**
         * 调度远程服务类
         */
        @Bean
        public SchedulerInternalService schedulerInternalService(){
            return new SchedulerInternalService();
        }

        /**
         * line message util
         */
        @Bean
        public LineMessageUtil lineMessageUtil() {
            return new LineMessageUtil();
        }

        /**
         * 资产国际化
         */
        @Bean
        public PropertyI18nRedisService propertyI18nRedisService(){
            return new PropertyI18nRedisService();
        }

        /**
         * 租户缓存
         */
        @Bean
        public TenantRedisService tenantRedisService() {
            return new TenantRedisService();
        }

        /**
         * 公共 service
         *
         * @author yyt
         * @date 2024/6/6
         **/
        @Bean
        public CommonService commonService() {
            return new CommonService();
        }

        /**
         * 异步执行器
         *
         * @return Executor
         * @author zc
         * @date 2023/7/7
         */
        @Bean
        @Primary
        @Override
        @NonNull
        public Executor getAsyncExecutor() {
            ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
            executor.setCorePoolSize(6);
            executor.setMaxPoolSize(11);
            executor.setQueueCapacity(500);
            executor.setWaitForTasksToCompleteOnShutdown(true);
            executor.setAwaitTerminationSeconds(30);
            executor.setThreadNamePrefix("FinanceExecutor-");
            executor.initialize();

            return executor;
        }

        /**
         * 异步异常处理类
         *
         * @return AsyncUncaughtExceptionHandler
         * @author zc
         * @date 2023/7/7
         */
        @Override
        @NonNull
        public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
            return new CustomAsyncExceptionHandler();
        }

        /**
         * 自定义RestTemplate
         */
        @Bean(name = "restTemplate")
        public RestTemplate restTemplate() {
            /*
             * 连接池配置
             *
             * ConnectionConfig.setConnectTimeout：
             * 定义建立连接的超时时间（即 TCP 握手的最长等待时间）。
             * 发生在获取连接后，客户端尝试与服务器建立连接的阶段。
             * 示例：服务器不可达时触发。
             * ConnectionConfig.setSocketTimeout / RequestConfig.setResponseTimeout：
             * 定义数据读取的超时时间（等待服务器响应的时间）。
             * 发生在连接建立后，请求发送和接收数据的阶段。
             * 示例：服务器响应过慢时触发。
             * ConnectionConfig.setTimeToLive
             * 配置连接池中连接的最大存活时间,一旦连接的存活时间超过这个值，HttpClient 会将其视为过期并关闭，即使它仍然有效。
             */
            PoolingHttpClientConnectionManager connectionManager = PoolingHttpClientConnectionManagerBuilder.create()
                    .setMaxConnTotal(20) // 总连接数
                    .setMaxConnPerRoute(10) // 同路由的并发数
                    .setDefaultConnectionConfig(ConnectionConfig.custom()
                            .setConnectTimeout(Timeout.ofSeconds(5)) // 连接超时 5 秒
                            .setSocketTimeout(Timeout.ofSeconds(5)) // 数据读取超时 5 秒
                            .setTimeToLive(TimeValue.ofSeconds(30)) // 存活时间 30 秒
                            .build())
                    .build();

            /*
             * HttpClient配置
             * setRetryStrategy(new DefaultHttpRequestRetryStrategy(3, TimeValue.ofMilliseconds(500)))
             *
             * setKeepAliveStrategy(DefaultConnectionKeepAliveStrategy.INSTANCE)
             * 决定连接在完成请求后保持存活的具体时间，依赖服务器的 Keep-Alive 头。
             * 如果服务器返回 Keep-Alive: timeout=5，连接会在 5 秒后关闭（但不会超过 30 秒，因为 timeToLive 是 30 秒）。
             * 如果服务器未指定 Keep-Alive 头，连接会保持存活直到 timeToLive（30 秒）到期。
             * DefaultConnectionKeepAliveStrategy 适合大多数情况，因为它尊重服务器的 Keep-Alive 设置，灵活且兼容性强
             *
             * RequestConfig.setConnectionRequestTimeout：
             * 定义从连接池获取连接的超时时间。
             * 发生在请求执行前，客户端尝试从连接池中获取可用连接的阶段。
             * 示例：连接池中所有连接都被占用且无可用连接时触发。
             */
            HttpClient httpClient = HttpClients.custom()
                    .setConnectionManager(connectionManager)
                    .setRetryStrategy(new DefaultHttpRequestRetryStrategy(3, TimeValue.ofMilliseconds(500))) // 重试 3 次，重试间隔500毫秒
                    .setKeepAliveStrategy(DefaultConnectionKeepAliveStrategy.INSTANCE) // 设置默认的 KeepAlive 策略
                    .setDefaultRequestConfig(RequestConfig.custom()
                            .setConnectionRequestTimeout(Timeout.ofSeconds(2)) //设置从连接池获取连接的超时时间 2秒
                            .build())
                    .setDefaultHeaders(Arrays.asList(new BasicHeader(HttpHeaders.ACCEPT_ENCODING, "gzip,deflate"),
                            new BasicHeader(HttpHeaders.CONNECTION, "keep-alive"))) // 设置默认请求头
                    .build();

            // region HttpComponentsClientHttpRequestFactory
            HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
            // 创建 RestTemplate
            RestTemplate restTemplate = new RestTemplate(clientHttpRequestFactory);
            restTemplate.setErrorHandler(new DefaultResponseErrorHandler());
            return restTemplate;
        }

        /**
         * FeatureInternalService
         * @return
         */
        @Bean
        public FeatureInternalService featureInternalService(){
            return new FeatureInternalService();
        }

        /**
         * FeatureRedisService
         * @return
         */
        @Bean
        public FeatureRedisService featureRedisService(){
            return new FeatureRedisService();
        }

        // --- 以下bean为测试环境使用，生产环境不会使用 ---
        @Bean("3pSender")
        @ConditionalOnProperty(prefix = "third-party", value = "enabled", havingValue = "false")
        public Sender getSender() {
            return new EmptySender();
        }

        @Bean("moduleEventSender")
        @ConditionalOnProperty(prefix = "w-connect.module-event", value = "status", havingValue = "inactive")
        public Sender getModuleEventSender() {
            return new EmptySender();
        }

        static class EmptySender extends Sender {
            public EmptySender() {
                super(null, null);
            }

            @Override
            public PublishResponse publish(String subject, String messageJson) {
                return null;
            }
        }
        // --- 以上bean为测试环境使用，生产环境不会使用 ---

        /**
         * s3ControllerUtil
         *
         * @return cloud.wconnect.commonlib.util.S3ControllerUtil
         * @author lichen
         * @date 2025/5/12
         */
        @Bean
        public S3ControllerUtil s3ControllerUtil() {
            return new S3ControllerUtil();
        }

        /**
         * m3ConfigInternalService
         *
         @return cloud.wconnect.commonlib.service.M3ConfigInternalService
         * @author lichen
         * @date 2025/9/26
         */
        @Bean
        public M3ConfigInternalService m3ConfigInternalService(){
            return new M3ConfigInternalService();
        }

        /**
         * roomsConfigInternalService
         *
         * @return cloud.wconnect.commonlib.service.M3ConfigInternalService
         * @author lichen
         * @date 2025/9/26
         */
        @Bean
        public RoomsConfigInternalService roomsConfigInternalService(){
            return new RoomsConfigInternalService();
        }
    }