批量添加测试数据
网页生成
这个链接一次可以生成1000条数据
(blank是空白数据的占比,100%就是全null;往下滑format可以选数据格式我选的sql)
for循环
一开始想的是批量生成测试数据后,直接粘贴到控制台执行就可以了。但是50w条数据得点500次。因为对测试数据得内容没有要求,所以就想着弄个for循环来插入数据。但是等了十分钟都没搞好。。
线程池
找了个以前的作业demo来配置好线程池
yml文件
# springboot异步线程配置
async:
executor:
thread:
core_pool_size: 5 #配置核心线程数
keep_alive_seconds: 60 #设置线程活跃时间(秒)
max_pool_size: 10 #配置最大线程数
name_prefix: async-service- #配置线程池中的线程的名称前缀
queue_capacity: 100 #配置队列大小
线程池配置类
@Configuration
@EnableAsync
public class ExecutorConfig {
private static final Logger logger = LoggerFactory.getLogger(ExecutorConfig.class);
@Value("${async.executor.thread.core_pool_size}")
private int corePoolSize;
@Value("${async.executor.thread.max_pool_size}")
private int maxPoolSize;
@Value("${async.executor.thread.queue_capacity}")
private int queueCapacity;
@Value("${async.executor.thread.name_prefix}")
private String namePrefix;
@Value("${async.executor.thread.keep_alive_seconds}")
private Integer keepAliveSeconds;
@Bean(name = "asyncServiceExecutor")
public ThreadPoolTaskExecutor asyncServiceExecutor() {
logger.info("start asyncServiceExecutor");
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
// 配置核心线程数
executor.setCorePoolSize(corePoolSize);
// 配置最大线程数
executor.setMaxPoolSize(maxPoolSize);
// 设置线程活跃时间(秒)
executor.setKeepAliveSeconds(keepAliveSeconds);
// 配置队列大小
executor.setQueueCapacity(queueCapacity);
// 配置线程池中的线程的名称前缀
executor.setThreadNamePrefix(namePrefix);
// 等待所有任务结果候再关闭线程池
executor.setWaitForTasksToCompleteOnShutdown(true);
// 任务的等待时间 如果超过这个时间还没有销毁就 强制销毁,以确保应用最后能够被关闭,而不是阻塞住
executor.setAwaitTerminationSeconds(60);
// 设置拒绝策略
// rejection-policy:当pool已经达到max size的时候,如何处理新任务
// CALLER_RUNS:不在新线程中执行任务,而是有调用者所在的线程来执行
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
// 执行初始化,初始化 core 线程
executor.initialize();
return executor;
}
}
自动导包要注意别导错了
这里自动导包导错了
应该是这俩
控制层
@Controller
public class TestController {
private static final Logger logger= LoggerFactory.getLogger(TestController.class);
@Autowired
AsyncService asyncService;
@LogAround("测试插入数据")
@PostMapping("/test")
public void test(){
CountDownLatch countDownLatch=new CountDownLatch(10000);
LocalDateTime start = LocalDateTime.now();
for(int i=0;i<10000;i++){
asyncService.executeAsyncInsertData(countDownLatch);
}
try{
countDownLatch.await();
}catch (Exception e){
logger.info("线程异常:{}",e.getMessage());
}
}
接口
public interface AsyncService {
public void executeAsyncInsertData(CountDownLatch countDownLatch);
}
实现
@Service
public class AsyncServiceImpl implements AsyncService{
@Autowired
TestDaoImpl testDao; //Mapper来的,这个demo没有就用Dao
private static final Logger logger = LoggerFactory.getLogger(AsyncServiceImpl.class);
@Async("asyncServiceExecutor")
@Override
public void executeAsyncInsertData(CountDownLatch countDownLatch) {
try{
logger.info("异步线程开始");
testDao.addTestData();
logger.info("异步线程结束");
}finally {
countDownLatch.countDown();
}
}
}
这里是用靠Spring异步注解@Async实现的,配置类Bean名字要和异步方法上的@