# App.java

    package cloud.wconnect.frontoffice.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.*;
    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.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Primary;
    import org.springframework.context.annotation.Profile;
    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.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 kazaff
     */
    @Configuration
    @EnableAsync
    @EnableRetry
    @Slf4j
    public class App 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映射类
         */
        @Bean
        public BeanMapper beanMapper(DozerBeanMapper dozerBeanMapper) {
            return new BeanMapper(dozerBeanMapper);
        }

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

            return dozerBeanMapper;
        }

        /**
         * 异步执行器
         *
         * @return Executor
         * @author chenx
         * @date 2021/3/1
         */
        @Bean
        @Primary
        @Override
        @NonNull
        public Executor getAsyncExecutor() {
            ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
            executor.setCorePoolSize(5);
            executor.setMaxPoolSize(8);
            executor.setQueueCapacity(500);
            executor.setWaitForTasksToCompleteOnShutdown(true);
            executor.setAwaitTerminationSeconds(30);
            executor.setThreadNamePrefix("FrontOfficeExecutor-");
            executor.initialize();

            return executor;
        }

        /**
         * 异步异常处理类
         *
         * @return AsyncUncaughtExceptionHandler
         * @author chenx
         * @date 2021/3/1
         */
        @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;
        }

        @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);
        }

        /**
         * @return cloud.wconnect.commonlib.service.MailInternalService
         * @description 注入邮件内部bean
         * @author lichen
         * @date 2023/3/10 14:04
         */
        @Bean
        public MailInternalService mailInternalService() {
            return new MailInternalService();
        }

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

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

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

        /**
         * 角色关联用户远程服务类
         *
         * @return cloud.wconnect.commonlib.service.RoleInternalService
         * @author lichen
         * @date 2023/4/26
         */
        @Bean
        public RoleInternalService roleInternalService() {
            return new RoleInternalService();
        }

        /**
         * 用户远程服务类
         *
         * @return cloud.wconnect.commonlib.service.UserInternalService
         * @author lichen
         * @date 2023/4/27
         */
        @Bean
        public UserInternalService userInternalService() {
            return new UserInternalService();
        }

        /**
         * 通知远程服务类
         *
         * @return cloud.wconnect.commonlib.service.NotifyInternalService
         * @author lichen
         * @date 2023/11/22
         */
        @Bean
        public NotifyInternalService notifyInternalService() {
            return new NotifyInternalService();
        }

        @Bean
        public SchedulerInternalService schedulerInternalService(){
            return new SchedulerInternalService();
        }

        /**
         * 生成line消息的工具类
         *
         * @return cloud.wconnect.commonlib.util.LineMessageUtil
         * @author lichen
         * @date 2023/11/22
         */
        @Bean
        public LineMessageUtil lineMessageUtil() {
            return new LineMessageUtil();
        }

        /**
         * 国际化配置缓存
         */
        @Bean
        public PropertyI18nRedisService propertyI18nRedisService(){
            return new PropertyI18nRedisService();
        }

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

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

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

        /**
         * 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
         */
    //    @Profile("!apitest")
        @Bean
        public S3ControllerUtil s3ControllerUtil() {
            return new S3ControllerUtil();
        }

        /**
         * 资产业务日期缓存
         *
         * @return cloud.wconnect.commonlib.redis.PropertyBusinessDateRedisService
         * @author lichen
         * @date 2025/12/18
         */
        @Bean
        public PropertyBusinessDateRedisService propertyBusinessDateRedisService() {
            return new PropertyBusinessDateRedisService();
        }

        /**
         * 数据权限配置内部服务类
         */
        @Bean
        public DataPermissionConfigInternalService dataPermissionConfigInternalService() {
            return new DataPermissionConfigInternalService();
        }

    }