package com.sqx;

import com.sqx.common.utils.MyGlobalThreadPool;
import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.actuate.autoconfigure.metrics.MeterRegistryCustomizer;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.event.ContextClosedEvent;

@SpringBootApplication
public class SqxApplication implements CommandLineRunner, ApplicationListener<ApplicationEvent> {

	public static void main(String[] args) {
		SpringApplication.run(SqxApplication.class, args);
	}

	@Override
	public void run(String... args) throws Exception {
		// 初始化公共线程池
		MyGlobalThreadPool.init();

		System.out.println("(♥◠‿◠)ﾉﾞ  同城外卖项目启动成功   ლ(´ڡ`ლ)ﾞ  \n"+
				"       _    \n" +
				"      | |   \n" +
				"  ___ | | __\n" +
				" / _ \\| |/ /\n" +
				"| (_) |   < \n" +
				" \\___/|_|\\_\\");
	}

	@Override
	public void onApplicationEvent(ApplicationEvent event) {
		if (event instanceof ContextClosedEvent) {
			// 应用停止时的操作
			MyGlobalThreadPool.shutdown(false);
		}
	}

	@Bean
	MeterRegistryCustomizer<MeterRegistry> configurer(@Value("${spring.application.name}") String applicationName, @Value("${spring.profiles.active}") String active) {
		return (registry) -> registry.config().commonTags("application", applicationName + "-" + active);
	}
}