package com.crunii.micro.service.sysmgr;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure;
import com.crunii.micro.common.utils.JasyptEncryptorUtil;
import com.crunii.micro.config.ClientVersionRibbonConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.boot.autoconfigure.data.redis.RedisReactiveAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration;
import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClients;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.EnableMBeanExport;
import org.springframework.context.annotation.ImportResource;
import org.springframework.jmx.support.RegistrationPolicy;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.util.StringUtils;

/**
 * @author 田平 create 2023-01-12 03:27:33
 */
@ImportResource({ "classpath:spring/*.xml" })
@EnableCaching
@EnableScheduling
@EnableFeignClients(basePackages = {"com.crunii.micro.feign"})
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
@SpringBootApplication(
		exclude = {
				RedisAutoConfiguration.class,
				RedisReactiveAutoConfiguration.class,
				TransactionAutoConfiguration.class,
				DataSourceAutoConfiguration.class,
				DruidDataSourceAutoConfigure.class })
@LoadBalancerClients(defaultConfiguration = ClientVersionRibbonConfiguration.class)
public class Application {

	public static void main(String[] args) {
		JasyptEncryptorUtil.setConfigClientProps();
		JasyptEncryptorUtil.setAdminClientProps();
		// 接netty重复设置的问题
		System.setProperty("es.set.netty.runtime.available.processors", "false");
		String env = System.getProperty("spring.profiles.active");
		if (!StringUtils.hasText(env)) {
			// local or dev or prod
			env = "dev";
			System.setProperty("spring.profiles.active", env);
		}
		SpringApplication.run(Application.class, args);
	}

	//	@Bean
//	public TomcatServletWebServerFactory createTomcatServletWebServerFactory() {
//		TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
//		tomcat.setProtocol("org.apache.coyote.http11.Http11Nio2Protocol");
//		tomcat.addConnectorCustomizers(connector -> {
//			Http11Nio2Protocol protocol = (Http11Nio2Protocol) connector.getProtocolHandler();
//			// 设置最大连接数
//			protocol.setConnectionTimeout(60000);// 1分钟
//			protocol.setMaxConnections(512);
//			// 设置最大线程数
//			protocol.setMaxThreads(200);
//		});
//		return tomcat;
//	}

	@Bean
	public UndertowServletWebServerFactory createUndertowServletWebServerFactory() {
		UndertowServletWebServerFactory undertowServletWebServerFactory = new UndertowServletWebServerFactory();
		return undertowServletWebServerFactory;
		/*
		TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
		tomcat.setProtocol("org.apache.coyote.http11.Http11Nio2Protocol");
		tomcat.addConnectorCustomizers(connector -> {
			Http11Nio2Protocol protocol = (Http11Nio2Protocol) connector.getProtocolHandler();
			// 设置最大连接数
			protocol.setConnectionTimeout(60000);// 1分钟
			protocol.setMaxConnections(512);
			// 设置最大线程数
			protocol.setMaxThreads(200);
		});
		return tomcat; */
	}

}
