getExecutor()
创建一个线程池供下面的调用
receiveConfigInfo()
每次当前的dataId只要改变,就会调用这个方法
```package com.laoyang.dtp;
import com.alibaba.nacos.api.annotation.NacosInjected;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.Listener;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.io.ByteArrayResource;
import java.util.Properties;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
/**
- @author:Kevin
- @create: 2023-10-24 19:56
- @Description: nacos自带监听器
*/
public class NacosLinsenter implements Listener, InitializingBean {
@NacosInjected
private ConfigService configService;
@Autowired
private DtpExecutor executor;
//nacos的dataId的名称
private static final String DATA_ID = "dtp.yaml";
private static final String GROUP = "DEFAULT_GROUP";
//最大核心数
private static final String CORE_POOL_SIZE = "dtp.core-pool-size";
//最大线程数
private static final String MAXIMUM_POOL_SIZE = "dtp.maximum-pool-size";
//创建一个线程池供下面的调用
@Override
public Executor getExecutor() {
return Executors.newFixedThreadPool(1);
}
//每次当前的dataId只要改变,就会调用这个方法
//但是调用这个方法的线程需要上面的方法创建一个线程池
@Override
public void receiveConfigInfo(String s) {
//首先需要将字符串yml格式转化为map的格式
YamlPropertiesFactoryBean factoryBean = new YamlPropertiesFactoryBean();
factoryBean.setResources(new ByteArrayResource(s.getBytes()));
//使用springboot内置工具类转换为Properties类似于map的格式
Properties object = factoryBean.getObject();
//获取更新后的数据
String corePoolSize = object.getProperty(CORE_POOL_SIZE);
String maximumPoolSize = object.getProperty(MAXIMUM_POOL_SIZE);
//直接更新数据
executor.setCorePoolSize(Integer.parseInt(corePoolSize));
executor.setMaximumPoolSize(Integer.parseInt(maximumPoolSize));
}
@Override
public void afterPropertiesSet() throws Exception {
//将这个NacosLinsenter与当前的dataId一一绑定
configService.addListener(DATA_ID,GROUP,this);
}
}
##3. 开始在user模块使用 dtp-spring-boot-starter模块
创建启动Springboot配置类
```package com.laoyang;
import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySource;
import com.alibaba.nacos.spring.context.annotation.discovery.EnableNacosDiscovery;
import com.laoyang.dtp.DtpExecutor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
/**
* @author:Kevin
* @create: 2023-10-24 17:04
* @Description:
*/
@SpringBootApplication
@NacosPropertySource(dataId = "dtp.yaml", autoRefreshed = true)
public class UserApplication {
public static void main(String[] args) {
SpringApplication.run(UserApplication.class, args);
}
}
创建Controller注入动态线程池对象来使用
```package com.laoyang.Controller;
import com.laoyang.dtp.DtpExecutor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import java.util.concurrent.ThreadPoolExecutor;
/**
- @author:Kevin
- @create: 2023-10-24 17:05
@Description: 视图层
*/
@Controller
public class UserController {@Autowired
private DtpExecutor executor;@GetMapping
public Integer test(){executor.execute(() -> dotest()); return 1;
}
public void dotest(){
System.out.println("dotest");
}
}
```